Do you want to request a feature or report a bug?
Feature :)
What is the current behavior?
As discussed in https://github.com/facebook/jest/issues/2972#issuecomment-301743045, currently Jest offers:
clearMocks : clear any mocked calls but leave the mock implementationresetMocks: remove fake impl but don't restore the originalThese 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.
Closing in favor of the PR.
@cpojer There is currently no config like restoreMocks: true. Is it something that can be added? Happy to open a PR.
@tothandras I would suggest you to check this out https://github.com/JoinColony/jest-sandbox
as a possible temporary solution
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.
@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.
@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
Any news on getting a config option for restoreMocks: true?
PR welcome :)
Reopening as the original request was both the programmatic api and config, only the api was added.
Taking a shot at this :)
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.
@gricard From the docs:
Beware that
jest.restoreAllMocks()only works when mock was created withjest.spyOn; other mocks will require you to manually restore them.
@SimenB Oh, right. I had read that too. Thanks!