Skip to main content
Next.js supports several testing approaches, each serving different purposes:
async Server Components are new to the React ecosystem and not fully supported by Jest or Vitest for unit testing. Use end-to-end testing for async Server Components.

Jest

Jest and React Testing Library are the standard for unit and snapshot testing in Next.js.

Setup

Use create-next-app with the Jest example:

Custom matchers

Add @testing-library/jest-dom custom matchers like .toBeInTheDocument():

Writing tests

Add test scripts to package.json:
Create a __tests__ directory in your project root and write your first test:
Add a snapshot test to catch unexpected UI changes:

Module path aliases

If you use path aliases in tsconfig.json, configure moduleNameMapper in Jest:

Vitest

Vitest offers faster execution and native ESM support, making it a good alternative to Jest.

Setup

Writing Vitest tests

Vitest watches for changes by default when you run npm run test.

Playwright

Playwright enables end-to-end testing across Chromium, Firefox, and WebKit.

Setup

Writing E2E tests

Create pages to test:
Write a test to verify navigation:

Running Playwright tests

Build and start your app, then run tests:
For CI, install browser dependencies first:

Cypress

Cypress supports both end-to-end and component testing.

Setup

Writing Cypress E2E tests

Run tests against your production build:

Cypress component tests

Cypress can also mount and test individual React components without a running server:

CI configuration

Run Cypress headlessly in CI: