Skip to content

Commit

Permalink
Fix/tests failing (#5)
Browse files Browse the repository at this point in the history
* fix: create test not properly updated for passing tests

Some tests suites were expecting the `it` function to be passed from the `describe` instead of importing it from `vitest`
A few leftover port numbers were not updated

* refac: Improve error message on not passing the baseURL

---------

Co-authored-by: thejoekingmann <[email protected]>
  • Loading branch information
thejoekingmann and thejoekingmann authored Jul 19, 2024
1 parent 22f8476 commit cdae87b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/test/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createApp, toNodeListener } from "h3";
import { type Listener, listen } from "listhen";
import { afterAll, beforeAll, describe, expect, expectTypeOf } from "vitest";
import { afterAll, beforeAll, describe, expect, expectTypeOf, it } from "vitest";
import { ZodError, z } from "zod";
import {
type FetchSchemaRoutes,
Expand Down Expand Up @@ -55,7 +55,7 @@ const schema = {
"@patch/method": {},
} satisfies FetchSchemaRoutes;

describe("create-fetch-runtime-test", (it) => {
describe("create-fetch-runtime-test", () => {
const $fetch = createFetch({
baseURL: "http://localhost:4001",
schema: createSchema(schema),
Expand Down Expand Up @@ -108,7 +108,7 @@ describe("create-fetch-runtime-test", (it) => {
});
});

describe("create-fetch-type-test", (it) => {
describe("create-fetch-type-test", () => {
const $fetch = createFetch({
baseURL: "http://localhost:4001",
customFetchImpl: async (req, init) => {
Expand Down Expand Up @@ -193,7 +193,7 @@ describe("create-fetch-type-test", (it) => {
it("should infer params", () => {
const f = createFetch({
schema: createSchema(schema),
baseURL: "http://localhost:3000",
baseURL: "http://localhost:4001",
customFetchImpl: async (url, req) => {
return new Response();
},
Expand All @@ -216,13 +216,16 @@ describe("create-fetch-type-test", (it) => {

it("should infer default response and error types", () => {
const $fetch = createFetch({
baseURL: "http://localhost:3000",
baseURL: "http://localhost:4001",
defaultOutput: z.object({
data: z.string(),
}),
defaultError: z.object({
error: z.string(),
}),
customFetchImpl: async (url, req) => {
return new Response();
},
});

expectTypeOf($fetch("/")).toMatchTypeOf<
Expand Down
13 changes: 12 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,18 @@ export function getURL(url: string, options?: BetterFetchOption) {
url = url.replace(`@${m}/`, "/");
}
}
let _url = url.startsWith("http") ? url : new URL(url, options?.baseURL);
let _url;
try {
_url = url.startsWith("http") ? url : new URL(url, options?.baseURL);
} catch(e) {
if(e instanceof TypeError){
if (!options?.baseURL) {
throw TypeError(`Invalid URL ${url}. Are you passing in a relative url but not setting the baseURL?`)
}
throw TypeError(`Invalid URL ${url}. Please validate that you are passing the correct input.`)
}
throw e;
}

/**
* Dynamic Parameters.
Expand Down

0 comments on commit cdae87b

Please sign in to comment.