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)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:100 (3 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
FYI The mocking documentation and API is extremely unclear, and overly complicated IMHO.
I think the default config should include:
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.