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

toHaveBeenCalledOnceWith incorrectly compares Maps #589

Open
beebs93 opened this issue Apr 11, 2023 · 1 comment
Open

toHaveBeenCalledOnceWith incorrectly compares Maps #589

beebs93 opened this issue Apr 11, 2023 · 1 comment

Comments

@beebs93
Copy link

beebs93 commented Apr 11, 2023

Bug

  • package version: 3.2.4
  • node version: 16.10.0
  • npm (or yarn) version: yarn 1.22.19

Relevant code or config
Most likely this line making the comparison:

const pass = invokedOnce && this.equals(expected, actual);

What you did:

  1. I checked out this package and added the following to the toHaveBeenCalledOnceWith.test.js file
    test('passes if comparing two empty `Map`s', () => {
      mock(new Map());
      expect(mock).toHaveBeenCalledOnceWith(new Map());
    });
    
    test('passes if comparing two non-empty `Map`s', () => {
      const testMap = new Map([
        ['key1', 'value1'],
        ['key2', 'value2'],
      ]);
      mock(testMap);
      expect(mock).toHaveBeenCalledOnceWith(testMap);
    });
    
    test('fails if comparing two `Map`s with different entries', () => {
      const testMap1 = new Map([
        ['key1', 'value1'],
        ['key2', 'value2'],
      ]);
      const testMap2 = new Map([
        ['key3', 'value3'],
        ['key4', 'value4'],
      ]);
      mock(testMap1);
      expect(() => expect(mock).toHaveBeenCalledOnceWith(testMap2)).toThrowErrorMatchingSnapshot();
    });
  2. You can also reproduce this quickly via https://jest-extended.jestcommunity.dev by pasting the following into the Playground text area:
    test('toHaveBeenCalledOnceWith with Maps', () => {
      const testMap1 = new Map([
        ['key1', 'value1'],
        ['key2', 'value2'],
      ]);
      const testMap2 = new Map([
        ['key3', 'value3'],
        ['key4', 'value4'],
      ]);
      const myMock = jest.fn();
      myMock(testMap1);
    
      expect(myMock).toHaveBeenCalledOnceWith(testMap1); // This passess as expected
      expect(myMock).toHaveBeenCalledOnceWith(testMap2); // This passes, but it should fail
      expect(myMock).toHaveBeenCalledWith(testMap2); // This fails as jest's native `toHaveBeenCalledWith` implementation detects the mismatch
    });

What happened (please provide anything you think will help):
The toHaveBeenCalledWith matcher cannot detect Maps with differently entries while jest's native toHaveBeenCalledWith does.

Reproduction repository (if possible):
N/A

@keeganwitt
Copy link
Collaborator

The equals function is defined in Jest itself here. Jest's toHaveBeenCalledWith also uses this function. I'm trying to understand why there seems to be a difference in behavior.

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

No branches or pull requests

2 participants