Skip to content

Commit

Permalink
fix: fix failure to spy in browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Jan 28, 2025
1 parent cb429dd commit bfaa5f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/fetch-mock/src/FetchMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions packages/fetch-mock/src/__tests__/FetchMock/mock-and-spy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
});
});

0 comments on commit bfaa5f3

Please sign in to comment.