jest: "resetAllMocks" does not reset all mocks
๐ 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
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 20
- Comments: 26 (3 by maintainers)
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).
After playing with this topic for a bit, it seems like calling
jestMock.clearAllMocks()
will work on those mocks.It seems like the file is required multiple times (within
jest-runtime
,jest-env-jsdom
andjest-env-node
) and the require cache is (probably on purpose) not always respected.Iโm not sure how to continue, possibly by attaching the mock state to
global
?Apologies to @maumercado, I didnโt mean to steal this from you, hope this info could help you solve it.
@johannes-scharlach Iโm not sure I follow - can you post a sample of what you tested?
@maumercado feel free to take a look as well!