Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oleksandrva opened this issue Aug 8, 2023 · 6 comments · May be fixed by #675
Open

Comments

@oleksandrva
Copy link

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

@keeganwitt
Copy link
Collaborator

keeganwitt commented Aug 10, 2023

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

@keeganwitt
Copy link
Collaborator

This is working for me

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

@keeganwitt
Copy link
Collaborator

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

@eabrouwer3
Copy link

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

@aarowman
Copy link

aarowman commented Nov 7, 2023

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
Copy link

aarowman commented Nov 7, 2023

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

@forivall forivall linked a pull request Jan 26, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants