Skip to content
ProductionChapter 33 / 34Advanced5 min read

Testing Next.js Apps

Test Next.js apps with Playwright and Vitest - unit tests with Vitest and React Testing Library, end-to-end tests with Playwright, with setup steps.

On this page

1. What is testing a Next.js app?

Test typeWhat it checksTool
UnitOne function or component on its ownVitest + React Testing Library
End-to-end (E2E)The user's whole journey: routing, server, databasePlaywright

2. Why does it matter?

Checking login, checkout and forms by hand before every deploy is tiring and unreliable. Tests do this work in seconds on every push.

Manual testing onlyAutomated tests
Click through by hand on every releaseRun automatically in CI on every push
You do not notice when old features breakRegressions are caught right away
Fear of refactoringIf tests pass, the refactor is safe
Bugs are found in productionBugs are found in the pull request

3. Where is it used?

ThingBetter test
A utility function like formatPrice()Vitest unit test
A sync component like Button or CardVitest + Testing Library
An async Server Component that fetches dataPlaywright E2E
Login, checkout, form submit flowPlaywright E2E
Server Action validation logicMove the logic to a separate function and test it with Vitest

4. How does it work?

Testing pyramid
Lots of unit tests (fast, cheap)
Some integration tests
A few E2E tests (slow, but a real browser)
CI runs everything on every push

Vitest renders components in jsdom, without a browser. Playwright first starts the app with next build and next start, then performs real clicks in Chromium, Firefox or WebKit.

5. Code example

Vitest setup

bash
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths
ts
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",
  },
});

The file above is vitest.config.mts. Now a unit test:

tsx
import { expect, test } from "vitest";
import { render, screen } from "@testing-library/react";
import Page from "../app/page";

test("home page shows the heading", () => {
  render(<Page />);
  expect(screen.getByRole("heading", { level: 1, name: "Home" })).toBeDefined();
});

Playwright setup

bash
npm init playwright@latest
ts
import { defineConfig } from "@playwright/test";

export default defineConfig({
  testDir: "./e2e",
  use: { baseURL: "http://localhost:3000" },
  webServer: {
    command: "npm run build && npm run start",
    url: "http://localhost:3000",
    reuseExistingServer: !process.env.CI,
  },
});
ts
import { test, expect } from "@playwright/test";

test("user can navigate to the about page", async ({ page }) => {
  await page.goto("/");
  await page.getByRole("link", { name: "About" }).click();
  await expect(page).toHaveURL("/about");
  await expect(page.getByRole("heading", { level: 1 })).toContainText("About");
});

How does the code work?

ItemMeaning
environment: "jsdom"A fake DOM in Node so components can render
tsconfigPaths()Path aliases like @/components also work in tests
render and screenRender a component and find elements the way a user would
webServerPlaywright starts the production server itself before the tests
getByRoleFinds an element by its accessible role, sturdier than a CSS selector
toHaveURLChecks the URL after navigation
json
{
  "scripts": {
    "test": "vitest",
    "test:e2e": "playwright test"
  }
}

6. Common mistakes

Mistake: Testing an async Server Component in Vitest

Why? Vitest does not yet support rendering async Server Components, so the test fails or gives a wrong result.

Better: Write a Playwright E2E test for async components, and pull their logic into a separate function to unit test it.

Mistake: Running E2E tests against next dev

Why? Dev mode is slow and behaves differently from production, so the tests are flaky and misleading.

Better: Give the Playwright webServer the build and start commands so that a real production build is tested.

Mistake: Finding elements by CSS class or DOM structure

Why? Selectors break as soon as the design changes, even though the feature works fine.

Better: Use getByRole, getByLabel and getByText, the way a user sees the screen.

Mistake: Writing only E2E tests

Why? E2E tests are slow, CI gets long and it is hard to find the real cause of a failure.

Better: Write more unit tests, and use E2E only for important flows like login and checkout.

Interview tip

"When should you use a unit test and when an E2E test?" Use unit tests for pure logic and small components, because they are fast and cheap. Use E2E where routing, server, database and browser all work together.

7. What to learn before and after

Final quick revision

PointDetail
Unit testsVitest + React Testing Library, jsdom
E2E testsPlaywright, a real browser
Async Server ComponentsTest them with E2E
Playwright serverBuild and start in webServer
SelectorsgetByRole, getByLabel