jest-community/jest-extended

`toHaveBeenCalledOnceWith` incorrectly compares `Map`s

beebs93 opened this issue · 1 comments

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

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.