"resetAllMocks" does not reset all mocks
See original GitHub issue🐛 Bug Report
resetAllMocks
does not reset mocks created with generateFromMetadata
method
To Reproduce
Here is a utility method that I’ve created to create class mocks:
import * as jestMock from 'jest-mock'
export interface IClassType<T> {
new (...args: any[]): T
}
export function mock<T>(classType: IClassType<T>): any {
const mockType = jestMock.generateFromMetadata(jestMock.getMetadata(classType))
return new mockType()
}
However when I use jest.resetAllMocks()
mocks created by this function are not reset.
Here is a code example:
import { mock } from '../../../mock-utils'
import { Test } from './Test'
export class Test {
public testMethod(param: number): boolean {
return false
}
}
describe('test mock', () => {
const testMock = mock(Test)
it('should reset mocks', () => {
testMock.testMethod(123)
testMock.testMethod.mockReset() // <--- works as expected
expect(testMock.testMethod).not.toHaveBeenCalled() // <---- passes
testMock.testMethod(123)
jest.resetAllMocks() // <--- does not reset the mock
expect(testMock.testMethod).not.toHaveBeenCalled() // <--- fails
})
})
Expected behavior
The above test should pass.
Link to repl or repo (highly encouraged)
N/A
Run npx envinfo --preset jest
Paste the results here:
System:
OS: macOS High Sierra 10.13.1
CPU: x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Binaries:
Node: 10.11.0 - /usr/local/bin/node
Yarn: 1.10.1 - /usr/local/bin/yarn
npm: 6.4.1 - ~/node_modules/.bin/npm
npmPackages:
@types/jest: ^23.1.0 => 23.3.2
jest: ^22.0.0 => 22.4.4
Issue Analytics
- State:
- Created 5 years ago
- Reactions:20
- Comments:26 (3 by maintainers)
Top Results From Across the Web
jest.clearAllMocks(); does not remove mock implementation ...
The easiest solution I saw was to reset modules and re-require them before each test. This way resetAllMocks didn't wipe out all the...
Read more >Why is my resetAllMocks not working in jest - Stack Overflow
I was experiencing the same problem until I placed jest.resetAllMocks(); inside afterEach like so: afterEach(() => { jest.
Read more >How to automatically reset mocks and restore spies in Jest
Configuring Jest to automatically reset / restore mocks and spies. As it seemed, it turned out Jest can be configured to do an...
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 >Jest mock and spy — mockClear vs mockReset vs mockRestore
When we will be updating one tests all other might start failing. If something like this is happening we most probably don't have...
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
So what worked was the following
So the
this._mockState
seems to be different betweenjest.clearAllMocks()
andjestMock.clearAllMocks
.One possible solution here would be to use
global._mockState
instead ofthis._mockState
, making it definitely the same.The other thing I found out was that the constructor of the
ModuleMockerClass
is invoked 3 times when I run this for 1 test file: Once byjest-environment-node
, byjest-environment-jsdom
and byjest-runtime
. (I found out about that by logging a stack trace in the constructor ofModuleMockerClass
.). Maybe there is a better way to fix this, given that info?@caitecoll this workaround, mentioned on #7136, worked for me: https://github.com/facebook/jest/issues/7136#issuecomment-565976599
I think this ^ should be the default jest behavior. Leaking state between tests is an anti-pattern because it means test start to rely on running in a certain order (they rely on the side effects of previous tests). This is a problem because:
-t
).IMO, clearing state between tests should be the default for these reasons and because the vast majority of projects do not require the performance benefits of not having to rebuild state before each test (and those projects that do can opt-into preserving state with config).
If it’s very hard to change these defaults due to back-compat, then at least this deserves thorough documentation and a section on how to set up this config (rather than having to do an extensive grep through issues and stack overflow to find it).