Part 17 / Topic 1 of 8
Why Vitest, and why pinned to 4.1.12 specifically
The installed Next.js docs recommend exactly this pairing for Unit Testing — node_modules/next/dist/docs/01-app/02-guides/testing/vitest.md opens with "Vitest and React Testing Library are frequently used together for Unit Testing" and gives the manual setup this part follows (vitest, @vitejs/plugin-react, jsdom, @testing-library/react, @testing-library/dom, vite-tsconfig-paths, plus @testing-library/jest-dom and @testing-library/user-event for the matcher/interaction sugar used below). A plain npm install -D vitest ... installed 5.1.2 — and that's a real problem, not a hypothetical one:
$ npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react ...
npm error ERESOLVE could not resolve
npm error Found: @types/[email protected]
...
npm error peerOptional @types/node@"^22.0.0 || >=24.0.0" from [email protected]
Vitest's latest dist-tag resolves to 5.1.2 — genuinely stable, no rc/beta marker, unlike the Prisma case DESIGN-NOTES.md already rejected — but it requires @types/node ^22 or >=24. This harness's @types/node is pinned to ^20 (create-next-app's own default, matching Next's engines.node: ">=20.9.0" — see TOOLING.md). Bumping the whole harness's Node typings for one devDependency, just to chase a latest tag, is the wrong trade. Checked the alternative the same evidence-based way DESIGN-NOTES.md checks everything else:
$ npm view vitest dist-tags --json
{ "V3": "3.2.7", "rc": "5.0.0-rc.4", "V4": "4.1.12", "beta": "5.2.0-beta.1", "latest": "5.1.2" }
$ npm view [email protected] peerDependencies
{ ..., "@types/node": "^20.0.0 || ^22.0.0 || >=24.0.0", ... }
Vitest itself publishes a V4 dist-tag for exactly this situation — the newest release of the previous major, actively maintained, and it accepts @types/node@^20. npm install -D [email protected] ... (full command and the resulting package.json versions are in section "Installed, for real" below) resolved cleanly with 0 vulnerabilities (npm audit). For contrast, the very first attempt at this part — before pinning — used an unpinned npm install and npm audit on the result reported one critical finding (tinypool, a Vitest-internal worker-pool dependency, prototype-pollution-to-RCE, fixed in a later patch than what an unpinned install briefly pulled in) and one moderate finding (@vitest/mocker path traversal). Pinning 4.1.12 resolved both: found 0 vulnerabilities, confirmed by rerunning npm audit after the pin.
Installed, for real
$ npm install -D [email protected] @vitejs/plugin-react jsdom @testing-library/react \
@testing-library/dom @testing-library/[email protected] @testing-library/user-event vite-tsconfig-paths
added 104 packages, and audited 472 packages
Exact versions, read from harness/package.json after install:
| Package | Version | Why this one |
|---|---|---|
vitest |
4.1.12 |
V4 dist-tag — newest release compatible with this harness's @types/node@^20 pin; see above |
@testing-library/react |
16.3.3 |
current stable, matches React 19 |
@testing-library/jest-dom |
7.0.1 |
its own vitest: '>= 0.32' peer is satisfied by 4.1.12; the just-released 8.0.0 demands vitest >=5 <6, which would reintroduce the exact conflict this section just resolved |
@testing-library/dom |
10.4.2 |
peer of both Testing Library packages above |
@testing-library/user-event |
14.6.13 |
realistic click/type simulation for section 6 |
@vitejs/plugin-react |
6.3.0 |
JSX transform for .tsx test files, the installed docs' own recommendation |
jsdom |
31.0.1 |
the DOM environment Vitest renders into |
vite-tsconfig-paths |
6.1.1 |
resolves this project's @/* import alias inside test files the same way next dev/next build do |
A package.json "test": "vitest run" script was added (vitest with no args watches by default — run is the one-shot mode this part always wants, including every transcript below).
Done here? The practice set for Part 17 — including the solutions — is on the part page.