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

expect(jest.fn().mock.calls)toStrictEqual([]) fails #7929

Closed
lpinca opened this issue Feb 18, 2019 · 12 comments · Fixed by #13960
Closed

expect(jest.fn().mock.calls)toStrictEqual([]) fails #7929

lpinca opened this issue Feb 18, 2019 · 12 comments · Fixed by #13960
Labels

Comments

@lpinca
Copy link

lpinca commented Feb 18, 2019

🐛 Bug Report

The following expectation

expect(jest.fn().mock.calls).toStrictEqual([])

fails

To Reproduce

$ cat jest.config.js 
'use strict';

module.exports = {
  testEnvironment: 'node'
};
luigi@imac:mock$ cat test.js 
'use strict';

const fn = jest.fn();

test('`jest.fn().mock.calls` is an `Array`', function() {
  expect(Array.isArray(fn.mock.calls)).toBe(true);
  expect([]).toStrictEqual([]);
  expect(fn.mock.calls).toStrictEqual([]);
});
luigi@imac:mock$ npx jest
 FAIL  ./test.js
  ✕ `jest.fn().mock.calls` is an `Array` (5ms)

  ● `jest.fn().mock.calls` is an `Array`

    expect(received).toStrictEqual(expected)

    Difference:

    Compared values have no visual difference.

       6 |   expect(Array.isArray(fn.mock.calls)).toBe(true);
       7 |   expect([]).toStrictEqual([]);
    >  8 |   expect(fn.mock.calls).toStrictEqual([]);
         |                         ^
       9 | });
      10 | 

      at Object.toStrictEqual (test.js:8:25)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.374s, estimated 1s
Ran all test suites.

Expected behavior

Test passes.

Link to repl or repo (highly encouraged)

Please provide either a repl.it demo or a minimal repository on GitHub.

Issues without a reproduction link are likely to stall.

Run npx envinfo --preset jest

Paste the results here:

npx: installed 1 in 1.068s

  System:
    OS: macOS 10.14.3
    CPU: (16) x64 Intel(R) Xeon(R) W-2140B CPU @ 3.20GHz
  Binaries:
    Node: 11.10.0 - /usr/local/bin/node
    npm: 6.7.0 - /usr/local/bin/npm
@jeysal
Copy link
Contributor

jeysal commented Feb 18, 2019

Just checked, indeed [].__proto__ !== jest.fn().mock.calls.__proto__, so I would guess this is probably another symptom of #2549

@lpinca
Copy link
Author

lpinca commented Feb 18, 2019

Forgot to mention that this was working as intended in jest@<24.

@SimenB
Copy link
Member

SimenB commented Feb 18, 2019

I agree it's a bug, but it's recommended to not rely on the internal structure of the mocks. If you want to verify the mock hasn't been called, you can do expect(jest.fn()).not.toHaveBeenCalled().

Just checked, indeed [].__proto__ !== jest.fn().mock.calls.__proto__, so I would guess this is probably another symptom of #2549

Equality checks shouldn't care about the prototype... So I'd say this is an issue with toStrictEqual, and not jest.fn().mock

@jeysal
Copy link
Contributor

jeysal commented Feb 18, 2019

Equality checks shouldn't care about the prototype

Sorry, my statement was inaccurate - toStrictEqual does not care about the .__proto__, but the .constructor.
And [].constructor !== jest.fn().mock.calls.constructor, even though it seems like jest-mock initializes calls with just [], so it might be #2549 after all?

@lpinca
Copy link
Author

lpinca commented Feb 19, 2019

@SimenB I'm actually using it in place of

expect(fn).toHaveBeenCalledTimes(number);
expect(fn).toHaveBeenNthCalledWith(1, arg1, agr2);
expect(fn).toHaveBeenNthCalledWith(2, arg1, agr2);

so I can either use that or create a new array from mock.calls.

expect(Array.from(fn.mock.calls)).toStrictEqual([
  [arg1, arg2],
  [arg1, arg2]
]);

Both are ok but I think that the documentation should mention that toStrictEqual() fails when used against mockFn.mock.calls.

@SimenB
Copy link
Member

SimenB commented Feb 19, 2019

It's definitely a bug that should be fixed 🙂

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@spudly
Copy link

spudly commented Feb 26, 2022

Please keep this open. I would love to see this get fixed

@SimenB
Copy link
Member

SimenB commented Feb 26, 2022

PR welcome! 😀

@github-actions github-actions bot removed the Stale label Feb 26, 2022
@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 26, 2023
@ibuibu
Copy link
Contributor

ibuibu commented Feb 27, 2023

I have one idea that I am not too sure about and I will try to create a PR.

@github-actions
Copy link

github-actions bot commented Apr 6, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants