Compare commits

...
Author SHA1 Message Date
Andrew DatsenkoandFacebook GitHub Bot 2ed261d3ed [skip ci] Add .toBeCalledWith and .toHaveBeenCalledWith (#49177)
Summary:

Changelog: [Internal]
Add missing jest expect apis

Reviewed By: rubennorte

Differential Revision: D69123826
2025-02-04 11:09:33 -08:00
Andrew DatsenkoandFacebook GitHub Bot 61d6bca1d4 [skip ci] Add .toStrictEqual (#49176)
Summary:

Changelog: [Internal]
Add missing jest expect apis

Reviewed By: rubennorte

Differential Revision: D69123117
2025-02-04 11:09:33 -08:00
2 changed files with 117 additions and 0 deletions
+44
View File
@@ -15,6 +15,9 @@ import deepEqual from 'deep-equal';
import {diff} from 'jest-diff';
import {format, plugins} from 'pretty-format';
const stringify = (data: mixed): string =>
format(data, {min: true, plugins: [plugins.ReactElement]});
class ErrorWithCustomBlame extends Error {
// Initially 5 to ignore all the frames from Babel helpers to instantiate this
// custom error class.
@@ -89,6 +92,31 @@ class Expect {
}
}
toStrictEqual(expected: mixed): void {
let expectedType: mixed =
typeof expected === 'object' && expected !== null
? Object.getPrototypeOf(expected)
: null;
let receivedType: mixed =
typeof this.#received === 'object' && this.#received !== null
? Object.getPrototypeOf(this.#received)
: null;
const pass =
deepEqual(this.#received, expected, {strict: true}) &&
expectedType === receivedType;
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected${this.#maybeNotLabel()} to strictly equal:\n${
diff(expected, this.#received, {
contextLines: 1,
expand: false,
omitAnnotationLines: true,
}) ?? 'Failed to compare outputs'
}`,
).blameToPreviousFrame();
}
}
toBe(expected: mixed): void {
const pass = this.#received === expected;
if (!this.#isExpectedResult(pass)) {
@@ -230,6 +258,22 @@ class Expect {
}
}
toBeCalledWith(...args: mixed[]): void {
return this.toHaveBeenCalledWith(...args);
}
toHaveBeenCalledWith(...args: mixed[]): void {
const mock = this.#requireMock();
const pass = mock.calls.some(callArgs =>
deepEqual(callArgs, args, {strict: true}),
);
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to have been called with ${stringify(args)}, but it was called with ${stringify(mock.calls)}`,
).blameToPreviousFrame();
}
}
toBeGreaterThan(expected: number): void {
if (typeof this.#received !== 'number') {
throw new ErrorWithCustomBlame(
@@ -111,6 +111,38 @@ describe('expect', () => {
}).toThrow();
});
test('toStrictEqual', () => {
class LaCroix {
flavor: string;
constructor(flavor: string) {
this.flavor = flavor;
}
}
expect({a: undefined, b: 2}).not.toStrictEqual({b: 2});
expect([2, undefined]).not.toStrictEqual([2]);
expect([2]).not.toStrictEqual([2, undefined]);
// This is part of spec https://jestjs.io/docs/expect#tostrictequalvalue
// eslint-disable-next-line no-sparse-arrays
expect([, 2]).not.toStrictEqual([undefined, 2]);
expect(new LaCroix('lemon')).not.toStrictEqual({flavor: 'lemon'});
expect({a: 1}).toStrictEqual({a: 1});
expect([2, undefined]).toStrictEqual([2, undefined]);
// This is part of spec https://jestjs.io/docs/expect#tostrictequalvalue
// eslint-disable-next-line no-sparse-arrays
expect([, 1]).toStrictEqual([, 1]);
expect(new LaCroix('lemon')).toStrictEqual(new LaCroix('lemon'));
expect(() => {
expect(new LaCroix('lemon')).toStrictEqual({flavor: 'lemon'});
}).toThrow();
expect(() => {
expect(new LaCroix('lemon')).not.toStrictEqual(new LaCroix('lemon'));
}).toThrow();
});
test('toBeInstanceOf', () => {
class Class {}
@@ -285,6 +317,47 @@ describe('expect', () => {
}),
);
['toBeCalledWith', 'toHaveBeenCalledWith'].map(toHaveBeenCalledWithAlias =>
test(toHaveBeenCalledWithAlias, () => {
const fn = jest.fn();
expect(fn).not[toHaveBeenCalledWithAlias]();
expect(() => {
expect(fn)[toHaveBeenCalledWithAlias]();
}).toThrow();
fn('happy');
fn({a: 1}, 2);
fn(['fantom'], {isAwesome: true});
expect(fn)[toHaveBeenCalledWithAlias]('happy');
expect(fn)[toHaveBeenCalledWithAlias]({a: 1}, 2);
expect(fn)[toHaveBeenCalledWithAlias](['fantom'], {isAwesome: true});
expect(fn).not[toHaveBeenCalledWithAlias]();
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1});
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1}, 2, null);
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1, b: 2}, 2);
expect(() => {
expect(fn).not[toHaveBeenCalledWithAlias]({a: 1}, 2);
}).toThrow();
expect(() => {
expect(fn)[toHaveBeenCalledWithAlias](1);
}).toThrow();
// Passing functions that aren't mocks should always fail
expect(() => {
expect(() => {})[toHaveBeenCalledWithAlias]();
}).toThrow();
expect(() => {
expect(() => {}).not[toHaveBeenCalledWithAlias]();
}).toThrow();
}),
);
describe('jest.fn()', () => {
it('tracks execution of functions without implementations', () => {
const fn = jest.fn();