Skip to content

Commit

Permalink
test: refine test so it runs in node too
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Jan 28, 2025
1 parent bfaa5f3 commit 44736b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
14 changes: 10 additions & 4 deletions packages/fetch-mock/src/FetchMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,18 @@ export class FetchMock {
matcher?: RouteMatcher | UserRouteConfig,
name?: RouteName,
): FetchMock {
const boundFetch = this.config.fetch.bind(globalThis);
if (matcher) {
//@ts-expect-error TODO findo out how to overload an overload
this.route(matcher, ({ args }) => this.config.fetch.bind(globalThis)(...args), name);
this.route(
// @ts-expect-error
matcher,
// @ts-expect-error
({ args }) => boundFetch(...args),
name,
);
} else {
//@ts-expect-error TODO findo out how to overload an overload
this.catch(({ args }) => this.config.fetch.bind(globalThis)(...args));
// @ts-expect-error
this.catch(({ args }) => boundFetch(...args));
}

return this;
Expand Down
15 changes: 8 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,13 @@ describe('mock and spy', () => {
method: 'post',
});
});
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);
});
}

it('can call actual native fetch without erroring', async () => {
fm.spyGlobal();
const isBrowser = Boolean(globalThis.location);
// avoids getting caught by a cors error
const testUrl = isBrowser ? '/' : 'http://a.com/';
await expect(fm.fetchHandler(testUrl)).resolves.toBeInstanceOf(Response);
});
});
});

0 comments on commit 44736b8

Please sign in to comment.