Skip to content

Commit

Permalink
Add .toBeCalledWith and .toHaveBeenCalledWith (facebook#49177)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#49177

Changelog: [Internal]
Add missing jest expect apis

Reviewed By: rubennorte

Differential Revision: D69123826

fbshipit-source-id: 53c550970d1cd2434f52a400ad18c8611f187176
  • Loading branch information
andrewdacenko authored and facebook-github-bot committed Feb 5, 2025
1 parent 3033aaa commit b30a5f8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/react-native-fantom/runtime/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -249,6 +252,21 @@ class Expect {
}
}

toBeCalledWith: (...args: Array<mixed>) => void;
toHaveBeenCalledWith(...args: Array<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(
Expand Down Expand Up @@ -446,6 +464,8 @@ class Expect {
Expect.prototype.toBeCalled = Expect.prototype.toHaveBeenCalled;
// $FlowExpectedError[method-unbinding]
Expect.prototype.toBeCalledTimes = Expect.prototype.toHaveBeenCalledTimes;
// $FlowExpectedError[method-unbinding]
Expect.prototype.toBeCalledWith = Expect.prototype.toHaveBeenCalledWith;

const expect: mixed => Expect = (received: mixed) => new Expect(received);

Expand Down
41 changes: 41 additions & 0 deletions packages/react-native-fantom/src/__tests__/expect-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,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();
Expand Down

0 comments on commit b30a5f8

Please sign in to comment.