Skip to content

Commit

Permalink
fix: make fetchMock globally available again (#29)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dirkluijk authored Oct 28, 2024
1 parent 6b4ba2e commit 17f9f68
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { vi as vitest } from 'vitest';
import type { Mock } from '@vitest/spy';

// type-definitions
declare global {
// eslint-disable-next-line no-var
var fetchMock: FetchMock;

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS {
interface Global {
fetchMock: FetchMock;
}
}
}

export type FetchMock = Mock<typeof global.fetch> & FetchMockObject;

class FetchMockObject {
Expand All @@ -16,6 +27,7 @@ class FetchMockObject {
// enable/disable
enableMocks(): FetchMock {
globalThis.fetch = this.mockedFetch;
globalThis.fetchMock = this.chainingResultProvider();
return this.chainingResultProvider();
}

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

const fetchMock: FetchMock = mockedFetch as FetchMock;
const fetchMockObject = new FetchMockObject(mockedFetch, globalThis.fetch, () => fetchMock);
const fetchMockObject = new FetchMockObject(mockedFetch, originalFetch, () => fetchMock);

copyMethods(fetchMockObject, fetchMock);

Expand Down
10 changes: 10 additions & 0 deletions tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,16 @@ describe('conditional mocking', () => {
});
});

it('works globally', async () => {
const fm = createFetchMock(vi);
fm.enableMocks();

fetchMock.mockResponseOnce('foo');
expect(await request()).toBe('foo');

fm.disableMocks();
});

it('enable/disable', async () => {
expect(vi.isMockFunction(globalThis.fetch)).toBe(false);
const fetch = createFetchMock(vi);
Expand Down
16 changes: 8 additions & 8 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import setupFm, { type MockResponse } from 'vitest-fetch-mock';
import { vi } from 'vitest';

const fetchMock = setupFm(vi);
const fm = setupFm(vi);

fetchMock.mockResponse(JSON.stringify({foo: "bar"}));
fetchMock.mockResponse(JSON.stringify({foo: "bar"}), {
Expand Down Expand Up @@ -108,10 +108,10 @@ function someSyncStringHandler(): string {
return JSON.stringify({foo: "bar"});
}

fetchMock.enableMocks();
fetchMock.disableMocks();
fetchMock.doMock();
fetchMock.dontMock();
fetchMock.doMockOnce();
fetchMock.dontMockOnce();
fetchMock.mockOnce();
fm.enableMocks();
fm.disableMocks();
fm.doMock();
fm.dontMock();
fm.doMockOnce();
fm.dontMockOnce();
fm.mockOnce();

0 comments on commit 17f9f68

Please sign in to comment.