CodePark/ questions / Mocks, spies & timers
START PRACTICINGPRACTICE
Frontend question · Testing › Mocks, spies & timers

A test silences errors with jest.spyOn(console, 'error').mockImplementation(() => {}). Which call in afterEach puts the real console.error back?

jest.restoreAllMocks()

jest.restoreAllMocks() puts the real console.error back, because it restores spied methods to their originals. The tempting resetAllMocks is wrong. It removes mock implementations, but the method is still a mock. clearAllMocks only resets recorded calls and results. resetModules clears the module registry, Jest's cache of loaded modules. It has nothing to do with spies.