Every method, from the real class
The spy keeps the class’s method names and types. Rename a method, and TypeScript shows which tests still use the old name.
Create typed spies from a class. Set the answer, run your code, and check what it called. Works with Vitest, Bun, node:test, and Rstest.
const users = createSpyFromClass(UserService);
users.load.resolveWith(user);
users.save.rejectWith(new HttpError(409));
users.load.mustBeCalledWith(7);
01 / The idea
const users = {
load: vi.fn(),
save: vi.fn(),
remove: vi.fn(),
} as unknown as UserService;
vi.mocked(users.load).mockResolvedValue(user);
vi.mocked(users.save).mockRejectedValue(new HttpError(409));import { type Spy, createSpyFromClass } from 'vitest-auto-spy';
const users: Spy<UserService> = createSpyFromClass(UserService);
users.load.resolveWith(user);
users.save.rejectWith(new HttpError(409));The spy keeps the class’s method names and types. Rename a method, and TypeScript flags the old name in your test. The real constructor never runs. If you only have an interface, use createAutoMock<T>().
02 / Install
npm i -D vitest-auto-spyImport from the entry point for your test runner. Everything after that line is the same.
import { createSpyFromClass } from 'vitest-auto-spy'; // Vitest, no config
import { createSpyFromClass } from 'vitest-auto-spy/bun'; // bun:test
import { createSpyFromClass } from 'vitest-auto-spy/node'; // node:test
import { createSpyFromClass } from 'vitest-auto-spy/rstest'; // RstestThe package has no runtime dependencies. rxjs and the @angular/* packages are needed only for their own entry points. Angular TestBed specs import provideAutoSpy and injectSpy from vitest-auto-spy/angular (Angular 20 or newer); createSpyFromClass works in any spec. See Installation.
03 / Slow tests
Vitest prints one Duration for the whole run. perf lists the spec files that are actually slow, re-measures each one alone, and shows where the CPU time went. Run it locally or in CI:
npx vitest-auto-spy perf --gateerror perf-gate-slow-file libs/player/src/lib/vod/vod.component.spec.ts
The test bodies in this file add up to 9.20s, over the 5.00s budget (…). Re-measured on its own: 8.70s, still over budget.
┌─ measurements ────────────────────────────────────────────────
│ tests 38 242ms each 20× the median test
├─ slowest tests ───────────────────────────────────────────────
│ 527ms focus > moves through the controls
├─ where the time went · CPU profile, 8.41s sampled ────────────
│ hooks ███████████░░░░░░░░░ 54% test bodies 46%
│ by package ██████░░░░░░░░░░░░░░ 28% jsdom
│ in the spec setUpWith 38% · VodComponent_Template 17% · assertFocus 8%
├─ likely cause ────────────────────────────────────────────────
│ Most of the time is set-up that every test repeats: 54% is in hooks — setUpWith alone is 38%.
└───────────────────────────────────────────────────────────────The budget scales with the median test of the same run, so a fast laptop and a slow CI machine reach the same verdict. A file that was slow only because of its neighbours, and is fast when it runs alone, is reported as not reproduced, not as a failure. The profiler loads only for that re-measurement, so a normal run pays nothing. On Angular specs the card also splits time into TestBed setup, component creation, change detection and compilation. --code-quality writes the findings for the GitLab merge request widget.
04 / Where to go next
05 / Already have one
jest-auto-spies and jasmine-auto-spies have the same API. The codemod rewrites the imports and the calls, and shows the diff before anything is saved. Spectator, @testing-library/angular and Suites each have a page that maps their helpers to these, line by line.
What the others do differently · Migrate your tests · What is new in 4.0