From f39a14c494b8af5c034e8233c42630a8b1a092b3 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Thu, 7 Nov 2024 07:49:45 -0500 Subject: [PATCH 1/3] Format tests --- package.json | 4 +-- tests/api.test.ts | 82 +++++++++++++++++++++++------------------------ 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/package.json b/package.json index 0a977f0..6977bad 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,8 @@ "test:watch": "vitest run --watch", "lint": "eslint src", "lint:fix": "eslint src --fix", - "format": "prettier src --write", - "format:check": "prettier src --check", + "format": "prettier src tests --write", + "format:check": "prettier src tests --check", "build": "tsc -p tsconfig.build.json" }, "repository": { diff --git a/tests/api.test.ts b/tests/api.test.ts index 0f1c179..23d816a 100644 --- a/tests/api.test.ts +++ b/tests/api.test.ts @@ -100,41 +100,41 @@ describe('testing mockResponse', () => { expect(fetch.mock.calls[0]![0]).toEqual(new URL('https://instagram.com')); }); - it('should allow empty response bodies', async () => { - fetch.mockResponseOnce(null, { status: 204 }); - fetch.mockResponseOnce(undefined, { status: 204 }); - fetch.mockResponseOnce(() => null, { status: 204 }); - fetch.mockResponseOnce(() => undefined, { status: 204 }); - fetch.mockResponseOnce(() => Promise.resolve(null), { status: 204 }); - fetch.mockResponseOnce(() => Promise.resolve(undefined), { status: 204 }); - fetch.mockResponseOnce({ status: 204 }); - fetch.mockResponseOnce(() => ({ status: 204 })); - fetch.mockResponseOnce(() => Promise.resolve({ status: 204 })); - fetch.mockResponseOnce(new Response(null, { status: 204 })); - fetch.mockResponseOnce(new Response(undefined, { status: 204 })); - fetch.mockResponseOnce(() => new Response(null, { status: 204 })); - fetch.mockResponseOnce(() => new Response(undefined, { status: 204 })); - fetch.mockResponseOnce(() => Promise.resolve(new Response(null, { status: 204 }))); - fetch.mockResponseOnce(() => Promise.resolve(new Response(undefined, { status: 204 }))); - fetch.mockResponseOnce('done'); - - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe(''); - expect(await request()).toBe('done'); - }); + it('should allow empty response bodies', async () => { + fetch.mockResponseOnce(null, { status: 204 }); + fetch.mockResponseOnce(undefined, { status: 204 }); + fetch.mockResponseOnce(() => null, { status: 204 }); + fetch.mockResponseOnce(() => undefined, { status: 204 }); + fetch.mockResponseOnce(() => Promise.resolve(null), { status: 204 }); + fetch.mockResponseOnce(() => Promise.resolve(undefined), { status: 204 }); + fetch.mockResponseOnce({ status: 204 }); + fetch.mockResponseOnce(() => ({ status: 204 })); + fetch.mockResponseOnce(() => Promise.resolve({ status: 204 })); + fetch.mockResponseOnce(new Response(null, { status: 204 })); + fetch.mockResponseOnce(new Response(undefined, { status: 204 })); + fetch.mockResponseOnce(() => new Response(null, { status: 204 })); + fetch.mockResponseOnce(() => new Response(undefined, { status: 204 })); + fetch.mockResponseOnce(() => Promise.resolve(new Response(null, { status: 204 }))); + fetch.mockResponseOnce(() => Promise.resolve(new Response(undefined, { status: 204 }))); + fetch.mockResponseOnce('done'); + + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe(''); + expect(await request()).toBe('done'); + }); }); describe('testing mockResponses', () => { @@ -822,15 +822,15 @@ describe('overloads', () => { expect(await request()).toBe('i'); }); }); - + it('works globally', async () => { - const fm = createFetchMock(vi); - fm.enableMocks(); + const fm = createFetchMock(vi); + fm.enableMocks(); - fetchMock.mockResponseOnce('foo'); - expect(await request()).toBe('foo'); + fetchMock.mockResponseOnce('foo'); + expect(await request()).toBe('foo'); - fm.disableMocks(); + fm.disableMocks(); }); it('enable/disable', async () => { From 3e03ca49d6a993b9edcce1730b208c2d22dab61c Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Thu, 7 Nov 2024 07:50:13 -0500 Subject: [PATCH 2/3] Lint tests --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6977bad..186b8f9 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,8 @@ "type-check": "tsc", "test": "vitest run", "test:watch": "vitest run --watch", - "lint": "eslint src", - "lint:fix": "eslint src --fix", + "lint": "eslint src tests", + "lint:fix": "eslint src tests --fix", "format": "prettier src tests --write", "format:check": "prettier src tests --check", "build": "tsc -p tsconfig.build.json" From 401511884ece2b79ca410b5483333f9811cf89a0 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Thu, 7 Nov 2024 08:27:51 -0500 Subject: [PATCH 3/3] Support relative urls --- src/index.ts | 31 +++++++++++++++++++++++++++++-- tests/api.test.ts | 8 ++++++++ tsconfig.json | 3 ++- vitest.config.ts | 3 +++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 10e15ac..8bfff2b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -309,6 +309,33 @@ function requestNotMatches(request: Request, urlOrPredicate: UrlOrPredicate): bo return !requestMatches(request, urlOrPredicate); } +// Node 18 does not support URL.canParse() +export function canParseURL(url: string): boolean { + try { + new URL(url); + return true; + } catch (err) { + return false; + } +} + +// Node Requests cannot be relative +function resolveInput(input: string): string { + if (canParseURL(input)) return input; + + // Window context + if (typeof window.document !== 'undefined') { + return new URL(input, window.document.baseURI).toString(); + } + + // Worker context + if (typeof location !== 'undefined') { + return new URL(input, location.origin).toString(); + } + + return input; +} + function normalizeRequest(input: RequestInput, requestInit?: RequestInit): Request { if (input instanceof Request) { if (input.signal && input.signal.aborted) { @@ -319,12 +346,12 @@ function normalizeRequest(input: RequestInput, requestInit?: RequestInit): Reque if (requestInit && requestInit.signal && requestInit.signal.aborted) { abort(); } - return new Request(input, requestInit); + return new Request(resolveInput(input), requestInit); } else { if (requestInit && requestInit.signal && requestInit.signal.aborted) { abort(); } - return new Request(input.toString(), requestInit); + return new Request(resolveInput(input.toString()), requestInit); } } diff --git a/tests/api.test.ts b/tests/api.test.ts index 23d816a..9517547 100644 --- a/tests/api.test.ts +++ b/tests/api.test.ts @@ -100,6 +100,14 @@ describe('testing mockResponse', () => { expect(fetch.mock.calls[0]![0]).toEqual(new URL('https://instagram.com')); }); + it('should support relative request urls', async () => { + fetch.mockResponseOnce(JSON.stringify({ data: 'abcde' }), { status: 200 }); + + const response = await fetch('folder/file.json').then((res) => res.json()); + + expect(response).toEqual({ data: 'abcde' }); + }); + it('should allow empty response bodies', async () => { fetch.mockResponseOnce(null, { status: 204 }); fetch.mockResponseOnce(undefined, { status: 204 }); diff --git a/tsconfig.json b/tsconfig.json index 51e76b6..1537b89 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,8 @@ "module": "NodeNext", "moduleResolution": "NodeNext", "lib": [ - "es2022" + "es2022", + "dom", ], "baseUrl": "./", "paths": { diff --git a/vitest.config.ts b/vitest.config.ts index 75f855e..58a9b1e 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -6,4 +6,7 @@ export default defineConfig({ 'vitest-fetch-mock': './src/index', }, }, + test: { + environment: 'jsdom', + }, });