The following expectation
expect(jest.fn().mock.calls).toStrictEqual([])
fails
$ 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.
Test passes.
Please provide either a repl.it demo or a minimal repository on GitHub.
Issues without a reproduction link are likely to stall.
npx envinfo --preset jestPaste 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
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 ๐
Most helpful comment
It's definitely a bug that should be fixed ๐