Next.js with SeaLights Node.js and Browser Agents (Multi-Module Scan)
Next.js with SeaLights Node and Browser Agents (Multi-Module Scan)
Next.js applications require two scans of the same build session: one for the browser bundles in
.next/static/and one for the server-side bundles in the remainder of.next/. The browser bundles are instrumented at build time, while server-side coverage is collected using the SeaLights Node.js runtime preload.Requires:
slnodejs6.2.10 or later
Overview
Next.js produces two distinct types of bundles during a production build. SeaLights processes each bundle type using a different instrumentation approach.
Client (browser)
.next/static/
Build-time scan with --instrumentForBrowsers
Server (Node.js)
Remaining .next/ output
Runtime coverage using the SeaLights Node.js preload via -r .../slnodejs/lib/preload.js
Both scans use the same build session and are identified by different --uniqueModuleId values. This allows SeaLights to present the client and server coverage as a single application.
Why Are Two Scans Required?
Next.js splits your code into two fundamentally different execution environments, each requiring a different instrumentation approach:
Client bundles
.next/static/ contains JavaScript that executes in the browser. SeaLights instruments these bundles during the build using:
--instrumentForBrowsersServer bundles
The remaining .next/ output contains Node.js code executed on the server. SeaLights collects coverage for these modules at runtime using the Node.js preload:
One build session
Both scans use the same build session:
This allows SeaLights to associate the two sets of modules with the same application build.
Important: Do not reuse the same --uniqueModuleId for the client and server scans. Each scan must have a unique module ID, such as client and server.
Prerequisites
Before you begin, ensure you have:
A Node.js application using Next.js 15 or later.
slnodejs6.2.10 or later.A SeaLights Agent Token.
Permission to modify the application's
next.config.mjs.A production build that generates source maps for both client and server bundles.
Step 1 — Enable Source Maps in Next.js
Add both flags to next.config.mjs:
Important: Do not skip this step. SeaLights uses the generated .js.map files to map runtime coverage hits back to your original source files. Without source maps the server will report 0% coverage.
Step 2 — Exclude Framework-Generated Files
Create .slignore.generated file in the project root:
This prevents framework-generated files from appearing in the SeaLights coverage tree and keeps the coverage view focused on application source code. Example, without .slignore.generated file the SeaLights coverage tree shows ~200 internal Next.js/webpack entries that obscure the actual src/ code.
Step 3 — Provision Your Agent Token
Place your SeaLights agent token in sltoken.txt at the project root.
Important: Do not commit the token to source control.
Add the following entries to .gitignore:
Step 4 — Build and Scan the Application
The script below executes the complete build → scan → start lifecycle.
Replace <YOUR_APP> with your SeaLights application name. In CI, replace --branch main and --build "$BUILD_ID" with your real branch name and build identifier (e.g. the commit SHA).
Step 5 — Open a Test Session, Exercise the App, Then Close It
After the server starts (Step 4), open a test stage from a separate terminal in the same project directory. This associates all incoming coverage data with the correct test stage on the dashboard.
Step 6 — Verify Coverage on the Dashboard
Stop the server (
Ctrl+C).Wait for data ingestion to complete.
Open the SeaLights coverage dashboard.
Select the application and build created in Step 4.
Expand the build to see the Manual test stage.
Verify that both client-side and server-sde source files contain coverage.
The coverage tree should show your application source files with coverage collected from both the browser and server execution environments.
Troubleshooting
src/ shows 0% coverage
Source maps not enabled or are not available during the scan
Add productionBrowserSourceMaps and serverSourceMaps flags to next.config.mjs.
webpack:/ or .next/ folders appear in coverage tree
.slignore.generated is missing or not being loaded
Create .slignore.generated in the project root and ensure slnodejs is 6.2.10 or later.
lib/ or layout.tsx stay at 0% even though pages are covered
Older slnodejs version or source-map resolution issue
Upgrade to slnodejs 6.2.10 or later.
/api/... routes show 0% coverage
Routes are never called implicitly by Next.js
Explicitly call the API routes using your tests or tools such as curl.
sltoken.txt not found
The agent token file is missing from the project root
Create sltoken.txt and add the SeaLights Agent Token.
Last updated
Was this helpful?

