Skip to content

Commit

Permalink
fix: hooks into jest's global mock management functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 30, 2024
1 parent 3648db5 commit aaadda0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
11 changes: 6 additions & 5 deletions packages/jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ npm i -D @fetch-mock/jest

```js
import fetchMock, { manageFetchMockGlobally } from '@fetch-mock/jest';
import { jest } from '@jest/glonals';

manageFetchMockGlobally(); // optional
manageFetchMockGlobally(jest); // optional
```

## API
Expand All @@ -48,13 +49,13 @@ Clears all call history from the mocked `fetch` implementation _and_ removes all

Calls `mockReset()` and additionally restores global fetch to its unmocked implementation.

### manageFetchMockGlobally()
### manageFetchMockGlobally(jest)

Hooks fetchMock up to jest's global mock management so that

- `vi.clearAllMocks()` will call `fetchMock.mockClear()`
- `vi.resetAllMocks()` will call `fetchMock.mockReset()`
- `vi.restoreAllMocks()` will call `fetchMock.mockRestore()`
- `jest.clearAllMocks()` will call `fetchMock.mockClear()`
- `jest.resetAllMocks()` will call `fetchMock.mockReset()`
- `jest.restoreAllMocks()` will call `fetchMock.mockRestore()`

Note that these **will not** clear any sticky routes added to fetchMock. You will need to make an additional call to `fetchMock.removeRoutes({includeSticky: true})`.

Expand Down
5 changes: 2 additions & 3 deletions packages/jest/src/__tests__/reset-methods.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetchMockModule, { manageFetchMockGlobally } from '../index';
import {
describe,
it,
Expand All @@ -8,8 +9,6 @@ import {
jest,
} from '@jest/globals';

import fetchMockModule, { manageFetchMockGlobally } from '../index';

const fetchMock = fetchMockModule.default;

describe('reset methods', () => {
Expand Down Expand Up @@ -128,7 +127,7 @@ describe('reset methods', () => {
});
describe('when enabled', () => {
beforeAll(() => {
manageFetchMockGlobally();
manageFetchMockGlobally(jest);
});
it('jest.clearAllMocks() calls .mockClear()', () => {
jest.clearAllMocks();
Expand Down
4 changes: 2 additions & 2 deletions packages/jest/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { jest } from '@jest/globals';
import {
FetchMock,
defaultFetchMockConfig,
RemoveRouteOptions,
} from '@fetch-mock/core';
import './jest-extensions';
import type { Jest } from '@jest/environment';

type MockResetOptions = {
includeSticky: boolean;
Expand All @@ -28,7 +28,7 @@ class FetchMockJest extends FetchMock {
}
}

export function manageFetchMockGlobally() {
export function manageFetchMockGlobally(jest: Jest) {
const { clearAllMocks, resetAllMocks, restoreAllMocks } = jest;

jest.clearAllMocks = () => {
Expand Down

0 comments on commit aaadda0

Please sign in to comment.