slnodejs
slnodejs — SeaLights Node.js Agent
slnodejs is the SeaLights agent for Node.js applications. It reports the structure of each build to SeaLights (build scanning), collects code coverage while your tests or your application run, and applies Test Impact Analysis to Mocha tests. It can also instrument web application code so that the SeaLights browser agent reports browser coverage.
Install
Install the package in your project:
npm install --save-dev slnodejs
To load the preload script in an environment that installs production dependencies only (for example npm ci --omit=dev in a container image), install it as a regular dependency instead: npm install slnodejs.
Usage
List the commands, then print the flags of the command you need:
npx slnodejs --help
npx slnodejs <command> --help
npx slnodejs <command> --help prints every flag of a command with its description and default. It is the parameter reference.
Preload Script
To collect coverage from a Node.js process that you do not start through slnodejs, load the preload script with -r or through NODE_OPTIONS. slnodejs config creates the buildSessionId file that the preload script reads; the build must also be scanned with slnodejs scan:
printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt
npx slnodejs config --tokenFile sltoken.txt --appName my-app --branchName main --buildName 1.0.0
export SL_LAB_ID=my-lab
NODE_OPTIONS="-r ./node_modules/slnodejs/lib/preload.js" node server.js
The preload script is configured only through environment variables:
| Variable | Meaning |
|---|---|
SL_TOKEN or SL_TOKEN_FILE |
The SeaLights agent token, or the path of a file that contains it. Without either, sltoken.txt in the working directory is used. |
SL_BUILD_SESSION_ID or SL_BUILD_SESSION_ID_FILE |
The build session ID, or the path of a file that contains it. Without either, buildSessionId in the working directory is used. |
SL_LAB_ID |
The lab ID of the test environment. |
SL_COLLECTOR_URL |
The URL of a SeaLights Collector, when coverage is sent through one. |
SL_PROJECT_ROOT |
The project root. |
SL_PROXY |
An HTTP proxy for SeaLights requests. |
SL_TLS_VERIFY, SL_CA_FILE |
Not preload-specific: they apply to every slnodejs connection. Set SL_TLS_VERIFY=true to verify the certificates of SeaLights connections; SL_CA_FILE adds a PEM CA bundle for that check. |
The camelCase spellings (SL_token, SL_tokenFile, SL_labId, …) are also accepted and are read first.
If the agent cannot start, for example because no token is found, the preload script runs your program without SeaLights, so the program still works but no coverage is collected. Set NODE_DEBUG=sl to see the agent's log and the reason.