From 3648db50dc58410ba8e6c9a68af351db4ad830dc Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Fri, 30 Aug 2024 18:29:46 +0100 Subject: [PATCH] test: jest correctly scaffolded, mosts tests pass --- .../jest/src/__tests__/extensions.spec.js | 4 +- .../jest/src/__tests__/reset-methods.spec.js | 44 ++++++++----------- packages/jest/src/index.ts | 16 +++---- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/packages/jest/src/__tests__/extensions.spec.js b/packages/jest/src/__tests__/extensions.spec.js index 076cbc49..63f03498 100644 --- a/packages/jest/src/__tests__/extensions.spec.js +++ b/packages/jest/src/__tests__/extensions.spec.js @@ -1,6 +1,8 @@ import { describe, it, beforeAll, afterAll, expect } from '@jest/globals'; -import fetchMock from '../index'; +import fetchMockModule from '../index'; +const fetchMock = fetchMockModule.default; + describe('expect extensions', () => { [ 'Fetched', diff --git a/packages/jest/src/__tests__/reset-methods.spec.js b/packages/jest/src/__tests__/reset-methods.spec.js index d667f52d..ca9c3c2a 100644 --- a/packages/jest/src/__tests__/reset-methods.spec.js +++ b/packages/jest/src/__tests__/reset-methods.spec.js @@ -5,10 +5,12 @@ import { afterAll, expect, afterEach, - vi, -} from 'vitest'; + jest, +} from '@jest/globals'; -import fetchMock, { manageFetchMockGlobally } from '../index'; +import fetchMockModule, { manageFetchMockGlobally } from '../index'; + +const fetchMock = fetchMockModule.default; describe('reset methods', () => { describe('new fetch-mock methods', () => { @@ -98,14 +100,14 @@ describe('reset methods', () => { describe('manageFetchMockGlobally', () => { const originalMethods = {}; beforeAll(() => { - // cannot use vi.spyOn as that is part of the functionality we are + // cannot use jest.spyOn as that is part of the functionality we are // aiming to test! originalMethods.mockClear = fetchMock.mockClear; - fetchMock.mockClear = vi.fn(); + fetchMock.mockClear = jest.fn(); originalMethods.mockReset = fetchMock.mockReset; - fetchMock.mockReset = vi.fn(); + fetchMock.mockReset = jest.fn(); originalMethods.mockRestore = fetchMock.mockRestore; - fetchMock.mockRestore = vi.fn(); + fetchMock.mockRestore = jest.fn(); }); afterEach(() => { fetchMock.mockClear.mockClear(); @@ -116,40 +118,32 @@ describe('reset methods', () => { afterAll(() => { Object.assign(fetchMock, originalMethods); }); - it('by default does not hook into vitest global mock management', () => { - vi.clearAllMocks(); + it('by default does not hook into jesttest global mock management', () => { + jest.clearAllMocks(); expect(fetchMock.mockClear).not.toHaveBeenCalled(); - vi.resetAllMocks(); + jest.resetAllMocks(); expect(fetchMock.mockReset).not.toHaveBeenCalled(); - vi.restoreAllMocks(); - expect(fetchMock.mockRestore).not.toHaveBeenCalled(); - vi.unstubAllGlobals(); + jest.restoreAllMocks(); expect(fetchMock.mockRestore).not.toHaveBeenCalled(); }); describe('when enabled', () => { beforeAll(() => { manageFetchMockGlobally(); }); - it('vi.clearAllMocks() calls .mockClear()', () => { - vi.clearAllMocks(); + it('jest.clearAllMocks() calls .mockClear()', () => { + jest.clearAllMocks(); expect(fetchMock.mockClear).toHaveBeenCalled(); expect(fetchMock.mockReset).not.toHaveBeenCalled(); expect(fetchMock.mockRestore).not.toHaveBeenCalled(); }); - it('vi.resetAllMocks() calls .mockReset()', () => { - vi.resetAllMocks(); + it('jest.resetAllMocks() calls .mockReset()', () => { + jest.resetAllMocks(); expect(fetchMock.mockClear).not.toHaveBeenCalled(); expect(fetchMock.mockReset).toHaveBeenCalled(); expect(fetchMock.mockRestore).not.toHaveBeenCalled(); }); - it('vi.restoreAllMocks() calls .mockRestore()', () => { - vi.restoreAllMocks(); - expect(fetchMock.mockClear).not.toHaveBeenCalled(); - expect(fetchMock.mockReset).not.toHaveBeenCalled(); - expect(fetchMock.mockRestore).toHaveBeenCalled(); - }); - it('vi.unstubAllGlobals() calls .mockRestore()', () => { - vi.unstubAllGlobals(); + it('jest.restoreAllMocks() calls .mockRestore()', () => { + jest.restoreAllMocks(); expect(fetchMock.mockClear).not.toHaveBeenCalled(); expect(fetchMock.mockReset).not.toHaveBeenCalled(); expect(fetchMock.mockRestore).toHaveBeenCalled(); diff --git a/packages/jest/src/index.ts b/packages/jest/src/index.ts index 0376f285..e16da785 100644 --- a/packages/jest/src/index.ts +++ b/packages/jest/src/index.ts @@ -10,7 +10,7 @@ type MockResetOptions = { includeSticky: boolean; }; -class FetchMockVitest extends FetchMock { +class FetchMockJest extends FetchMock { mockClear() { this.clearHistory(); return this; @@ -29,31 +29,31 @@ class FetchMockVitest extends FetchMock { } export function manageFetchMockGlobally() { - const { clearAllMocks, resetAllMocks, restoreAllMocks } = - jest; + const { clearAllMocks, resetAllMocks, restoreAllMocks } = jest; jest.clearAllMocks = () => { + console.log('yeah') clearAllMocks.apply(jest); - fetchMockVitest.mockClear(); + fetchMockJest.mockClear(); return jest; }; jest.resetAllMocks = () => { resetAllMocks.apply(jest); - fetchMockVitest.mockReset(); + fetchMockJest.mockReset(); return jest; }; jest.restoreAllMocks = () => { restoreAllMocks.apply(jest); - fetchMockVitest.mockRestore(); + fetchMockJest.mockRestore(); return jest; }; } -const fetchMockVitest = new FetchMockVitest({ +const fetchMockJest = new FetchMockJest({ ...defaultFetchMockConfig, }); -export default fetchMockVitest; +export default fetchMockJest;