Skip to content

Commit bfaa5f3

Browse files
committed
fix: fix failure to spy in browsers
1 parent cb429dd commit bfaa5f3

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

packages/fetch-mock/src/FetchMock.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ export class FetchMock {
164164
): FetchMock {
165165
if (matcher) {
166166
//@ts-expect-error TODO findo out how to overload an overload
167-
this.route(matcher, ({ args }) => this.config.fetch(...args), name);
167+
this.route(matcher, ({ args }) => this.config.fetch.bind(globalThis)(...args), name);
168168
} else {
169169
//@ts-expect-error TODO findo out how to overload an overload
170-
this.catch(({ args }) => this.config.fetch(...args));
170+
this.catch(({ args }) => this.config.fetch.bind(globalThis)(...args));
171171
}
172172

173173
return this;

packages/fetch-mock/src/__tests__/FetchMock/mock-and-spy.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ describe('mock and spy', () => {
135135
method: 'post',
136136
});
137137
});
138-
139-
it('can call actual native fetch without erroring', async () => {
140-
fm.spyGlobal();
141-
await expect(
142-
fm.fetchHandler('http://example.com/'),
143-
).resolves.toBeInstanceOf(Response);
144-
});
138+
const isBrowser = Boolean(globalThis.location);
139+
if (isBrowser) {
140+
it('can call actual native fetch without erroring', async () => {
141+
fm.spyGlobal();
142+
await expect(fm.fetchHandler('/')).resolves.toBeInstanceOf(Response);
143+
});
144+
}
145145
});
146146
});

0 commit comments

Comments
 (0)