Next.js Testing Guide
Next.js Testing

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.
---
name: nextjs-testing
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.
metadata:
triggers:
files:
- '**/*.test.{ts,tsx}'
- 'cypress/**'
- 'tests/**'
- 'jest.config.*'
keywords:
- vitest
- playwright
- msw
- testing-library
---
# Next.js Testing
Overview
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.
Manual install (npx, ZIP)
Claude Code
npx skills add HoangNguyen0403/agent-skills-standard --skill nextjs-testing -g -a claude-codeClaude Code (degit)
npx degit HoangNguyen0403/agent-skills-standard/skills/nextjs/nextjs-testing#develop ~/.claude/skills/nextjs-testingClaude.ai / desktop
Codex, Cursor, other agents
Connects with Agent Alley MCP · agentalley.io/mcp
Files included
SKILL.md3.0 KB
Read SKILL.md here
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] |
Details
- License
- MIT
- Stars on the repo
- ★ 569
- Checked
- Source
- HoangNguyen0403/agent-skills-standard ↗
- Creator
- HoangNguyen0403 · 21 listings
Similar skills
See all →
SkilliOS Simulator Testing Scripts
SkillMoAI Autonomous Orchestrator
SkillAndroid Test Writer
AgentVibe Testing
SkillFeature Testing Framework
SkillWeb App TesterMore from HoangNguyen0403
SkillAndroid Test Writer
SkillArchitecture Diagram Designer
SkillCode Review Expert
SkillCybersecurity Framework Mapper
SkillDecision Discipline Framework
SkillFlutter Robot Pattern Tests