jest-extended
jest-extended copied to clipboard
`toHaveBeenCalledExactlyOnceWith` expects an array of objects, when the object is passed as `args`
Bug
packageversion:4.0.1nodeversion:18.16.1npmversion:9.5.1
test('should render ProfileAvatar with props', async () => {
render(<Header adminUsername={'John Doe'} onLogout={onLogoutMock} />);
await waitFor(async () =>
expect(ProfileAvatar).toHaveBeenCalledExactlyOnceWith({
adminUsername: 'John Doe',
onLogout: onLogoutMock,
}),
);
});
What you did:
After the update to version 4.0.1 tests that check the call of the components with params are failing with this kind of error:
This was working with 4.0.0? I'm thinking #633 broke this.
This is working for me
test('toHaveBeenCalledExactlyOnceWith with object', () => {
const mock = jest.fn();
mock({ foo: 'bar' });
expect(mock).toHaveBeenCalledExactlyOnceWith({ foo: 'bar' });
});
Do you have a simple example I can reproduce this with?
I think the problem is with the error message. Here's an example of what I'm seeing:
it('github issue', () => {
const mock = jest.fn();
mock('hello', 'world', '!');
expect(mock).toHaveBeenCalledExactlyOnceWith('hello', 'world');
});
This throws an error message (which is expected) that looks like this:
Expected mock function to have been called exactly once with ["hello", "world"], but it was called with "hello"
I would expect it to show all of the args it was called with (like toHaveBeenCalledWith does):
Expected: "hello", "world"
Received: "hello", "world", "!"
I think it's just the error message that's confusing, but @oleksandrva lmk if I'm wrong in that assumption.
On a side note, toHaveBeenCalledExactlyOnceWith does not work on the jestcommunity.dev website, but I don't know who runs that. https://jest-extended.jestcommunity.dev/docs/matchers/Mock
I just ran into this too. It looks like it's asserting correctly (on the full ...args array), but the error message prints incorrectly - only shows the first value of the array.
Note that the expected value is printed correctly. The issue is just printing the actual.
I think it's because it calls printReceived(...actual) instead of printReceived(actual). It correctly calls printReceived(expected)
I also like that the toHaveBeenCalledWith is cleaner (less verbose) as @eabrouwer3 mentioned.
Note: here is the test which uses the related snapshot that shows this same issue.
Expected mock function to have been called exactly once with <green>["hello"]</color>, but it was called with <red>"not hello"</color>"
the "not hello" should be ["not hello"].
I'd like to see this test use more than 1 arg to highlight the logic (e.g. ("hello", "world")) as @eabrouwer3's example