From 3248c946af3bdcb15308c3fd53683ba054e1113f Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 10 Sep 2022 08:11:51 +0300 Subject: [PATCH 1/2] fix(tests): clean up `jest.Mock` usage in tests --- .../expect/src/__tests__/spyMatchers.test.ts | 34 +++++++++++-------- .../src/__tests__/dependency_resolver.test.ts | 10 +++--- .../__tests__/installCommonGlobals.test.ts | 22 ++++++++---- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/packages/expect/src/__tests__/spyMatchers.test.ts b/packages/expect/src/__tests__/spyMatchers.test.ts index 48dc65edcc3e..669b297a63a7 100644 --- a/packages/expect/src/__tests__/spyMatchers.test.ts +++ b/packages/expect/src/__tests__/spyMatchers.test.ts @@ -12,12 +12,18 @@ import jestExpect from '../'; expect.addSnapshotSerializer(alignedAnsiStyleSerializer); jestExpect.extend({ - optionalFn(fn) { + optionalFn(fn?: unknown) { const pass = fn === undefined || typeof fn === 'function'; return {message: () => 'expect either a function or undefined', pass}; }, }); +declare module '../types' { + interface AsymmetricMatchers { + optionalFn(fn?: unknown): void; + } +} + // Given a Jest mock function, return a minimal mock of a Jasmine spy. const createSpy = (fn: jest.Mock) => { const spy = function () {}; @@ -351,7 +357,7 @@ const createSpy = (fn: jest.Mock) => { ).toThrowErrorMatchingSnapshot(); }); - test('works with trailing undefined arguments when explicitely requested as optional by matcher', () => { + test('works with trailing undefined arguments when explicitly requested as optional by matcher', () => { // issue 12463 const fn = jest.fn(); fn('foo', undefined); @@ -652,7 +658,7 @@ const createSpy = (fn: jest.Mock) => { test('incomplete recursive calls are handled properly', () => { // sums up all integers from 0 -> value, using recursion - const fn: jest.Mock = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { // Before returning from the base case of recursion, none of the // calls have returned yet. @@ -821,7 +827,7 @@ const createSpy = (fn: jest.Mock) => { test('incomplete recursive calls are handled properly', () => { // sums up all integers from 0 -> value, using recursion - const fn: jest.Mock = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { return 0; } else { @@ -1041,7 +1047,7 @@ const createSpy = (fn: jest.Mock) => { if (basicReturnedWith.indexOf(returnedWith) >= 0) { describe('returnedWith', () => { test('works with more calls than the limit', () => { - const fn = jest.fn(); + const fn = jest.fn(); fn.mockReturnValueOnce('foo1'); fn.mockReturnValueOnce('foo2'); fn.mockReturnValueOnce('foo3'); @@ -1065,12 +1071,12 @@ const createSpy = (fn: jest.Mock) => { test('incomplete recursive calls are handled properly', () => { // sums up all integers from 0 -> value, using recursion - const fn: jest.Mock = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { // Before returning from the base case of recursion, none of the // calls have returned yet. // This test ensures that the incomplete calls are not incorrectly - // interpretted as have returned undefined + // interpreted as have returned undefined jestExpect(fn).not[returnedWith](undefined); expect(() => jestExpect(fn)[returnedWith](undefined), @@ -1091,7 +1097,7 @@ const createSpy = (fn: jest.Mock) => { if (nthReturnedWith.indexOf(returnedWith) >= 0) { describe('nthReturnedWith', () => { test('works with three calls', () => { - const fn = jest.fn(); + const fn = jest.fn(); fn.mockReturnValueOnce('foo1'); fn.mockReturnValueOnce('foo2'); fn.mockReturnValueOnce('foo3'); @@ -1111,7 +1117,7 @@ const createSpy = (fn: jest.Mock) => { }); test('should replace 1st, 2nd, 3rd with first, second, third', async () => { - const fn = jest.fn(); + const fn = jest.fn(); fn.mockReturnValueOnce('foo1'); fn.mockReturnValueOnce('foo2'); fn.mockReturnValueOnce('foo3'); @@ -1153,7 +1159,7 @@ const createSpy = (fn: jest.Mock) => { }); test('positive throw matcher error for n that is not integer', async () => { - const fn: jest.Mock = jest.fn(() => 'foo'); + const fn = jest.fn(() => 'foo'); fn('foo'); expect(() => { @@ -1162,7 +1168,7 @@ const createSpy = (fn: jest.Mock) => { }); test('negative throw matcher error for n that is not number', async () => { - const fn: jest.Mock = jest.fn(() => 'foo'); + const fn = jest.fn(() => 'foo'); fn('foo'); expect(() => { @@ -1172,7 +1178,7 @@ const createSpy = (fn: jest.Mock) => { test('incomplete recursive calls are handled properly', () => { // sums up all integers from 0 -> value, using recursion - const fn: jest.Mock = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { return 0; } else { @@ -1212,7 +1218,7 @@ const createSpy = (fn: jest.Mock) => { if (lastReturnedWith.indexOf(returnedWith) >= 0) { describe('lastReturnedWith', () => { test('works with three calls', () => { - const fn = jest.fn(); + const fn = jest.fn(); fn.mockReturnValueOnce('foo1'); fn.mockReturnValueOnce('foo2'); fn.mockReturnValueOnce('foo3'); @@ -1229,7 +1235,7 @@ const createSpy = (fn: jest.Mock) => { test('incomplete recursive calls are handled properly', () => { // sums up all integers from 0 -> value, using recursion - const fn: jest.Mock = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { // Before returning from the base case of recursion, none of the // calls have returned yet. diff --git a/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts b/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts index 35b28ed79f07..5115ef184ea4 100644 --- a/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts +++ b/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts @@ -16,11 +16,11 @@ import {DependencyResolver} from '../index'; const maxWorkers = 1; let dependencyResolver: DependencyResolver; let runtimeContextResolver: Resolver; -let Runtime: typeof import('jest-runtime'); +let Runtime: typeof import('jest-runtime').default; let config: Config.ProjectConfig; -const cases: Record = { - fancyCondition: jest.fn(path => path.length > 10), - testRegex: jest.fn(path => /.test.js$/.test(path)), +const cases: Record boolean> = { + fancyCondition: path => path.length > 10, + testRegex: path => /.test.js$/.test(path), }; const filter = (path: string) => Object.keys(cases).every(key => cases[key](path)); @@ -84,7 +84,7 @@ test('resolves dependencies for scoped packages', () => { }); test('resolves no inverse dependencies for empty paths set', () => { - const paths = new Set(); + const paths = new Set(); const resolved = dependencyResolver.resolveInverse(paths, filter); expect(resolved.length).toEqual(0); }); diff --git a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts index 369cfbe2b48f..0157e578c700 100644 --- a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts +++ b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts @@ -7,21 +7,28 @@ import {createContext, runInContext} from 'vm'; +declare global { + function DTRACE_NET_SERVER_CONNECTION(): unknown; +} + +const fake = jest.fn(); +globalThis.DTRACE_NET_SERVER_CONNECTION = fake; + let installCommonGlobals: typeof import('../installCommonGlobals').default; -let fake: jest.Mock; function getGlobal(): typeof globalThis { return runInContext('this', createContext()); } beforeEach(() => { - fake = jest.fn(); - // @ts-expect-error - globalThis.DTRACE_NET_SERVER_CONNECTION = fake; - installCommonGlobals = require('../installCommonGlobals').default; }); +afterEach(() => { + jest.clearAllMocks(); + jest.resetModules(); +}); + it('returns the passed object', () => { const myGlobal = getGlobal(); @@ -33,8 +40,9 @@ it('turns a V8 global object into a Node global object', () => { expect(myGlobal.process).toBeDefined(); expect(myGlobal.DTRACE_NET_SERVER_CONNECTION).toBeDefined(); - expect(myGlobal.DTRACE_NET_SERVER_CONNECTION).not.toBe(fake); + myGlobal.DTRACE_NET_SERVER_CONNECTION(); - expect(fake.mock.calls.length).toBe(1); + + expect(fake).toHaveBeenCalledTimes(1); }); From b231e393c76a908510d33d0b567f4a2e76a89da5 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 10 Sep 2022 08:29:25 +0300 Subject: [PATCH 2/2] snap --- .../__tests__/__snapshots__/spyMatchers.test.ts.snap | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.ts.snap b/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.ts.snap index af10f62cd39a..5a0a44b6a556 100644 --- a/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.ts.snap +++ b/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.ts.snap @@ -182,7 +182,7 @@ Expected: not "foo", undefined Number of calls: 1 `; -exports[`lastCalledWith works with trailing undefined arguments when explicitely requested as optional by matcher 1`] = ` +exports[`lastCalledWith works with trailing undefined arguments when explicitly requested as optional by matcher 1`] = ` expect(jest.fn()).not.lastCalledWith(...expected) Expected: not "foo", optionalFn<> @@ -549,7 +549,7 @@ Expected: not "foo", undefined Number of calls: 1 `; -exports[`nthCalledWith works with trailing undefined arguments when explicitely requested as optional by matcher 1`] = ` +exports[`nthCalledWith works with trailing undefined arguments when explicitly requested as optional by matcher 1`] = ` expect(jest.fn()).not.nthCalledWith(n, ...expected) n: 1 @@ -1197,7 +1197,7 @@ Expected: not "foo", undefined Number of calls: 1 `; -exports[`toBeCalledWith works with trailing undefined arguments when explicitely requested as optional by matcher 1`] = ` +exports[`toBeCalledWith works with trailing undefined arguments when explicitly requested as optional by matcher 1`] = ` expect(jest.fn()).not.toBeCalledWith(...expected) Expected: not "foo", optionalFn<> @@ -1584,7 +1584,7 @@ Expected: not "foo", undefined Number of calls: 1 `; -exports[`toHaveBeenCalledWith works with trailing undefined arguments when explicitely requested as optional by matcher 1`] = ` +exports[`toHaveBeenCalledWith works with trailing undefined arguments when explicitly requested as optional by matcher 1`] = ` expect(jest.fn()).not.toHaveBeenCalledWith(...expected) Expected: not "foo", optionalFn<> @@ -1775,7 +1775,7 @@ Expected: not "foo", undefined Number of calls: 1 `; -exports[`toHaveBeenLastCalledWith works with trailing undefined arguments when explicitely requested as optional by matcher 1`] = ` +exports[`toHaveBeenLastCalledWith works with trailing undefined arguments when explicitly requested as optional by matcher 1`] = ` expect(jest.fn()).not.toHaveBeenLastCalledWith(...expected) Expected: not "foo", optionalFn<> @@ -1999,7 +1999,7 @@ Expected: not "foo", undefined Number of calls: 1 `; -exports[`toHaveBeenNthCalledWith works with trailing undefined arguments when explicitely requested as optional by matcher 1`] = ` +exports[`toHaveBeenNthCalledWith works with trailing undefined arguments when explicitly requested as optional by matcher 1`] = ` expect(jest.fn()).not.toHaveBeenNthCalledWith(n, ...expected) n: 1