Skip to content

Next.js

This ports upstream’s Next.js page (docs/frameworks/nextjs.mdx, with its docs/examples/nextjs/e2e.config.ts). e2e init writes the Next.js target (below), but the port has no SetupPrompt card, and no with-next starter, so the page starts from a web suite and leaves out “Start a new app”. The config is C# code, because e2e.config.json can’t read CI.

This page adds E2E to an existing Next.js app. The runner starts next dev on your machine and next start in CI, and the web engine tests the app in Chromium.

e2e init finds next in the package.json above the test project and, in a new config, writes a web target that starts the app, then prints what it wrote:

Output
Found Next.js: the web target starts npm run dev on http://localhost:3000

The target runs the dev script with your package manager and reuseExisting: true, on the port of your dev script (--port, -p, or PORT= on the next command, else 3000). Because e2e.config.json can’t read CI, it runs dev in CI too; upstream’s init writes the start command for CI when build and start scripts serve the same port, which the code config below does.

Set app.command yourself so the first test starts your app and waits until app.url answers. A fixture base class builds the config in code, with E2EConfig.IsCi() choosing the command. It reads CI as E2E does: unset, "", 0, and false mean a local run.

NextTest.cs
using E2E.NUnit;
public abstract class NextTest : E2ETest
{
// Keep the `agents` entry your e2e.config.json had.
private static readonly Lazy<E2EConfig> NextConfig = new(() =>
{
// CI serves the production build: run `next build` before `dotnet test`.
var command = E2EConfig.IsCi()
? """{ "executable": "npm", "args": ["run", "start"] }"""
: """{ "executable": "npm", "args": ["run", "dev"], "reuseExisting": true }""";
var json = $$"""
{
"targets": [{
"name": "web",
"platform": "web",
"app": { "url": "http://localhost:3000", "command": {{command}} }
}],
"agents": { "default": { "provider": "gateway", "model": "openai/gpt-6-luna-fast" } }
}
""";
return E2EConfig.Parse(json, Directory.GetCurrentDirectory());
});
protected override E2EConfig Config => NextConfig.Value;
}

Derive your test classes from NextTest.

  • Locally, the first test starts next dev. With reuseExisting, it uses a dev server you already started on that port.
  • In CI, it starts next start, which serves the build. reuseExisting doesn’t apply there.
  • The port in app.url must be the port that dev and start both listen on. Next.js uses 3000. When one script sets --port, set the same port in the other.

command also takes env, log, startupTimeout, and shutdownTimeout, and a url with port 0 takes a free port. See CommandConfig and the Config section.

Use http://127.0.0.1:0 (or http://[::1]:0, never localhost:0) as app.url and pass {port} to the server in args or env: "args": ["run", "dev", "--", "--port", "{port}"]. A port-0 url needs a command, and reuseExisting beside it is INVALID_CONFIG, so drop it from the local command. Next.js then identifies as localhost while the target opens 127.0.0.1, so add the host to allowedDevOrigins (see below).

Build the app before the tests. The CI service sets CI, so the config starts next start:

Terminal window
npm run build
dotnet test

For a full workflow, take the GitHub Actions job and, in the Next.js app’s directory, set up Node.js and run npm ci before the build step. To test each Vercel preview deployment instead, see Vercel Previews.

Next.js 16 blocks cross-origin requests to its dev resources. When the target opens 127.0.0.1 or [::1], as port 0 does, next dev still identifies as localhost. The page renders, locators resolve, and clicks land, but no React handler runs.

Add the host the target opens to allowedDevOrigins. Use '[::1]' when app.url opens http://[::1]:0:

next.config.ts
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
allowedDevOrigins: ['127.0.0.1'],
};
export default nextConfig;

A localhost target and a production next start target need no entry.

GetByRole(“alert”) finds an extra element

Section titled “GetByRole(“alert”) finds an extra element”

The App Router renders an empty role="alert" route announcer on every page. Screen.GetByRole("alert") matches it too. Pick your alert by its text:

await Expect.That(Screen.GetByRole("alert").Filter("Enter a name first.")).ToBeVisibleAsync();

The agent doesn’t see the route announcer, and the replay cache doesn’t count it as a change.

E2E writes its run output under .e2e/: results, logs, traces, and the replay cache. A watcher that reads this folder can rebuild the app again and again during a run.

  • e2e init adds only some .e2e subpaths (.e2e/results/, .e2e/cache/, and a few more) to .gitignore. Add .e2e/ itself when a watcher must skip the whole folder. Tools that scan the project for source files, such as Tailwind CSS, skip ignored files.
  • With webpack, the next dev default before Next.js 16, add .e2e to the paths the watcher ignores:
next.config.ts
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
webpack: (config, { dev }) => {
if (dev) {
config.watchOptions = {
...config.watchOptions,
ignored: ['**/.git/**', '**/.next/**', '**/node_modules/**', '**/.e2e/**'],
};
}
return config;
},
};
export default nextConfig;

This list keeps the Next.js defaults. When your config already sets ignored, add '**/.e2e/**' to it instead.