jest-extended icon indicating copy to clipboard operation
jest-extended copied to clipboard

`toHaveBeenCalledExactlyOnceWith` expects an array of objects, when the object is passed as `args`

Open oleksandrva opened this issue 2 years ago • 7 comments

Bug

  • package version: 4.0.1
  • node version: 18.16.1
  • npm version: 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: image

oleksandrva avatar Aug 08 '23 07:08 oleksandrva

This was working with 4.0.0? I'm thinking #633 broke this.

keeganwitt avatar Aug 10 '23 15:08 keeganwitt

This is working for me

test('toHaveBeenCalledExactlyOnceWith with object', () => {
    const mock = jest.fn();
    mock({ foo: 'bar' });
    expect(mock).toHaveBeenCalledExactlyOnceWith({ foo: 'bar' });
});

keeganwitt avatar Aug 10 '23 23:08 keeganwitt

Do you have a simple example I can reproduce this with?

keeganwitt avatar Aug 16 '23 15:08 keeganwitt

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

eabrouwer3 avatar Oct 20 '23 19:10 eabrouwer3

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.

aarowman avatar Nov 07 '23 23:11 aarowman

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

aarowman avatar Nov 07 '23 23:11 aarowman