From aaadda01fe77df773c4e6e9ddc5cf45c1dc8981b Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Fri, 30 Aug 2024 18:37:53 +0100 Subject: [PATCH] fix: hooks into jest's global mock management functions --- packages/jest/README.md | 11 ++++++----- packages/jest/src/__tests__/reset-methods.spec.js | 5 ++--- packages/jest/src/index.ts | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/jest/README.md b/packages/jest/README.md index 37b7300b..87d680dd 100644 --- a/packages/jest/README.md +++ b/packages/jest/README.md @@ -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 @@ -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})`. diff --git a/packages/jest/src/__tests__/reset-methods.spec.js b/packages/jest/src/__tests__/reset-methods.spec.js index ca9c3c2a..9f182cc0 100644 --- a/packages/jest/src/__tests__/reset-methods.spec.js +++ b/packages/jest/src/__tests__/reset-methods.spec.js @@ -1,3 +1,4 @@ +import fetchMockModule, { manageFetchMockGlobally } from '../index'; import { describe, it, @@ -8,8 +9,6 @@ import { jest, } from '@jest/globals'; -import fetchMockModule, { manageFetchMockGlobally } from '../index'; - const fetchMock = fetchMockModule.default; describe('reset methods', () => { @@ -128,7 +127,7 @@ describe('reset methods', () => { }); describe('when enabled', () => { beforeAll(() => { - manageFetchMockGlobally(); + manageFetchMockGlobally(jest); }); it('jest.clearAllMocks() calls .mockClear()', () => { jest.clearAllMocks(); diff --git a/packages/jest/src/index.ts b/packages/jest/src/index.ts index e16da785..77f0b75a 100644 --- a/packages/jest/src/index.ts +++ b/packages/jest/src/index.ts @@ -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; @@ -28,7 +28,7 @@ class FetchMockJest extends FetchMock { } } -export function manageFetchMockGlobally() { +export function manageFetchMockGlobally(jest: Jest) { const { clearAllMocks, resetAllMocks, restoreAllMocks } = jest; jest.clearAllMocks = () => {