async Server Components are new to the React ecosystem and not fully supported by Jest or Vitest for unit testing. Use end-to-end testing for async Server Components.Jest
Jest and React Testing Library are the standard for unit and snapshot testing in Next.js.Setup
- Quickstart
- Manual
Use
create-next-app with the Jest example:Custom matchers
Add@testing-library/jest-dom custom matchers like .toBeInTheDocument():
Writing tests
Add test scripts topackage.json:
__tests__ directory in your project root and write your first test:
Module path aliases
If you use path aliases intsconfig.json, configure moduleNameMapper in Jest:
Vitest
Vitest offers faster execution and native ESM support, making it a good alternative to Jest.Setup
- Quickstart
- Manual
Writing Vitest tests
npm run test.
Playwright
Playwright enables end-to-end testing across Chromium, Firefox, and WebKit.Setup
- Quickstart
- Manual
Writing E2E tests
Create pages to test:Running Playwright tests
Build and start your app, then run tests:Cypress
Cypress supports both end-to-end and component testing.Setup
- Quickstart
- Manual
