Skip to content
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

Set resetMocks to true by default #9047

Closed
EmmanuelDemey opened this issue Oct 12, 2019 · 7 comments
Closed

Set resetMocks to true by default #9047

EmmanuelDemey opened this issue Oct 12, 2019 · 7 comments

Comments

@EmmanuelDemey
Copy link

EmmanuelDemey commented Oct 12, 2019

🚀 Feature Proposal

As described in the thread https://twitter.com/kentcdodds/status/1182744387190607872, we should set resetMocks param to true by default.

Motivation

In order to make our tests isolated.

Example

thanks to this new default value, we won't have to define afterAll method in our unit tests, in order to reset something.

afterEach(() => {
  jest.clearAllMocks()
})

Pitch

This feature is part of the core project . https://jestjs.io/docs/en/configuration#resetmocks-boolean. This change will make de developer experience smoother when writing unit test with jest.
As writing isolated test is a good practice, we should make it easy to implement, without writing extra code.

Why does this feature belong in the Jest core platform?

Common feature proposals that do not typically make it to core:

  • New matchers (see jest-extended)
  • Changes to the default reporter (use custom reporters instead)
  • Changes to node/jsdom test environments (use custom environments instead)
@github-actions
Copy link

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 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@bozdoz
Copy link

bozdoz commented Mar 3, 2022

Note the proposal does not match the example. resetMocks is not equal to clearAllMocks

clearAllMocks clears calls, instances, and results (and this is a good thing to do!): https://jestjs.io/docs/jest-object#jestclearallmocks

resetAllMocks is what is called by setting resetMocks to true; and this does what clearAllMocks does, but also removes implementations and mocked return values (and this is not a great thing to do!): https://jestjs.io/docs/configuration#resetmocks-boolean

Justification: clearing all mocks is great, because tests should be isolated (thanks @kentcdodds). Resetting all mocks is generally bad, because you will make manual mocks useless (also see #10419), and make useless any other mock implementation that a developer may have created for a test suite (solution to the latter is to re-implement it in a beforeEach, but there shouldn't be a need to do this)

I would propose to close this ticket, and keep resetMocks default as false

@github-actions github-actions bot removed the Stale label Mar 3, 2022
@drFabio
Copy link

drFabio commented Mar 7, 2022

Is there any viable solution to restore manual mocks?

Having reset on a beforeEach makes them look like they could be only used once. Looks like with this reset clearing the manual we have no use for manual mocks that can have a different implementation

@jeppester
Copy link
Contributor

@bozdoz @drFabio
I made the following PR a long time ago, which unfortunately got stuck somewhere:
#9270

It makes each mock remember it's initial implementation and provides a way to restore the initial implementation.

I believe it would be a very viable solution to restoring manual mocks, and that it would be a safer and better way to reset the state between tests than resetAllMocks or restoreAllMocks.

Copy link

github-actions bot commented Mar 8, 2024

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.

@github-actions github-actions bot added the Stale label Mar 8, 2024
Copy link

github-actions bot commented Apr 7, 2024

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2024
Copy link

github-actions bot commented May 9, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants