Skip to content

Commit 44736b8

Browse files
committed
test: refine test so it runs in node too
1 parent bfaa5f3 commit 44736b8

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
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.bind(globalThis)(...args), name);
167+
this.route(
168+
// @ts-expect-error
169+
matcher,
170+
// @ts-expect-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.bind(globalThis)(...args));
175+
// @ts-expect-error
176+
this.catch(({ args }) => boundFetch(...args));
171177
}
172178

173179
return this;

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,13 @@ describe('mock and spy', () => {
135135
method: 'post',
136136
});
137137
});
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-
}
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://a.com/';
144+
await expect(fm.fetchHandler(testUrl)).resolves.toBeInstanceOf(Response);
145+
});
145146
});
146147
});

0 commit comments

Comments
 (0)