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.

jest.clearAllMocks(); does not remove mock implementation within `afterEach`

See original GitHub issue

🐛 Bug Report

jest.clearAllMocks(); does not remove mock implementation within afterEach

To Reproduce

I have a file called src/layouts/index.js

// ./src/layouts/index.js
const importAll = (r) =>
    r.keys().reduce(
        (acc, key) => ({
            ...acc,
            [key.replace(/^\.\/(.*).json$/, '$1')]: r(key)
        }),
        {}
    );

module.exports = importAll(require.context('./', true, /\.json$/));

It utilizes webpack require.context so I am trying to mock with jest.mock.

I have another file… say file util.js

//./src/util.js
import layouts from '../layouts';
export const getLayout(name) {
  return layouts[name];
}

in my test I’m trying to clear the mocks after each test

//./src/util.test.js
describe('my test suite', () => {
  afterEach(() => {
     jest.clearAllMocks();
  })
  test('test number one', () => {
      jest.mock('./layouts', () => ({
          layout1 : { a : 1 },
          layout2 : { b: 2 },
     }));
     assert.equals(getLayout('layout1').a, 1);
     assert.equals(getLayout('layout2').b, 2);
  });
  test('test number two', () => {
     assert.equals(getLayout('layout1').a, 1);
     assert.equals(getLayout('layout2').b, 2);
  });
});

Expected behavior

I would expect for the first test to pass and the second test to fail… because the mock should have been cleared.

Link to repl or repo (highly encouraged)

https://repl.it/@CharlieHoover/SorrowfulBackSandboxes

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:100 (3 by maintainers)

github_iconTop GitHub Comments

221reactions
cphoovercommented, Oct 10, 2018

FYI The mocking documentation and API is extremely unclear, and overly complicated IMHO.

148reactions
damien-rochecommented, Jan 3, 2020

I think the default config should include:

{
  restoreMocks: true,
  clearMocks: true,
  resetMocks: true
} 

It is shocking that the default behaviour is to vomit state between tests. Can you please just keep my tests isolated by default? Then the [hopeful minority] who want to spread state across multiple tests can do so by opt-in.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why isn't jest's clearAllMocks working and how do I clear ...
This should do the job : const getFirebaseMock = jest.spyOn(ReduxFirebase, "getFirebase"). ... You can try afterEach(() => { jest.
Read more >
Jest set, clear and reset mock/spy/stub implementation
This post goes through how to set, reset and clear mocks, stubs and spies in Jest using techniques such as the beforeEach hook...
Read more >
jest.clearAllMocks vs jest.resetAllMocks ... - DEV Community ‍ ‍
clearAllMocks. Clear all mock usage data such as mock.calls , mock.instances , mock.contexts and mock.results but not their implementation.
Read more >
Mock Functions - Jest
If no implementation is given, the mock function will return ... mockClear() will replace mockFn.mock , not just reset the values of its ......
Read more >
jest.clearAllMocks JavaScript and Node.js code examples
How to use. clearAllMocks. function. in. jest ... index.test.js/afterEach ... it('should call recordmanager correctly for objectlikes', () => {.
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