Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make fetchMock globally available again #29

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Loading