From bfaa5f33c133af17a0bd097d2d3dbcb01966a0a8 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Tue, 28 Jan 2025 16:29:38 +0000 Subject: [PATCH] fix: fix failure to spy in browsers --- packages/fetch-mock/src/FetchMock.ts | 4 ++-- .../src/__tests__/FetchMock/mock-and-spy.test.js | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/fetch-mock/src/FetchMock.ts b/packages/fetch-mock/src/FetchMock.ts index aad608c0..6f6db09f 100644 --- a/packages/fetch-mock/src/FetchMock.ts +++ b/packages/fetch-mock/src/FetchMock.ts @@ -164,10 +164,10 @@ export class FetchMock { ): FetchMock { if (matcher) { //@ts-expect-error TODO findo out how to overload an overload - this.route(matcher, ({ args }) => this.config.fetch(...args), name); + this.route(matcher, ({ args }) => this.config.fetch.bind(globalThis)(...args), name); } else { //@ts-expect-error TODO findo out how to overload an overload - this.catch(({ args }) => this.config.fetch(...args)); + this.catch(({ args }) => this.config.fetch.bind(globalThis)(...args)); } return this; diff --git a/packages/fetch-mock/src/__tests__/FetchMock/mock-and-spy.test.js b/packages/fetch-mock/src/__tests__/FetchMock/mock-and-spy.test.js index 7e891600..4a7701b6 100644 --- a/packages/fetch-mock/src/__tests__/FetchMock/mock-and-spy.test.js +++ b/packages/fetch-mock/src/__tests__/FetchMock/mock-and-spy.test.js @@ -135,12 +135,12 @@ describe('mock and spy', () => { method: 'post', }); }); - - it('can call actual native fetch without erroring', async () => { - fm.spyGlobal(); - await expect( - fm.fetchHandler('http://example.com/'), - ).resolves.toBeInstanceOf(Response); - }); + const isBrowser = Boolean(globalThis.location); + if (isBrowser) { + it('can call actual native fetch without erroring', async () => { + fm.spyGlobal(); + await expect(fm.fetchHandler('/')).resolves.toBeInstanceOf(Response); + }); + } }); });