Is there any way to reset mocks with

Is there any way to reset mocks with fetchMock? The docs say "Deactivated by default and reset before running each test file.", but I'm seeing mocks carry over between describe blocks within a single test file and it'd be useful to just reset between the describe blocks in an afterAll or afterEach.
1 Reply
ericmatthys.
ericmatthys.OP5mo ago
Basically, mocks set up within a nested describe can affect other describes in the test file. import { resetMockAgent } from 'cloudflare:mock-agent'; seems to be what I am looking for... Follow up to ^^ is there any way to import resetMockAgent without a TS import error?
// @ts-expect-error
import { resetMockAgent } from 'cloudflare:mock-agent';
import { fetchMock } from 'cloudflare:test';
import { afterEach, beforeEach } from 'vitest';

beforeEach(() => {
fetchMock.activate();
fetchMock.disableNetConnect();
});

afterEach(() => {
fetchMock.assertNoPendingInterceptors();
resetMockAgent(fetchMock);
});
// @ts-expect-error
import { resetMockAgent } from 'cloudflare:mock-agent';
import { fetchMock } from 'cloudflare:test';
import { afterEach, beforeEach } from 'vitest';

beforeEach(() => {
fetchMock.activate();
fetchMock.disableNetConnect();
});

afterEach(() => {
fetchMock.assertNoPendingInterceptors();
resetMockAgent(fetchMock);
});
Want results from more Discord servers?
Add your server