Add .nthCalledWith and .toHaveBeenNthCalledWith (#49221)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/49221

Changelog: [Internal]
Add missing jest expect apis

Reviewed By: cortinico

Differential Revision: D69133125

fbshipit-source-id: fe4e54cbb3646c154108cee6ead9a64c3e75c1b7
This commit is contained in:
Andrew Datsenko
2025-02-06 08:02:32 -08:00
committed by Facebook GitHub Bot
parent 5506441df9
commit 1d5cdf10fc
2 changed files with 70 additions and 0 deletions
+23
View File
@@ -288,6 +288,27 @@ class Expect {
}
}
nthCalledWith: (index: number, ...args: mixed[]) => void;
toHaveBeenNthCalledWith(index: number, ...args: mixed[]): void {
if (index < 1) {
throw new ErrorWithCustomBlame(
`Expected index to be positive number, got ${index}.`,
).blameToPreviousFrame();
}
const mock = this.#requireMock();
if (this.#isNot && mock.calls.length < index) {
return;
}
const pass = deepEqual(mock.calls[index - 1], args, {strict: true});
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to have been nth(${index}) called with ${stringify(args)}, but it was called with ${stringify(mock.calls[index - 1])}.`,
);
}
}
toBeGreaterThan(expected: number): void {
if (typeof this.#received !== 'number') {
throw new ErrorWithCustomBlame(
@@ -489,6 +510,8 @@ Expect.prototype.toBeCalledTimes = Expect.prototype.toHaveBeenCalledTimes;
Expect.prototype.toBeCalledWith = Expect.prototype.toHaveBeenCalledWith;
// $FlowExpectedError[method-unbinding]
Expect.prototype.lastCalledWith = Expect.prototype.toHaveBeenLastCalledWith;
// $FlowExpectedError[method-unbinding]
Expect.prototype.nthCalledWith = Expect.prototype.toHaveBeenNthCalledWith;
const expect: mixed => Expect = (received: mixed) => new Expect(received);
@@ -405,6 +405,53 @@ describe('expect', () => {
}),
);
['nthCalledWith', 'toHaveBeenNthCalledWith'].map(
toHaveBeenNthCalledWithAlias =>
test(toHaveBeenNthCalledWithAlias, () => {
const fn = jest.fn();
expect(fn).not[toHaveBeenNthCalledWithAlias](1);
expect(fn).not[toHaveBeenNthCalledWithAlias](1, {});
expect(() => {
expect(fn)[toHaveBeenNthCalledWithAlias](0);
}).toThrow();
expect(() => {
expect(fn)[toHaveBeenNthCalledWithAlias](1);
}).toThrow();
fn('happy');
fn();
fn({a: 1}, 2);
expect(fn)[toHaveBeenNthCalledWithAlias](1, 'happy');
expect(fn)[toHaveBeenNthCalledWithAlias](2);
expect(fn)[toHaveBeenNthCalledWithAlias](3, {a: 1}, 2);
expect(fn).not[toHaveBeenNthCalledWithAlias](1);
expect(fn).not[toHaveBeenNthCalledWithAlias](3, {a: 1});
expect(fn).not[toHaveBeenNthCalledWithAlias](3, {a: 2}, 2);
expect(fn).not[toHaveBeenNthCalledWithAlias](3, {a: 1}, 2, undefined);
expect(() => {
expect(fn).not[toHaveBeenNthCalledWithAlias](3, {a: 1}, 2);
}).toThrow();
expect(() => {
expect(fn)[toHaveBeenNthCalledWithAlias](1);
}).toThrow();
// Passing functions that aren't mocks should always fail
expect(() => {
expect(() => {})[toHaveBeenNthCalledWithAlias](1);
}).toThrow();
expect(() => {
expect(() => {}).not[toHaveBeenNthCalledWithAlias](1);
}).toThrow();
}),
);
describe('jest.fn()', () => {
it('tracks execution of functions without implementations', () => {
const fn = jest.fn();