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

Files of Next.js Testing

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)

Discussion

Alternatives