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.

Make restoreMocks, resetMocks, clearMocks default

See original GitHub issue

🚀 Feature Proposal

The restoreMocks, resetMocks, and clearMocks settings should be enabled by default.

Motivation

We’ve spent a lot of time debugging tests due to mocks leaking behavior between tests. We added jest.resetAllMocks() to our test helper file a while back and that made a huge difference. But recently I discovered a lingering test spy was causing false positives in other tests that followed it. I also needed to add jest.restoreAllMocks().

I noticed there are config options, so then I moved them to our jest config. (Per #7068, restoreAllMocks() does not also resetAllMocks() as the docs suggest.)

Pitch

I think the common case would be for people to expect clean state between tests, and not having that by default might be causing a lot of confusion and false positives in tests. I’m not aware of a valid use case for not clearing/restoring all mock state after each test. If there is, it seems like it’s probably pretty rare, and could potentially be configured as needed.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:13
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
skovhuscommented, Mar 22, 2022

Maintaining mock state between tests is a footgun. So making clearMocks default to true makes a lot of sense to me – despite this being a breaking change.

@SimenB are you still in favour of changing this?

2reactions
mockdeepcommented, Jul 5, 2020

As soon as you have one test that needs to retain state

What sort of state do you need to retain between tests? If anything, this seems like an anti-pattern to me. But if it’s the sort of thing you know you’re going to need, it seems like you’re already going to have to deal with the issue of manually resetting as things are now.

mocks lose their initial implementations so you will need to do mockImplementation etc in a beforeEach hook

This actually seems like a good restriction to me. I don’t know if it is information that can be inferred, but it would be super helpful if Jest were to warn or throw an error when something like mockImplementation was called outside of a test context like it or beforeEach.

highly confused why your jest.fn().mockImplementation() is being lost.

We’ve actually seen the inverse of this a lot, so changing the default is probably a wash at worst. Right now, when somebody overrides the mockImplementation in one of the tests they can lose a lot of time trying to figure out why a change in one test is suddenly causing a lot of later tests to act strangely.

Bottom line for me is that tests should be independent of each other and not have some persistent state between them that could impact whether they pass or fail. As much as possible, the test framework should ideally be configured in such a way to strongly encourage this. I’m still not sure if there are valid cases for persisting state across tests, but if there are, it seems like that should be the longer road to step around framework defaults.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to automatically reset mocks and restore spies in Jest
Configuring Jest to automatically reset / restore mocks and spies · “clearMocks”: true : resets all the mocks usage data, but keeps the...
Read more >
Configuring Jest
clearMocks [boolean]​. Default: false. Automatically clear mock calls, instances, contexts and results before every test. Equivalent to calling jest.
Read more >
Difference between resetAllMocks, resetModules ...
restoreAllMocks() and restoreMocks:[boolean]. Similar to resetAllMocks() , with one very important difference. It restores the original ...
Read more >
jest.clearAllMocks vs jest.resetAllMocks ... - DEV Community ‍ ‍
By default, all mock function without implementation it will always return ... clearMocks: true, resetMocks: true, restoreMocks: true }.
Read more >
Configuring Jest - API Manual
jest.config.js const {defaults} = require('jest-config'); module.exports = { // ... For example, the following would create a global __DEV__ variable set to ...
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