Next.js Testing skill
Write Jest or Vitest unit tests with React Testing Library and Playwright E2E tests for Next.js projects.
by HoangNguyen0403·MIT license·★ 569 Stars on the repo·GitHub ↗
Use now
npx degit HoangNguyen0403/agent-skills-standard/skills/nextjs/nextjs-testing#develop ~/.claude/skills/nextjs-testingChecked ·commit develop
Files of Next.js Testing
SKILL.md
Show the full text57 lines
Next.js Testing
Priority: P1 (HIGH)
Test Runner
- Existing projects (Pages Router / legacy stack): Use Jest (
jest@29+babel-jest+jest-environment-jsdom). - New projects (App Router): Use Vitest for speed and native ESM support.
Workflow: Test New Feature
- Write unit tests — Use Jest (or Vitest for new projects) + RTL with Arrange-Act-Assert pattern.
- Mock APIs for unit/component tests — Set up MSW handlers for fetch boundaries in isolated tests where network determinism is required.
- Test interactions — Use
userEvent(async) for clicks, typing, form submissions. - Add E2E tests — Use Playwright for critical user flows (login, checkout).
- Verify coverage — Rely on risk-weighted behavioral verification and project-configured CI coverage gates; never write padding tests to hit an arbitrary percentage.
Component Test Example
Implementation Guidelines
- Unit Testing: Use Jest (existing projects) or Vitest (new projects) with React Testing Library (RTL). Follow Arrange-Act-Assert (AAA) patterns.
- E2E Testing: Use Playwright for full user flow validation. Focus on critical flows (Login, Checkout).
- Networking: Intercept API boundaries using Mock Service Worker (MSW) for isolated component/unit tests to guarantee deterministic fixtures; allow intentional real-network or staging coverage in dedicated Playwright E2E/contract suites. Ensure
serverandbrowserhandlers are correctly configured. - Interactions: Use
userEvent(async) to simulate user actions:await user.click(button). - Selectors: Favor
getByRole/findByRoleto test accessibility. Usedata-testidonly as fallback. - Environment: For Jest, use
jest-environment-jsdom. For Vitest, configurevitest.config.tswithjsdomorhappy-dom. - Reporting: Ensure tests generate JSON coverage reports for CI gates. Verify risk-weighted behavioral paths rather than padding code to reach arbitrary percentage targets.
Anti-Patterns
- No unisolated network in unit tests: Use MSW handlers or fakes for unit/component tests; do not perform uncontrolled external network calls outside dedicated E2E suites.
- No implementation testing: Test user behavior, not internal methods.
- No heavy E2E for unit logic: Use Jest/Vitest for isolated logic tests.
- No global state leakage: Reset MSW handlers and mocks after each test.
References
| 1 | |
| 2 | name nextjs-testing |
| 3 | description Write Jest or Vitest unit tests with React Testing Library and Playwright E2E tests for Next.js projects. Use when testing components with RTL, mocking APIs with MSW, or creating Playwright user flow tests. |
| 4 | metadata |
| 5 | triggers |
| 6 | files |
| 7 | - '**/*.test.{ts,tsx}' |
| 8 | - 'cypress/**' |
| 9 | - 'tests/**' |
| 10 | - 'jest.config.*' |
| 11 | keywords |
| 12 | - vitest |
| 13 | - playwright |
| 14 | - msw |
| 15 | - testing-library |
| 16 | |
| 17 | # Next.js Testing |
| 18 | |
| 19 | ## **Priority: P1 (HIGH)** |
| 20 | |
| 21 | ## Test Runner |
| 22 | |
| 23 | **Existing projects (Pages Router / legacy stack)**: Use **Jest** (`jest@29` + `babel-jest` + `jest-environment-jsdom`). |
| 24 | **New projects (App Router)**: Use **Vitest** for speed and native ESM support. |
| 25 | |
| 26 | ## Workflow: Test New Feature |
| 27 | |
| 28 | **Write unit tests** — Use Jest (or Vitest for new projects) + RTL with Arrange-Act-Assert pattern. |
| 29 | **Mock APIs for unit/component tests** — Set up MSW handlers for fetch boundaries in isolated tests where network determinism is required. |
| 30 | **Test interactions** — Use `userEvent` (async) for clicks, typing, form submissions. |
| 31 | **Add E2E tests** — Use Playwright for critical user flows (login, checkout). |
| 32 | **Verify coverage** — Rely on risk-weighted behavioral verification and project-configured CI coverage gates; never write padding tests to hit an arbitrary percentage. |
| 33 | |
| 34 | ## Component Test Example |
| 35 | |
| 36 | See [implementation examples] |
| 37 | |
| 38 | ## Implementation Guidelines |
| 39 | |
| 40 | **Unit Testing**: Use **Jest** (existing projects) or **Vitest** (new projects) with **React Testing Library (RTL)**. Follow **Arrange-Act-Assert (AAA)** patterns. |
| 41 | **E2E Testing**: Use **Playwright** for full user flow validation. Focus on critical flows (Login, Checkout). |
| 42 | **Networking**: Intercept API boundaries using **Mock Service Worker (MSW)** for isolated component/unit tests to guarantee deterministic fixtures; allow intentional real-network or staging coverage in dedicated Playwright E2E/contract suites. Ensure **`server` and `browser` handlers** are correctly configured. |
| 43 | **Interactions**: Use **`userEvent` (async)** to simulate user actions: `await user.click(button)`. |
| 44 | **Selectors**: Favor **`getByRole`** / **`findByRole`** to test accessibility. Use **`data-testid`** only as fallback. |
| 45 | **Environment**: For Jest, use `jest-environment-jsdom`. For Vitest, configure `vitest.config.ts` with `jsdom` or `happy-dom`. |
| 46 | **Reporting**: Ensure tests generate **JSON coverage reports** for CI gates. Verify risk-weighted behavioral paths rather than padding code to reach arbitrary percentage targets. |
| 47 | |
| 48 | ## Anti-Patterns |
| 49 | |
| 50 | **No unisolated network in unit tests**: Use MSW handlers or fakes for unit/component tests; do not perform uncontrolled external network calls outside dedicated E2E suites. |
| 51 | **No implementation testing**: Test user behavior, not internal methods. |
| 52 | **No heavy E2E for unit logic**: Use Jest/Vitest for isolated logic tests. |
| 53 | **No global state leakage**: Reset MSW handlers and mocks after each test. |
| 54 | |
| 55 | ## References |
| 56 | |
| 57 | [Next.js Test Patterns] |
Discussion
Alternatives
Android Testing StandardsWrite Android unit, Compose UI, and Hilt-integrated tests. Use when designing test behavior with MockK or coroutine test utilities; defer database/WorkManager-specific recipes to the owning feature skill.
Test-Driven Development (TDD)Use when implementing any feature or bugfix, before writing implementation code
Webapp testingToolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.Test first bug fixing approachGuide to fixing bugs using a test-first approach, ensuring code reliability through systematic testing and implementation.
Browse more free Claude skills or everything in Development.