Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): clean up jest.Mock usage in tests #13238

Merged
merged 2 commits into from Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise the default is jest.Mock<any, any>.

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)),
Comment on lines -21 to -23
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.fn() does nothing here. Can be simplified.

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();
});
Comment on lines +27 to +30
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tricky one. If I got it right: fake was being reset manually, looks like jest.clearAllMocks() does the same job; also require in beforeEach above has no effect without jest.resetModules() because of module caching. Or?


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);
});