skip to content

In Jest 30, what separates mockClear, mockReset and mockRestore, and which config keys run their all-mocks forms before every test?

level: middleimportance: must knowfreq 72%

answer

  1. three levels of cleanup
  2. record, then behaviour, then original
  3. only spies have an original
  4. three boolean config keys

basics

~10 s

mockClear empties the call record; mockReset also removes implementations, so the mock returns undefined; mockRestore also puts a jest.spyOn original back. clearMocks, resetMocks and restoreMocks run clearAllMocks, resetAllMocks and restoreAllMocks before each test.

solid answer

~40 s

They are three levels. `mockClear()` empties `mock.calls`, `mock.results`, `mock.instances` and `mock.contexts` but keeps every implementation, including queued `...Once` values. `mockReset()` does that and also drops the default implementation and the once queue, so the mock returns `undefined`; a reset spy stays installed but no longer calls through. `mockRestore()` does that and also puts the original back, which only means something for a `jest.spyOn` spy. The config keys `clearMocks`, `resetMocks` and `restoreMocks`, all `false` by default, run `jest.clearAllMocks()`, `jest.resetAllMocks()` and `jest.restoreAllMocks()` before every test. In Jest 30.5, `restoreAllMocks` restores spies and replaced properties but leaves plain `jest.fn()` mocks untouched, so I pair `restoreMocks` with `clearMocks`.

code

javascript · 19 lines
javascript
const logger = {
  info: message => `logged ${message}`,
};

test('clear, reset and restore on one spy', () => {
  const spy = jest.spyOn(logger, 'info').mockReturnValue('fake');
  logger.info('a');

  spy.mockClear();
  expect(spy).not.toHaveBeenCalled();
  expect(logger.info('b')).toBe('fake'); // implementation kept

  spy.mockReset();
  expect(logger.info('c')).toBeUndefined(); // implementation gone, spy still installed
  expect(jest.isMockFunction(logger.info)).toBe(true);

  spy.mockRestore();
  expect(logger.info('d')).toBe('logged d'); // original back
});

go deeper

for a junior

Remember the order: clear wipes the record, reset also wipes the behaviour, restore also puts a spied original back.

for a middle

Explain what each method keeps, why mockRestore only matters for spies, and how the three config keys map to the all-mocks functions.

for a senior

Predict what a suite does under each config key, including setup at module scope being wiped and Jest 30.5's restoreAllMocks leaving plain mocks alone.

for a principal

Choose and enforce one suite-wide combination of clearMocks, resetMocks and restoreMocks, and plan how existing tests that relied on leaked state get fixed.

## Three levels of cleanup A Jest mock function holds two kinds of state. The **record** is what happened: `mock.calls`, `mock.results`, `mock.instances` and `mock.contexts`. The **behaviour** is what it will do next: the default implementation and the queue of `...Once` implementations. A spy created with `jest.spyOn` holds a third thing, the **original** method it replaced. The three methods peel these off in layers, each doing everything the previous one does: | Method | Record | Implementations and once queue | Original method | |---|---|---|---| | `mockClear()` | emptied | kept | untouched | | `mockReset()` | emptied | removed; calls return `undefined` | untouched, spy stays installed | | `mockRestore()` | emptied | removed | put back, for `jest.spyOn` spies only | ## `mockClear` `mockClear()` is for "forget what happened, keep what you do". It is the right tool between two assertions on the same mock, or between tests that share a mock whose behaviour should persist. Two details matter: - It does **not** remove queued `...Once` values. A value that one test queued and never consumed is still waiting for the next call. - It replaces the `mock` object rather than emptying its arrays, so a variable that saved `charge.mock` earlier keeps pointing at stale data. `jest.clearAllMocks()` does the same for every mock at once, and is what the `clearMocks` key runs. ## `mockReset` `mockReset()` returns the mock to a blank `jest.fn()`. The default implementation is gone, the once queue is gone, and since Jest 30.5 any `whenCalledWith` registrations are gone too; every later call returns `undefined` until something new is configured. On a spy this is a frequent surprise: the spy is still installed on the object, but it no longer calls through, so `logger.info(...)` suddenly returns `undefined` instead of logging. ## `mockRestore` `mockRestore()` is reset plus undo. For a `jest.spyOn` spy it runs the restore function the spy recorded, so the object gets its original method back and the spy is detached. A mock created with `jest.fn()` has no original, so `mockRestore()` on it behaves exactly like `mockReset()`; assigning `obj.m = jest.fn()` and then calling `mockRestore()` leaves the now-empty fake on the object. `jest.replaceProperty` has a matching `restore()` on the object it returns. ## The config keys Each method has an all-mocks form, and each form has a config key that runs it in a hidden `beforeEach`: | Config key | Default | Runs before every test | |---|---|---| | `clearMocks` | `false` | `jest.clearAllMocks()` | | `resetMocks` | `false` | `jest.resetAllMocks()` | | `restoreMocks` | `false` | `jest.restoreAllMocks()` | Because they run **before** each test, anything configured at module scope or in `beforeAll` is already in place when they fire: 1. With `resetMocks`, a `mockResolvedValue` written at the top of the file is wiped before the first test, and the mock returns `undefined` everywhere. 2. With `restoreMocks`, a spy created at the top of the file is restored before the first test, so the tests talk to the real method. 3. The fix for both is the same: configure mocks and spies in `beforeEach` or inside the test. One Jest 30 detail is worth knowing precisely. The docs describe `jest.restoreAllMocks()` as calling `mockRestore()` on every mock, but in Jest 30.5 its source only runs the restore functions of spies and replaced properties. A quick test confirms that a plain `jest.fn()` keeps both its call record and its implementation. `restoreMocks: true` alone therefore does not clear `jest.fn()` mocks; pairing it with `clearMocks: true`, or with `resetMocks: true` when behaviour should not carry over either, covers them. ## Picking a level - Use **`mockClear`** or `clearMocks` when mocks get their behaviour once, for example in a `jest.mock` factory, and each test only needs a clean record. - Use **`mockReset`** or `resetMocks` when every test sets up its own behaviour and nothing should leak from the last one. - Use **`mockRestore`** or `restoreMocks` whenever spies touch shared objects such as module exports or `console`, so later tests see the real methods. - Mind the timing of a hand-written hook. `afterEach(() => jest.restoreAllMocks())` runs after each test rather than before, so a spy created at module scope works in the first test and silently disappears for the rest; creating spies inside each test avoids both timings.

  • Why can a test break when resetMocks is turned on for a suite that sets mockResolvedValue at the top of the file?
    `resetMocks` runs `jest.resetAllMocks()` before every test, including the first, so a default set at module scope or in `beforeAll` is removed before any test runs and the mock returns `undefined`. Move the setup into `beforeEach` or into each test, after the reset has happened.
  • After mockReset, does a jest.spyOn spy still call the original method?
    No. `mockReset` removes the call-through implementation along with everything else, but leaves the spy installed, so the method now returns `undefined`. To get the real behaviour back, either call `mockRestore()`, which detaches the spy, or give the spy a new implementation.
  • How does restoreMocks: true differ from calling jest.restoreAllMocks() in afterEach?
    Timing. The config key restores before every test, so a spy created at module scope is gone before the first test runs. The `afterEach` hook restores after each test, so that spy works in the first test and silently stops recording for the rest. Neither suits a module-scope spy: create spies in `beforeEach` or in the test, and either timing then works.

A spy is an understudy standing in for the lead actor. mockClear wipes the understudy's notebook of past performances; mockReset also takes away the lines they were given, so they stand on stage saying nothing; mockRestore sends them off and brings the lead back. A jest.fn() put on stage by hand is an understudy with no lead to call back, so restoring it just leaves them silent.

  • mockClear also removes the mockReturnValue set earlier.
  • mockReset puts the original method back on a spy.
  • mockRestore restores the original of a mock created with plain jest.fn().
  • clearMocks discards Once values that an earlier test queued but never used.
  • resetMocks runs after each test, so module-scope setup survives the first test.

Assumes Jest 30.5 or later, whose restoreAllMocks only restores spies and replaced properties and leaves plain jest.fn() mocks as they are; mockReset also clears whenCalledWith registrations, added in 30.5.