Backend Instrumentation (dynamic)
Collect coverage from a running Node.js service with slnodejs run or the preload script: lab IDs, per-test coverage, ES modules, containers.
Applies to: Node.js services, APIs, workers and CLIs (Express, NestJS, Fastify, Koa and others) that run while tests exercise them.
Back-end code is instrumented at runtime. You deploy the original code and start the process with the SeaLights test listener, which collects coverage while the process runs and sends it for the process's lab ID. By default the test listener uses NYC; for lower overhead, see Lightweight V8 Coverage Mode.
Scan the service
Scan the build first. See Scanning Your Application. Use the same project root for the scan and for the running process (both default to the working directory).
Start the service with the test listener
Choose how to start it:
slnodejs run
You control the start command (script, Dockerfile CMD).
None.
Preload script (node -r or NODE_OPTIONS)
A wrapper starts the process (npm start, pm2, a platform entry point), or every Node.js process in a test container should be covered.
Settings from environment variables only; no signal forwarding; no HTTP coloring; no NYC coverage for ES modules programs.
Option A: slnodejs run
Everything after -- is the program and its arguments.
npx slnodejs run \
--tokenFile sltoken.txt \
--buildSessionIdFile buildSessionId \
--labId qa-env \
-- dist/server.jsnpx slnodejs run `
--tokenFile sltoken.txt `
--buildSessionIdFile buildSessionId `
--labId qa-env `
-- dist/server.jsCMD ["npx", "slnodejs", "run", "--tokenFile", "/run/secrets/sltoken/sltoken.txt", "--buildSessionIdFile", "/app/buildSessionId", "--labId", "qa-env", "--forwardSignals", "--", "dist/server.js"]Useful flags:
--labId <id>
Always in a shared test environment. One lab ID per environment, the same for all replicas. Without it, the build session ID is the lab ID.
--forwardSignals
In containers and process managers, so that SIGTERM and SIGINT reach the service. --signalForwardingTimeout sets how long to wait before SIGKILL (default 10 seconds).
--httpServerColoring
Per-test coverage: attributes coverage of each incoming request to the test named in its headers.
--httpClientColoring
The service calls other instrumented services: passes the current test on outgoing requests.
--es6Modules
The program is ES modules ("type": "module").
--projectRoot <path>
The process starts in another directory than the scan ran in.
--collectorUrl <url>
Send coverage through a SeaLights Collector.
--libraries <list>
Your own code ships as packages in node_modules that must be covered.
Every flag: Command Reference → run.
Option B: preload script
The preload script starts the test listener inside the normal start command. It reads its settings from environment variables, with these exact names (case-sensitive; each also has an upper-case spelling, for example SL_LAB_ID):
Without SL_tokenFile and SL_buildSessionIdFile, the preload script uses ./sltoken.txt and ./buildSessionId.
NODE_OPTIONS applies to every Node.js process started in that shell or container, including build tools. Set it only in the environment that runs the service, or use node -r for a single process.
If the test listener cannot start, the preload script runs the original command without SeaLights.
Check that it works
Start the service. The console shows the build session and lab ID, and the log shows
Running agent using NYC(orRunning agent using lean V8 coverage listener).If the console shows
Sealights is disabled, running command without sealights, the agent could not start; see Troubleshooting.Open the Agent Monitor and check that the agent appears with the expected lab ID.
Open a test stage on the same lab, send at least one request, close the test stage, and check the coverage on the dashboard.
Run your tests
Open a test stage on the service's lab while the tests run. See Capturing Tests → Functional tests.
Multiple environments and replicas
Use the same lab ID for every replica of the service in one environment.
Use a different lab ID for each environment, for example
qa-envandstaging-env.Use the same lab ID for all services (and the front end) that are tested together in one environment.
Child processes and cluster workers
If the service forks workers (child_process.fork, cluster, process pools) that run your code, set SL_enableChildProcessPatcher=true so the agent also starts in those processes.
Troubleshooting
error: option '--useinitialcolor <useinitialcolor>' argument missing
--useinitialcolor takes a value: --useinitialcolor true. It is meant for test runners started with run, not for services.
Sealights is disabled, running command without sealights
The token or build session ID is missing or invalid. Check the file paths and that the build session comes from the same SeaLights account.
The agent appears, but coverage is 0%
No test stage was open on the service's lab ID, or the project root differs from the scan. See Troubleshooting → Coverage is missing or low.
The agent does not appear with the preload script
NODE_OPTIONS was replaced by a later layer (for example the base image or npm scripts), or the variable names are misspelled. Print NODE_OPTIONS in the running process.
slnodejs rejects a program argument as an unknown option, or the program receives SeaLights flags
Put -- between the SeaLights flags and the program. See Understanding the -- separator.
The service runs out of memory or starts slowly with the agent
See also dryRun to check the scan locally.
Last updated
Was this helpful?

