Skip to content

Commit 17f9f68

Browse files
authored
fix: make fetchMock globally available again (#29)
Fixes #28, a regression that was introduced in 820015b. Although I think this it's a bad idea to just install stuff in the global scope, let's keep this to remain backwards compatible for now. We could remove this in a future version and add a migration instruction to use rely on locally installed instances instead.
1 parent 6b4ba2e commit 17f9f68

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import { vi as vitest } from 'vitest';
22
import type { Mock } from '@vitest/spy';
33

4-
// type-definitions
4+
declare global {
5+
// eslint-disable-next-line no-var
6+
var fetchMock: FetchMock;
7+
8+
// eslint-disable-next-line @typescript-eslint/no-namespace
9+
namespace NodeJS {
10+
interface Global {
11+
fetchMock: FetchMock;
12+
}
13+
}
14+
}
15+
516
export type FetchMock = Mock<typeof global.fetch> & FetchMockObject;
617

718
class FetchMockObject {
@@ -16,6 +27,7 @@ class FetchMockObject {
1627
// enable/disable
1728
enableMocks(): FetchMock {
1829
globalThis.fetch = this.mockedFetch;
30+
globalThis.fetchMock = this.chainingResultProvider();
1931
return this.chainingResultProvider();
2032
}
2133

@@ -333,7 +345,7 @@ export default function createFetchMock(vi: typeof vitest): FetchMock {
333345
}) as FetchMock;
334346

335347
const fetchMock: FetchMock = mockedFetch as FetchMock;
336-
const fetchMockObject = new FetchMockObject(mockedFetch, globalThis.fetch, () => fetchMock);
348+
const fetchMockObject = new FetchMockObject(mockedFetch, originalFetch, () => fetchMock);
337349

338350
copyMethods(fetchMockObject, fetchMock);
339351

tests/api.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,16 @@ describe('conditional mocking', () => {
747747
});
748748
});
749749

750+
it('works globally', async () => {
751+
const fm = createFetchMock(vi);
752+
fm.enableMocks();
753+
754+
fetchMock.mockResponseOnce('foo');
755+
expect(await request()).toBe('foo');
756+
757+
fm.disableMocks();
758+
});
759+
750760
it('enable/disable', async () => {
751761
expect(vi.isMockFunction(globalThis.fetch)).toBe(false);
752762
const fetch = createFetchMock(vi);

types/test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import setupFm, { type MockResponse } from 'vitest-fetch-mock';
22
import { vi } from 'vitest';
33

4-
const fetchMock = setupFm(vi);
4+
const fm = setupFm(vi);
55

66
fetchMock.mockResponse(JSON.stringify({foo: "bar"}));
77
fetchMock.mockResponse(JSON.stringify({foo: "bar"}), {
@@ -108,10 +108,10 @@ function someSyncStringHandler(): string {
108108
return JSON.stringify({foo: "bar"});
109109
}
110110

111-
fetchMock.enableMocks();
112-
fetchMock.disableMocks();
113-
fetchMock.doMock();
114-
fetchMock.dontMock();
115-
fetchMock.doMockOnce();
116-
fetchMock.dontMockOnce();
117-
fetchMock.mockOnce();
111+
fm.enableMocks();
112+
fm.disableMocks();
113+
fm.doMock();
114+
fm.dontMock();
115+
fm.doMockOnce();
116+
fm.dontMockOnce();
117+
fm.mockOnce();

0 commit comments

Comments
 (0)