Unit Testing NestJS with Vitest for JUnit Developers: Two Calls, Not One
The design sheet The full architecture and the verified references. Free, no email.
4:03 docker compose up, and watch it happen
A NestJS project created today with nest new, one vi.fn() shared by two tests, each
calling it once. The second test saw two calls. The same two tests with a Mockito @Mock
field saw one call each. Measured on 30 September 2026.
Which Vitest you are on
Vitest 4 keeps a mock’s call history between tests unless clearMocks is set. Vitest 5
changed that default, and the same test reported one call each. But @nestjs/schematics
12.0.6 still pins "vitest": "^4.1.2", and that range never reaches 5. On the generated
setup, set clearMocks: true, or upgrade to Vitest 5 deliberately after its migration notes.
Three reset settings
With a stub and a spy created once at describe scope: clearMocks cleared the history and
kept both, the closest match to Mockito. mockReset destroyed the stub before the first test
ran, so the balance came back undefined. restoreMocks put the spy back to the real method.
Create shared stubs in beforeEach.
Strictness and hoisting
Mockito’s strict stubs fail a test that stubs something it never uses. The same unused stub
passes in Vitest. And because vi.mock is hoisted to the top of the file, a factory that
reads a file constant fails with “Cannot access ‘greeting’ before initialization”. In NestJS,
overrideProvider on the testing module is often the cleaner seam, the counterpart of
@MockitoBean (at the time of recording, @MockBean is gone in Spring Boot 4).
The full code for every measurement is in the repository, and the free design sheet above has the checklist on one page.