Next.js Testing Guide

Next.js Testing

by HoangNguyen0403 · MIT license · ★ 569 on GitHub

Next.js Testing cover

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 Codenpx skills add HoangNguyen0403/agent-skills-standard --skill nextjs-testing -g -a claude-code
Claude Code (degit)npx degit HoangNguyen0403/agent-skills-standard/skills/nextjs/nextjs-testing#develop ~/.claude/skills/nextjs-testing
Claude.ai / desktopZip the folder and upload it under Settings → Capabilities → Skills.
Codex, Cursor, other agentsPoint them at SKILL.md. The other files are plain text.

Connects with Agent Alley MCP · agentalley.io/mcp

Files included

  • SKILL.md3.0 KB
Read SKILL.md here
HoangNguyen0403/develop1 file shown
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

  1. Write unit tests — Use Jest (or Vitest for new projects) + RTL with Arrange-Act-Assert pattern.
  2. Mock APIs for unit/component tests — Set up MSW handlers for fetch boundaries in isolated tests where network determinism is required.
  3. Test interactions — Use userEvent (async) for clicks, typing, form submissions.
  4. Add E2E tests — Use Playwright for critical user flows (login, checkout).
  5. 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

See implementation examples

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 server and browser handlers are correctly configured.
  • Interactions: Use userEvent (async) to simulate user actions: await user.click(button).
  • Selectors: Favor getByRole / findByRole to test accessibility. Use data-testid only as fallback.
  • Environment: For Jest, use jest-environment-jsdom. For Vitest, configure vitest.config.ts with jsdom or happy-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---
2name: nextjs-testing
3description: 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.
4metadata:
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 
281. **Write unit tests** — Use Jest (or Vitest for new projects) + RTL with Arrange-Act-Assert pattern.
292. **Mock APIs for unit/component tests** — Set up MSW handlers for fetch boundaries in isolated tests where network determinism is required.
303. **Test interactions** — Use `userEvent` (async) for clicks, typing, form submissions.
314. **Add E2E tests** — Use Playwright for critical user flows (login, checkout).
325. **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 
36See [implementation examples](references/implementation.md)
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](references/implementation.md)
View all files on GitHub →

Details

License
MIT
Stars on the repo
★ 569
Checked
Source
HoangNguyen0403/agent-skills-standard ↗
Creator
HoangNguyen0403 · 21 listings
View creator

Similar skills

See all →
SkilliOS Simulator Testing Scriptsconorluddy/ios-simulator-skill · by conorluddy29 production-ready scripts for iOS app testing, building, and automation. Provides semantic UI navigation, build automation, accessibility testing, and simulator lifecycle management. Optimized for AI agents with minimal token output.★ 1.3k GitHub starsSkillMoAI Autonomous Orchestratormodu-ai/moai-adk · by modu-aiMoAI unified orchestrator for autonomous development. Routes natural language or subcommands (plan, run, sync, project, fix, loop, mx, feedback, review, clean, codemaps, gate, e2e, harness, goal, gtd, todo) to specialized agents.★ 1.2k GitHub starsSkillAndroid Test WriterHoangNguyen0403/agent-skills-standard · by HoangNguyen0403Write 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.★ 569 GitHub starsAgentVibe TestingAishwaryShrivastav/vibe-testing · by AishwaryShrivastavCode-aware browser testing for AI coding agents. 14 MCP tools, CLI workflows, regression memory, and human-readable QA reports.★ 18 GitHub starsSkillFeature Testing Frameworkobra/superpowers · by obraUse when implementing any feature or bugfix, before writing implementation code★ 290k GitHub starsSkillWeb App Testeranthropics/skills · by anthropicsToolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.★ 178k GitHub stars

More from HoangNguyen0403

SkillAndroid Test WriterHoangNguyen0403/agent-skills-standard · by HoangNguyen0403Write 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.★ 569 GitHub starsSkillArchitecture Diagram DesignerHoangNguyen0403/agent-skills-standard · by HoangNguyen0403Draws architecture diagrams as editable draw.io files with a fixed house style, C4 levels, evidence-tagged shapes, and optional multi-view identity checks. Use when producing a system context, container, component, deployment, data flow, sequence, state, or ERD, or redrawing an ASCII or Mermaid one.★ 569 GitHub starsSkillCode Review ExpertHoangNguyen0403/agent-skills-standard · by HoangNguyen0403Conduct high-quality, persona-driven code reviews. Use when reviewing PRs, critiquing code quality, or analyzing changes for team feedback.★ 569 GitHub starsSkillCybersecurity Framework MapperHoangNguyen0403/agent-skills-standard · by HoangNguyen0403Maintains sparse, reviewable cybersecurity framework edges with versioned IDs, relation, rationale, source, and review status. Use when mapping exercise observations or procedures to NIST, ATT&CK, or another named framework.★ 569 GitHub starsSkillDecision Discipline FrameworkHoangNguyen0403/agent-skills-standard · by HoangNguyen0403Right-size, ground, and gate SDLC decisions with SNC-sized depth, said-vs-assumed write-back, an evidence ledger, honest option cards, recorded approval, and self-review. Use when running brainstorm-feature, plan-feature, design-solution, or system-design-session.★ 569 GitHub starsSkillFlutter Robot Pattern TestsHoangNguyen0403/agent-skills-standard · by HoangNguyen0403Write unit, widget, and integration tests with robot patterns, widget keys, and Patrol in Flutter. Use when implementing test behavior; defer CI-only configuration without test changes.★ 569 GitHub stars