Skip to content

Commit

Permalink
test: jest correctly scaffolded, mosts tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 30, 2024
1 parent d6940bc commit 3648db5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
4 changes: 3 additions & 1 deletion packages/jest/src/__tests__/extensions.spec.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
44 changes: 19 additions & 25 deletions packages/jest/src/__tests__/reset-methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions packages/jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type MockResetOptions = {
includeSticky: boolean;
};

class FetchMockVitest extends FetchMock {
class FetchMockJest extends FetchMock {
mockClear() {
this.clearHistory();
return this;
Expand All @@ -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;

0 comments on commit 3648db5

Please sign in to comment.