Skip to content

Commit

Permalink
fix(tests): clean up jest.Mock usage in tests (#13238)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Sep 10, 2022
1 parent 0b0a54f commit 0aa9b02
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
Expand Up @@ -182,7 +182,7 @@ Expected: not <g>"foo"</>, <g>undefined</>
Number of calls: <r>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`] = `
<d>expect(</><r>jest.fn()</><d>).</>not<d>.</>lastCalledWith<d>(</><g>...expected</><d>)</>

Expected: not <g>"foo"</>, <g>optionalFn<></>
Expand Down Expand Up @@ -549,7 +549,7 @@ Expected: not <g>"foo"</>, <g>undefined</>
Number of calls: <r>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`] = `
<d>expect(</><r>jest.fn()</><d>).</>not<d>.</>nthCalledWith<d>(</>n<d>, </><g>...expected</><d>)</>

n: 1
Expand Down Expand Up @@ -1197,7 +1197,7 @@ Expected: not <g>"foo"</>, <g>undefined</>
Number of calls: <r>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`] = `
<d>expect(</><r>jest.fn()</><d>).</>not<d>.</>toBeCalledWith<d>(</><g>...expected</><d>)</>

Expected: not <g>"foo"</>, <g>optionalFn<></>
Expand Down Expand Up @@ -1584,7 +1584,7 @@ Expected: not <g>"foo"</>, <g>undefined</>
Number of calls: <r>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`] = `
<d>expect(</><r>jest.fn()</><d>).</>not<d>.</>toHaveBeenCalledWith<d>(</><g>...expected</><d>)</>

Expected: not <g>"foo"</>, <g>optionalFn<></>
Expand Down Expand Up @@ -1775,7 +1775,7 @@ Expected: not <g>"foo"</>, <g>undefined</>
Number of calls: <r>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`] = `
<d>expect(</><r>jest.fn()</><d>).</>not<d>.</>toHaveBeenLastCalledWith<d>(</><g>...expected</><d>)</>

Expected: not <g>"foo"</>, <g>optionalFn<></>
Expand Down Expand Up @@ -1999,7 +1999,7 @@ Expected: not <g>"foo"</>, <g>undefined</>
Number of calls: <r>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`] = `
<d>expect(</><r>jest.fn()</><d>).</>not<d>.</>toHaveBeenNthCalledWith<d>(</>n<d>, </><g>...expected</><d>)</>

n: 1
Expand Down
34 changes: 20 additions & 14 deletions packages/expect/src/__tests__/spyMatchers.test.ts
Expand Up @@ -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 () {};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<number, [value: number]> = jest.fn(value => {
if (value === 0) {
// Before returning from the base case of recursion, none of the
// calls have returned yet.
Expand Down Expand Up @@ -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<number, [value: number]> = jest.fn(value => {
if (value === 0) {
return 0;
} else {
Expand Down Expand Up @@ -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<string, []>();
fn.mockReturnValueOnce('foo1');
fn.mockReturnValueOnce('foo2');
fn.mockReturnValueOnce('foo3');
Expand All @@ -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<number, [value: number]> = 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),
Expand All @@ -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<string, []>();
fn.mockReturnValueOnce('foo1');
fn.mockReturnValueOnce('foo2');
fn.mockReturnValueOnce('foo3');
Expand All @@ -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<string, []>();
fn.mockReturnValueOnce('foo1');
fn.mockReturnValueOnce('foo2');
fn.mockReturnValueOnce('foo3');
Expand Down Expand Up @@ -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<string, [string]>(() => 'foo');
fn('foo');

expect(() => {
Expand All @@ -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<string, [string]>(() => 'foo');
fn('foo');

expect(() => {
Expand All @@ -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<number, [value: number]> = jest.fn(value => {
if (value === 0) {
return 0;
} else {
Expand Down Expand Up @@ -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<string, []>();
fn.mockReturnValueOnce('foo1');
fn.mockReturnValueOnce('foo2');
fn.mockReturnValueOnce('foo3');
Expand All @@ -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<number, [number]> = jest.fn(value => {
if (value === 0) {
// Before returning from the base case of recursion, none of the
// calls have returned yet.
Expand Down
Expand Up @@ -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<string, jest.Mock> = {
fancyCondition: jest.fn(path => path.length > 10),
testRegex: jest.fn(path => /.test.js$/.test(path)),
const cases: Record<string, (path: string) => boolean> = {
fancyCondition: path => path.length > 10,
testRegex: path => /.test.js$/.test(path),
};
const filter = (path: string) =>
Object.keys(cases).every(key => cases[key](path));
Expand Down Expand Up @@ -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<string>();
const resolved = dependencyResolver.resolveInverse(paths, filter);
expect(resolved.length).toEqual(0);
});
Expand Down
22 changes: 15 additions & 7 deletions packages/jest-util/src/__tests__/installCommonGlobals.test.ts
Expand Up @@ -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();

Expand All @@ -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);
});

0 comments on commit 0aa9b02

Please sign in to comment.