question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
hugeflowercommented, Aug 11, 2020

Simple solution I found to check call order :

    expect(a.mock.invocationCallOrder[0]).toBeLessThan(b.mock.invocationCallOrder[0])

to assert that a was called before b.

0reactions
github-actions[bot]commented, Apr 30, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found