Skip to content

Commit 1dbc3c0

Browse files
authored
Merge pull request #900 from wheresrhys/rhys/issue-899
Rhys/issue 899
2 parents 7ff5915 + 0fc590a commit 1dbc3c0

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

packages/fetch-mock/src/FetchMock.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,18 @@ export class FetchMock {
162162
matcher?: RouteMatcher | UserRouteConfig,
163163
name?: RouteName,
164164
): FetchMock {
165+
const boundFetch = this.config.fetch.bind(globalThis);
165166
if (matcher) {
166-
//@ts-expect-error TODO findo out how to overload an overload
167-
this.route(matcher, ({ args }) => this.config.fetch(...args), name);
167+
this.route(
168+
// @ts-expect-error related to the overloading of .route()
169+
matcher,
170+
// @ts-expect-error this is just args from a fetch call being passed into a bound fetch - no idea why the error
171+
({ args }) => boundFetch(...args),
172+
name,
173+
);
168174
} else {
169-
//@ts-expect-error TODO findo out how to overload an overload
170-
this.catch(({ args }) => this.config.fetch(...args));
175+
// @ts-expect-error this is just args from a fetch call being passed into a bound fetch - no idea why the error
176+
this.catch(({ args }) => boundFetch(...args));
171177
}
172178

173179
return this;

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

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('mock and spy', () => {
5959
describe('.spy()', () => {
6060
testChainableMethod('spy');
6161
testChainableMethod('spyGlobal');
62+
6263
it('passes all requests through to the network by default', async () => {
6364
vi.spyOn(fm.config, 'fetch');
6465
fm.spy();
@@ -134,5 +135,13 @@ describe('mock and spy', () => {
134135
method: 'post',
135136
});
136137
});
138+
139+
it('can call actual native fetch without erroring', async () => {
140+
fm.spyGlobal();
141+
const isBrowser = Boolean(globalThis.location);
142+
// avoids getting caught by a cors error
143+
const testUrl = isBrowser ? '/' : 'http://example.com/';
144+
await expect(fm.fetchHandler(testUrl)).resolves.toBeInstanceOf(Response);
145+
});
137146
});
138147
});

0 commit comments

Comments
 (0)