Unit and component testing
Set up Vitest or Jest for a Next.js app, write a component test, and learn why async Server Components belong in end-to-end tests instead.
Unit and component testing
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Unit and component tests are the fast layer: they run in a terminal with no browser or server, but cannot see routing, caching or the proxy.
- Vitest needs `@vitejs/plugin-react` and a `jsdom` environment; Jest works through `next/jest`, which sets up the compiler transform and mocks CSS, images and `next/font`.
- Query by role and accessible name, as a user would, so tests survive markup refactors.
- Vitest does not support `async` Server Components, so keep logic in plain functions you can unit test and cover the assembled page end to end.
What a unit test buys you
You change a price formatter and the invoice page quietly shows $NaN. A test catches that in a second, before a customer does. Unit and component tests are the fast layer of testing: they run in a terminal with no browser and no server, so you can run them on every save.
The Next.js docs define the terms. A unit test checks one piece in isolation, a function, a hook or a component. A component test is a unit test where the subject is a React component: render it, interact with it, assert on what the user would see. They are fast and precise, and they are blind to anything outside the unit: routing, caching, middleware, the real browser. That is the job of end-to-end tests, in the next lesson.
The React roadmap already teaches the testing mindset in testing components with React Testing Library, and the Node roadmap covers Vitest and Jest. This lesson is the Next.js-specific part: what to configure and what you cannot test this way.
Choosing Vitest or Jest
Next.js documents both, plus Playwright and Cypress for end-to-end. For a new project, Vitest is the lighter setup. For one with existing Jest tests, Jest works well because Next.js ships a helper for it.
Vitest needs a config file with the React plugin and a DOM environment:
// vitest.config.mts
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";
export default defineConfig({
plugins: [tsconfigPaths(), react()],
test: { environment: "jsdom" },
});
installed with vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths, as in the current docs. They note that the current Vitest and jsdom releases need a newer Node.js than the Next.js minimum of 20.9, so check the version requirements before installing.
Jest works through next/jest, a wrapper that configures the transform with the Next.js compiler, auto-mocks CSS, images and next/font, and loads your .env files. You write jest.config.ts around nextJest({ dir: "./" }) and set testEnvironment: "jsdom".
A first component test
// __tests__/page.test.tsx
import { expect, test } from "vitest";
import { render, screen } from "@testing-library/react";
import Page from "../app/page";
test("Page shows a heading", () => {
render(<Page />);
expect(screen.getByRole("heading", { level: 1, name: "Home" })).toBeDefined();
});
Query by role and accessible name, as the user’s assistive technology would, not by CSS class. The test then survives a refactor of the markup and doubles as a light accessibility check.
One gotcha with the config above. React Testing Library only cleans up after each test on its own when the test runner exposes globals, and Vitest does not by default. I ran two tests in one file, each calling render, and the second failed with Found multiple elements with the role "button", because the first test’s DOM was still on the page. Add the cleanup yourself:
import { afterEach } from "vitest";
import { cleanup } from "@testing-library/react";
afterEach(cleanup);
Put it in a setup file (test.setupFiles) so every test file gets it, or set test: { globals: true }. With that line the same two tests passed on Vitest 5.1.1 against Next.js 16.4.6.
What you cannot unit test: async Server Components
Here is the Next.js-specific catch. An async Server Component does await on a database or cookies() as it renders, and the test environment has no React server runtime to run that. The Vitest docs say plainly that Vitest does not support async Server Components, and the general Next.js testing page recommends end-to-end tests for them instead.
The practical response is architectural. Keep logic out of the component: put the data-shaping and decision code in plain functions you can unit test, and keep the async Server Component as thin wiring. Test the functions here and the assembled page in the browser. Synchronous Server Components and all Client Components can be rendered in a unit test normally.
You will also usually mock the Next.js pieces your component calls, such as next/navigation’s useRouter, since no router exists in the test.
What the video covers
OrcDev’s “How to Test Next.js Apps: A Quick & Easy Guide (2025)” is a 26-minute video, published July 2024, covering setup, a simple test and a more complex test in a Next.js project. Its metadata and chapters do not say whether it uses Jest or Vitest, so check the setup against the config above. It also comes from before Next.js 16. Dave Gray’s 25-minute Jest and React Testing Library video (2023) is listed under “deeper”.
Where this fits
Fast tests cover units. They do not prove that the pieces work together in a real browser. Next: end-to-end testing with Playwright.
Check yourself
What can a unit test of a component not tell you about a Next.js app?
Anything outside the unit, such as routing, caching, the proxy and real browser behavior, which need end-to-end tests.
Why can you not unit test an `async` Server Component with Vitest?
The test environment has no React server runtime that can run its `await` during render, and the docs say Vitest does not support them.
How does `next/jest` simplify a Jest setup?
It configures the Next.js compiler transform, auto-mocks stylesheets, image imports and `next/font`, and loads your `.env` files.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.