Jest: expect(jest.fn().mock.calls)toStrictEqual([]) fails

Created on 18 Feb 2019  ยท  6Comments  ยท  Source: facebook/jest

๐Ÿ› 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
Bug

Most helpful comment

It's definitely a bug that should be fixed ๐Ÿ™‚

All 6 comments

Just checked, indeed [].__proto__ !== jest.fn().mock.calls.__proto__, so I would guess this is probably another symptom of https://github.com/facebook/jest/issues/2549

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

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

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?

@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.

It's definitely a bug that should be fixed ๐Ÿ™‚

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephenlautier picture stephenlautier  ยท  3Comments

samzhang111 picture samzhang111  ยท  3Comments

StephanBijzitter picture StephanBijzitter  ยท  3Comments

rosiakr picture rosiakr  ยท  3Comments

paularmstrong picture paularmstrong  ยท  3Comments