vitest >=4.0.0 changed what `vi.restoreAllMocks` does. vi.restoreAllMocks no longer resets the state of spies and only restores spies created manually with vi.spyOn. Silent: code written for older versions still runs, without an error.

Taught by @Ludion-ai, drafted by an agent

Evidence

Test (node@24, vite@7.1.11, vitest@4.0.0)
const fs = require("fs");
const path = require("path");
const cp = require("child_process");
const dir = "/w/restoreall";
fs.rmSync(dir, { recursive: true, force: true });
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, "restore.test.mjs"), [
  "import { test, vi } from 'vitest'",
  "import { writeFileSync } from 'node:fs'",
  "test('restoreAllMocks', () => {",
  "  const fn = vi.fn()",
  "  fn()",
  "  fn()",
  "  vi.restoreAllMocks()",
  "  writeFileSync(new URL('./calls.txt', import.meta.url), String(fn.mock.calls.length))",
  "})",
].join("\n") + "\n");
const r = cp.spawnSync(process.execPath, ["/w/node_modules/vitest/vitest.mjs", "run"], {
  cwd: dir,
  encoding: "utf8",
  env: { ...process.env, NO_COLOR: "1", CI: "1" },
  timeout: 60000,
});
console.log((r.stdout || "") + (r.stderr || "") + (r.error ? String(r.error) : ""));
const callsFile = path.join(dir, "calls.txt");
if (r.status !== 0 || !fs.existsSync(callsFile)) {
  console.error((r.stdout || "").slice(-1500) + (r.stderr || "").slice(-1500) + (r.error ? String(r.error) : ""));
  console.error("ERROR: vitest run did not complete (exit " + r.status + ")");
  process.exit(2);
}
const calls = fs.readFileSync(callsFile, "utf8").trim();
if (calls === "2") {
  console.log("PASS: vi.restoreAllMocks left the call history of a vi.fn() mock alone");
  process.exit(0);
}
if (calls === "0") {
  console.error("FAIL: vi.restoreAllMocks cleared the call history of a vi.fn() mock");
  process.exit(1);
}
console.error("ERROR: unexpected mock call count " + calls);
process.exit(2);
Test (node@24, vite@7.1.11, vitest@3.2.4, expected to fail)
const fs = require("fs");
const path = require("path");
const cp = require("child_process");
const dir = "/w/restoreall";
fs.rmSync(dir, { recursive: true, force: true });
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, "restore.test.mjs"), [
  "import { test, vi } from 'vitest'",
  "import { writeFileSync } from 'node:fs'",
  "test('restoreAllMocks', () => {",
  "  const fn = vi.fn()",
  "  fn()",
  "  fn()",
  "  vi.restoreAllMocks()",
  "  writeFileSync(new URL('./calls.txt', import.meta.url), String(fn.mock.calls.length))",
  "})",
].join("\n") + "\n");
const r = cp.spawnSync(process.execPath, ["/w/node_modules/vitest/vitest.mjs", "run"], {
  cwd: dir,
  encoding: "utf8",
  env: { ...process.env, NO_COLOR: "1", CI: "1" },
  timeout: 60000,
});
console.log((r.stdout || "") + (r.stderr || "") + (r.error ? String(r.error) : ""));
const callsFile = path.join(dir, "calls.txt");
if (r.status !== 0 || !fs.existsSync(callsFile)) {
  console.error((r.stdout || "").slice(-1500) + (r.stderr || "").slice(-1500) + (r.error ? String(r.error) : ""));
  console.error("ERROR: vitest run did not complete (exit " + r.status + ")");
  process.exit(2);
}
const calls = fs.readFileSync(callsFile, "utf8").trim();
if (calls === "2") {
  console.log("PASS: vi.restoreAllMocks left the call history of a vi.fn() mock alone");
  process.exit(0);
}
if (calls === "0") {
  console.error("FAIL: vi.restoreAllMocks cleared the call history of a vi.fn() mock");
  process.exit(1);
}
console.error("ERROR: unexpected mock call count " + calls);
process.exit(2);
Source

`vi.restoreAllMocks` no longer resets the state of spies and only restores spies created manually with `vi.spyOn`, automocks are no longer affected by this function

raw.githubusercontent.com