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

Add restoreMocks config option #3580

Closed
jackfranklin opened this issue May 16, 2017 · 13 comments · May be fixed by manekinekko/devoxx-assistant#3
Closed

Add restoreMocks config option #3580

jackfranklin opened this issue May 16, 2017 · 13 comments · May be fixed by manekinekko/devoxx-assistant#3

Comments

@jackfranklin
Copy link
Contributor

Do you want to request a feature or report a bug?
Feature :)

What is the current behavior?

As discussed in #2972 (comment), currently Jest offers:

  • clearMocks : clear any mocked calls but leave the mock implementation
  • resetMocks: remove fake impl but don't restore the original

These align to the jest.clearAllMocks and jest.resetAllMocks functions that in turn call mockFn.mockClear() and mockFn.mockReset.

There is also mockFn.mockRestore which clears out the fake implementations and restores the original. This is the behaviour that we'd like to have for all mocks that's automatically run between each test.

Therefore I'd like to propose a new function jest.restoreAllMocks which will call mockRestore on each mock. In addition I'd like to add a config option restoreMocks: false (by default) that will do this automatically.

This would also mean jest.spyOn with restoreMocks: true would be very close to Jasmine's spyOn behaviour.

@cpojer
Copy link
Member

cpojer commented Jul 17, 2017

Closing in favor of the PR.

@tothandras
Copy link

@cpojer There is currently no config like restoreMocks: true. Is it something that can be added? Happy to open a PR.

@sergeysolovev
Copy link

@tothandras I would suggest you to check this out https://github.com/JoinColony/jest-sandbox
as a possible temporary solution

@tothandras
Copy link

tothandras commented Oct 9, 2017

Thanks @sergeysolovev! I've used that once, but the experience is not the same as mocha+sinon. 😞 With mocha beforeEach, afterEach could be used to set up a sandbox (this.sandbox) for each test case, had to be written down only once in the test setup.

@sergeysolovev
Copy link

@tothandras I agree, these frameworks have quite rich and sophisticated API, letting you do whatever you would like to. That' why I've started using jest together with sinon first. Later I found out, that I could manage everything only using jest. It's not that impossible, actually. The method jest.spyOn is really helpful for me. Here is an example on how I handle missing restoreMocks:

https://github.com/sergeysolovev/swader/blob/master/src/containers/Resource.test.js

Maybe it's not that pretty, but it's not that ugly actually. I wish it could give you some fresh ideas.

@sergeysolovev
Copy link

@tothandras Oh yes, I forgot to mention, that big players like airbnb and paypal (not sure about the latter though) are switching to jest not for no reason :) The performance really worth it

@nunofgs
Copy link

nunofgs commented Nov 17, 2017

Any news on getting a config option for restoreMocks: true?

@SimenB
Copy link
Member

SimenB commented Nov 21, 2017

PR welcome :)

Reopening as the original request was both the programmatic api and config, only the api was added.

@SimenB SimenB reopened this Nov 21, 2017
@gricard
Copy link
Contributor

gricard commented Jan 15, 2018

Taking a shot at this :)

@gricard
Copy link
Contributor

gricard commented Jan 16, 2018

I basically did what was there already for clearMocks & resetMocks config options.

Branch in my fork is here: https://github.com/gricard/jest/tree/gricard-restoremocks

Re-reading docs. Still working on getting the integration tests to pass.

@SimenB
Copy link
Member

SimenB commented Jan 16, 2018

@gricard From the docs:

Beware that jest.restoreAllMocks() only works when mock was created with jest.spyOn; other mocks will require you to manually restore them.

@gricard
Copy link
Contributor

gricard commented Jan 16, 2018

@SimenB Oh, right. I had read that too. Thanks!

@github-actions
Copy link

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 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants