Skip to content

Add restoreMocks config option #3580

@jackfranklin

Description

@jackfranklin

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions