Layered application windows with passing test indicators

Vitest and React Testing Library help test React components and utilities in a Next.js application. Setup involves registering the React plugin, choosing a DOM environment and resolving path aliases. This guide walks through those decisions, then shows where unit testing fits alongside browser-level checks for the App Router.

Quickstart with the official example

The fastest route is the with-vitest example maintained in the Next.js repository. Running create-next-app with the example flag generates a project that already includes the configuration, a sample component and a passing test. It is a good reference even if you plan to set up an existing project manually, because it shows the exact versions and files the Next.js team expects.

Manual setup in an existing project

Install the dependencies

Add vitest, @vitejs/plugin-react, jsdom, @testing-library/react and @testing-library/dom as development dependencies. Projects that use TypeScript path aliases such as the @ prefix also add vite-tsconfig-paths so Vitest resolves imports the same way Next.js does. All of these are dev dependencies and do not affect the production bundle.

Create the config file

Create vitest.config.mts in the project root. Export defineConfig from vitest/config with the React plugin in the plugins array, the tsconfig paths plugin if you installed it, and a test block that sets environment to jsdom. The .mts extension keeps the config in ESM regardless of the project's module setting. No Next.js-specific plugin is required because Vitest compiles components with the standard React plugin rather than the Next.js build.

Add the script

Add a test script to package.json with the value vitest. Running it starts watch mode; use vitest run in CI. Keep the existing dev, build and start scripts untouched.

Writing the first test

Create a __tests__ folder or colocate a page.test.tsx next to the component. Import expect and test from vitest, render and screen from @testing-library/react, and the page component. Render it, then assert that a heading with the expected name is in the document using getByRole and the toBeDefined matcher. Run the test and confirm it passes. From here, the usual Testing Library patterns apply: query by role, fire events through user-event, and assert on the resulting DOM.

App Router considerations

Synchronous Server Components and Client Components can be rendered and tested in the same way. Asynchronous Server Components are a newer part of React, and Vitest does not support rendering them in unit tests at the time of writing; the Next.js documentation recommends end-to-end tests for those. Components that call next/navigation hooks such as useRouter need a mock, because there is no router context in a unit test. Use vi.mock on the module and return the functions your component calls.

Environment variables and aliases

Next.js loads environment variables from .env files during its own build. Vitest does not read them automatically, so either load them in a setup file or set them in the test block's env option. For path aliases, the tsconfig paths plugin reads the same tsconfig.json that Next.js uses, which keeps imports consistent between the application and the tests.

Mocking Next.js modules

next/image, next/link and next/font modules work in jsdom for most cases, but you may prefer to mock next/image to a plain img element to avoid loader warnings. next/headers and server-only modules should be mocked in client-side tests. Put shared mocks in a setup file referenced by setupFiles so every test gets the same baseline.

Running in CI

Use vitest run with the junit reporter if your CI service renders test results, and add --coverage with the v8 provider to produce coverage reports. Because Vitest does not invoke the Next.js build, tests run quickly in CI without a .next directory, which keeps pipelines short.

Common issues

A ReferenceError about document means the environment is not set to jsdom. A failure to resolve an import with an @ prefix means the paths plugin is missing. A warning about JSX runtime means the React plugin is not in the plugins array. An error from useRouter means the navigation module needs a mock. Each of these is a one-line fix once identified.

Testing hooks, utilities and route handlers

Not everything in a Next.js app is a component. Custom hooks can be exercised with renderHook from Testing Library, pure utilities need no DOM at all and run faster with the Node environment selected per file, and route handlers in the app directory are plain functions that accept a Request and return a Response, so they can be called directly in a test with a constructed Request object. Keeping these tests in the Node environment avoids jsdom overhead where it is not needed.

Snapshot and visual checks

Vitest's snapshot support works with Testing Library's rendered output through the container element. Use snapshots sparingly for stable markup such as layout shells, and prefer explicit role and text queries for anything that changes often, because large snapshots are updated without review more often than they are read.

Organising the test folder

Next.js allows test files inside the app directory as long as they are not treated as routes; the common __tests__ convention keeps them visible but separate. A setup file referenced from the config holds Testing Library's jest-dom matchers if you use them, global mocks for navigation and images, and any environment variables the components read. With that in place, a new test is a single import and a render call.

If the team already uses Playwright or Cypress for end-to-end coverage, keep those for full page flows and reserve Vitest for the component and utility layer. The two complement each other: Vitest answers whether a piece behaves correctly in isolation, while the end-to-end suite confirms that the assembled application works in a real browser with real data.

Conclusion

Setting up Vitest with Next.js means installing the runner, the React plugin, jsdom and Testing Library, creating a small vitest.config.mts with the jsdom environment, and adding a test script. From there you can unit test synchronous Server and Client Components with familiar Testing Library patterns, mock Next.js modules where needed, and keep asynchronous Server Components in end-to-end tests until unit support arrives. The result is a fast feedback loop that lives alongside the Next.js dev server without interfering with it.