-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Make restoreMocks, resetMocks, clearMocks default #10242
Comments
Not sure |
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.
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
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 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. |
I expected them to be Jest defaults when it emerged, at least clearMocks and restoreMocks. But now it seems a bit late to enable them. I agree this would be "super breaking". My suggestion is to make a soft transition, officially recommend clearMocks is a good thing that prevents tests from contaminating each other. This will break tests that were already badly written. restoreMocks makes mocks in
It may be good but only for old-fashioned style with mandatory beforeEach (beforeAll wasn't initially available in Jasmine):
As for default resetMocks, it's worse. A big show stopper is that resetMocks ruins
|
For reasons |
I agree with making |
It would be really fantastic if |
so are you saying @vcarl that |
That was 6 months ago, I don't recall the specifics of the situation anymore |
Anyway, implementations done inside mocks should not be reset under any circumstance. I fear there are a lot of seriously broken tests out there because of all of this strange default mock leaking behaviour of jest. |
Maintaining mock state between tests is a footgun. So making @SimenB are you still in favour of changing this? |
Yes, this is a very breaking change, but I personally believe there is a fundamental issue here that warrants the change. IMO, it is far too easy to write buggy tests that give a false assurance of working and tested software, due to subtle mock leakage between tests. Ever had to modify unit tests and have to spend hours debugging why things broke when you made some chages, only to discover something was leaking, and a change of order or the existence of a new test was the cause? I have worked on dozens of codebases in the past several years and had this problem. It's a nightmare to debug and I wouldn't expect a trusted tool such as Jest to let me do slightly wild stuff like that, by default. I want to have high confidence that my tests are passing because they're testing what I expected it to, using the input and mocks I expected it to use. Even if my test code is more verbose and explicit. I hope @SimenB and @mrazauskas will consider this change once again |
This change needs to happen to fix all the broken jest tests out there. The fallout may be massive but people currently don't know their tests, and by that, their code, is potentially broken. +1 for changing the defaults. Should increase major version for that to stay true to semver though. |
Is there any further plan for this issue? It has been opened awhile |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
The issue is still relevant. |
This issue is still relevant. |
🚀 Feature Proposal
The
restoreMocks
,resetMocks
, andclearMocks
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 addjest.restoreAllMocks()
.I noticed there are config options, so then I moved them to our jest config. (Per #7068,
restoreAllMocks()
does not alsoresetAllMocks()
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.
The text was updated successfully, but these errors were encountered: