Run Tests
Use this skill when running, adding, or debugging tests in the Next.js repo.
The basic commands are in the root AGENTS.md. For flaky tests, read
flaky-tests.md.
Bootstrap a fresh checkout
- Run
pnpm install --frozen-lockfile. If you are deliberately building native bindings from source and the native postinstall fails, useNEXT_SKIP_NATIVE_POSTINSTALL=1(seescripts/install-native.mjs). - Build JS with workspace dependencies:
pnpm build. In a fresh checkout, a barepnpm --filter=next buildcan miss workspace outputs such as polyfills. - Install the browser version that matches the repo's Playwright:
pnpm exec playwright install chromium. A different Chromium can still fail withExecutable doesn't exist. - Rust or Turbopack changes need a native build before Next.js e2e tests. See
the root
AGENTS.mdrebuild rules.
Choose the command
- Mode-specific scripts such as
pnpm test-dev-turbocallscripts/run-jest.shand set the mode and bundler. - A dev-only test can be skipped by
test-start-*. CheckisNextDev, gates, and the CI job before choosing a command. pnpm test-unit <file>appends<file>tojest test/unit/ packages/next/ packages/font, so it can run all of those suites. Usepnpm exec jest <file>for one file.- Jest only searches the
rootsinjest.config.js. A copied test outside those roots reportsNo tests found. Use a temporary worktree for experiments. node run-tests.jsis the CI runner:--retries 0 --runs Nrepeats each selected file; repeats require--retries 0and fail whenNEXT_SKIP_ISOLATEis set.--concurrency 1serializes files.--debugshows test output.- It selects files, not individual test names. Use a Jest script with
-tto focus one case.
Match CI
- Inline snapshots can depend on CI env flags. Use the exact
afterBuild:flags from.github/workflows/build_and_test.yml, keep them set for the full run, and use the same bundler. For examplebuild-output-prerenderexpects__NEXT_EXPERIMENTAL_STRICT_ROUTE_TYPES=true. Thereact-dom/serverbuild depends on runtime and bundler: Edge contexts getserver.edge(norenderToPipeableStream), Node contexts getserver.node, and the Pages renderer differs between bundlers. Checkcrates/next-core/src/next_import_map.rs(Turbopack) andpackages/next/src/build/create-compiler-aliases.ts(webpack). - For exact replication of a failed job, use
$pr-status-triageand itslocal-repro.md. - Isolated installs use a
.nodefile frompackages/next-swc/nativewhen present; otherwise they use the installed@next/swc-*package. Check the loggedswcNativeDirectorybefore trusting a native-binding repro. nextTestSetup({ dependencies })installs dependencies in the isolated app. Arequire()in the Jest test file still resolves from the repo. Run isolated tools as child processes with theircwdin the test app.- New
test/e2esuites also run in deploy mode. Readtest/AGENTS.mdbefore adding file patches or log assertions.
Quick smoke testing
For fast feedback, generate a fixture with
pnpm new-test --args true <name> e2e. Run Next.js with the fixture as the
working directory and an absolute path to the built CLI:
cd <fixture-dir>
node <repo>/packages/next/dist/bin/next dev --port <port>Probe it with curl --max-time 10. If a throwaway app symlinks packages from
the checkout, link next, react, and react-dom individually. Set
turbopack.root and outputFileTracingRoot to a common parent when the build
needs to resolve the linked next package.
Commit fixtures completely
test/.gitignoreignorestsconfig.jsonundere2e/**,production/**, anddevelopment/**. If the fixture needs that file, force-add it withgit add -f. Don't force-add generatednext-env.d.ts.- The root
.gitignoreonly ignores the Rusttargetdirectories of the Cargo workspaces. If you add a Cargo workspace, add itstargetpath there. - Before pushing, compare the tracked fixture files with the files the test
needs. Use
git ls-tree -r --name-only HEAD -- <dir>, orgit check-ignore -v <path>for a single path.
Gates beyond the test run
pnpm --filter=next typesusestsconfig.build.json, which excludes tests. When test files change, runpnpm lint-typescript(or the fullpnpm types-and-precompiledwhen its clean-tree check is appropriate).pnpm lint-languagerunsalexon Markdown files. After editing docs, runpnpm exec alex <files> --quiet.- CI checks the PR merged with current canary. If Prettier passes locally but
fails in CI, compare
.prettierignoreand the formatter config with canary. - Common test-file ESLint rules include
jest/no-standalone-expectand@typescript-eslint/prefer-as-const. - Treat unexpected snapshot churn as a possible regression. Find the source of the output change before updating snapshots.
Related Skills
$pr-status-triage- CI failure triage and exact CI env reproduction$gate-tests-@gate/@force-gateinstead of skips$router-act- deterministic client-router tests