-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable clearMocks
, resetMocks
, and restoreMocks
by default
#18768
Comments
I'm ok with 1 but don't like 2 and 3. 2 and 3 are onyl for lazy people whow don't want to think about test order and side effects. |
I agree about the potential for performance penalties, but I think it's a trade-off between test correctness and performance. Already, enabling |
Looks like PR #20402 fixes something about mocking as well:
|
Describe the proposed change(s).
Jest config allows you to specify 3 flags regarding mocks in increasing order of strictness:
clearMocks: true
clears the call count and args of any mocks before each test.resetMocks: true
goes a step further, and also clears any mock implementations / return values defined before each test.restoreMocks: true
is the strictest and actually removes all mocks from all functions before each test.The default, unfortunately, is not to do any of this, which means out-of-the-box Jest makes it very easy to write unit tests that unintentionally rely on test ordering.
I think the bare minimum should be to enable
clearMocks
injest.config.ts
(and remove any explicitjest.clearAllMocks()
calls). Ideally I would also like to enableresetMocks
(and remove any explicitjest.resetAllMocks()
calls). I'd like to evaluate use ofrestoreMocks
onceresetMocks
is reached.See jestjs/jest#10242 for more context
The text was updated successfully, but these errors were encountered: