invocationCallOrder counter doesn't / can't be reset between tests
See original GitHub issue🐛 Bug Report
invocationCallOrder counter doesn’t / can’t be reset between tests
To Reproduce
Steps to reproduce the behavior: Run two tests against two mock functions, running .mockClear on each
const a = jest.fn();
const b = jest.fn();
beforeEach(() => {
a.mockClear();
b.mockClear();
});
test('Test 1', () => {
a();
b();
expect(a.mock.invocationCallOrder).toEqual([1]);
expect(b.mock.invocationCallOrder).toEqual([2]);
});
test('Test 2', () => {
a();
b();
expect(a.mock.invocationCallOrder).toEqual([1]); // Actual value [3]
expect(b.mock.invocationCallOrder).toEqual([2]); // Actual value [4]
});
Expected behavior
I’d expect the invocationCallOrder in the 2nd test to be [1] and [2].
It seems like the mock state is reset but not the counter, this means the tests will only have the expected callOrders if they’re the first tests run in the file. It also means you can’t run a single test as the values will be different.
Run npx envinfo --preset jest
Paste the results here:
System:
OS: macOS High Sierra 10.13.6
CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Binaries:
Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
npm: 6.4.1 - ~/.nvm/versions/node/v8.9.4/bin/npm
npmPackages:
jest: ^23.6.0 => 23.6.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
How to reset Jest mock functions calls count before every test
One way I found to handle it: to clear mock function after each test: To add to Sum.test.js: afterEach(() => { local.getData.
Read more >types/jest/index.d.ts - UNPKG
186, * Resets the module registry - the cache of all required modules. ... modules for every test so that local module state...
Read more >How to automatically reset mocks and restore spies in Jest
When I used jest for the first time for unit testing, it struck me that function mocks and spies were not automatically reset...
Read more >A guide to module mocking with Jest - Emma Goto
Clearing mocks between tests with clearAllMocks. If we declare the mock once, its call count doesn't reset between tests. So the second test...
Read more >Mock Functions - Jest
Mock functions allow you to test the links between code by erasing the actual implementation of a function, capturing calls to the function ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Simple solution I found to check call order :
to assert that a was called before b.
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.