Recommended Configurations
Complete, copy-paste slnodejs configurations for the most common setups: back-end services, web applications, full stack, containers, monorepos, and pull request builds.
Each configuration below is complete: it creates the token file, the build session, the scan, the runtime coverage and the test capture. Pick the one that matches your answers in Choosing Your Setup, then replace the names in angle brackets.
All examples assume:
slnodejsis installed in the project (npm install --save-dev slnodejs); see Installing the Node.js Agent.The agent token is in the CI secret
SEALIGHTS_AGENT_TOKEN.Commands run from the project root.
Back-end service with Mocha unit tests
Use when: a Node.js service has Mocha unit or component tests that load the code in the test process.
What you get: per-test coverage, test results, and Test Impact Analysis (tests that SeaLights does not recommend are skipped).
printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt
npm ci
npm run build # if the service has a build step
npx slnodejs config --tokenFile sltoken.txt --appName "<my-service>" \
--branchName "$BRANCH_NAME" --buildName "$BUILD_NUMBER" --failOnError true
npx slnodejs scan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \
--scanDir src --scmType git --failOnError true
npx slnodejs mocha --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \
--testStage "Unit Tests" -- --recursive test
npx slnodejs completeScan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --okjobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # full history for source control data
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- name: SeaLights build session and scan
env:
SEALIGHTS_AGENT_TOKEN: ${{ secrets.SEALIGHTS_AGENT_TOKEN }}
run: |
printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt
npx slnodejs config --tokenFile sltoken.txt --appName "${{ github.event.repository.name }}" \
--branchName "${{ github.ref_name }}" --buildName "${{ github.run_id }}-${{ github.run_attempt }}" --failOnError true
npx slnodejs scan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \
--scanDir src --scmType git --failOnError true
- name: Unit tests with SeaLights
run: npx slnodejs mocha --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --testStage "Unit Tests" -- --recursive test
- name: Complete the build
run: npx slnodejs completeScan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --okNotes:
Scan the files that the tests load. For TypeScript run through
ts-nodeor a Mocha TypeScript loader, scansrcand setSL_fileExtensions=.ts. For code compiled before the tests run, scan the output folder.Keep your Mocha reporter:
-- --recursive test --reporter-options reporter=dot. See Mocha → Reporter options.Add
--es6Modulestoscanandmochafor ES modules code.
Unit tests with an NYC report
Use when: your unit tests run with Karma, AVA, or another framework that writes an NYC (Istanbul) JSON report and a JUnit XML report. For Jest, prefer the SeaLights Jest plugin.
What you get: coverage and test results per test stage. No Test Impact Analysis.
Notes:
The NYC report must cover the same files as the scan. Before the first run, check both with
npx slnodejs dryRun --scanDir src --hasNycReport --report coverage/coverage-final.json --verbose.If the tests run in another directory than the scan (for example in a container), add
--projectRoot <path>tonycReport.
Back-end service under functional tests
Use when: a deployed Node.js service is exercised by API, integration or end-to-end tests that run in another process or machine.
What you get: coverage of the service per test stage, and per test when the test framework sends the test identity in HTTP headers.
1. Build job: create the build session and scan. Keep buildSessionId as a build artifact.
2. Test environment: start the service with the test listener and a lab ID for this environment.
The preload script cannot turn on HTTP coloring (--httpServerColoring, --httpClientColoring). For per-test coverage, use slnodejs run.
3. Test job: open a test stage on the same lab, run the tests, close it.
Notes:
If the tests use a SeaLights test framework plugin (Jest, Playwright, Cypress, Cucumber.js), the plugin opens and closes the test stage and reports results; skip the
openTestStage,uploadReportsandcloseTestStagecommands and give the plugin the same lab ID. See Node.js Testing Plugins.--httpServerColoringattributes coverage to the test named in incoming request headers. Leave it out if the tests do not send them.Use the same lab ID for every replica of the service in the environment, and a different lab ID per environment.
Web application under end-to-end tests
Use when: a React, Angular, Vue or other web application is tested in a browser by Playwright, Cypress, Selenium, or manual testers.
What you get: browser coverage per test stage, and per test when the test framework plugin sets the current test in the browser.
Then run the tests with the same lab ID: through a SeaLights plugin, or between openTestStage --labId qa-env and closeTestStage --labId qa-env as in the previous configuration.
Notes:
Framework-specific source map settings, output folders and
--es6Modules: Framework and Bundler Setups.Never deploy
sl_distto production.To check the agent in the browser, open the developer tools console on a page of the test environment; see Setting Up Browser Coverage → Check that it works.
Several test environments: instrument once per environment with its own
--labIdand--outputPath, or set the lab ID at runtime; see Frontend Instrumentation → Multiple environments.If the page has a content security policy or the token must not reach the browser, see Browser Agent → Configuration.
Full stack: front end and back end
Use when: end-to-end tests drive a web application that calls an instrumented Node.js back end, and you want back-end coverage attributed to the same tests.
Each application has its own build session (its own config and scan). Both report on the same lab ID.
Front end: instrument with context propagation, listing the back-end origins (and the page's own origin).
Back end: accept the test identity from incoming requests.
Notes:
The back end must allow the
baggageheader in its CORS preflight response (Access-Control-Allow-Headers). See Context Propagation.If the back end calls other instrumented services, also add
--httpClientColoringto it.Pass
--allowCORStoscan --instrumentForBrowsers, not toinstrument:instrumentcurrently drops the list.
Containers and Kubernetes
Use when: the service under test runs in a container.
Kubernetes: mount the token from a secret.
Notes:
--forwardSignalspassesSIGTERMto the service when the container stops, so it shuts down cleanly. The preload script does not forward signals.If the image runs
npm ci --omit=dev, installslnodejsas a regular dependency.To keep the image start command unchanged, use the preload script instead: set
SL_tokenFile,SL_buildSessionIdFile,SL_labIdandNODE_OPTIONS=--require /app/node_modules/slnodejs/lib/preload.jsas container environment variables.Do not bake
sltoken.txtinto the image.
Monorepo with several modules
Use when: several packages or apps of one repository ship and are tested as one build.
Notes:
Module names must be unique within the build and stay the same between builds.
Add
--useModulePrefixwhen two modules contain files with the same relative path.If the packages are released and deployed independently, give each its own application (
config --appName) instead, and combine them with an integration build.
Low-overhead back-end coverage
Use when: the default agent makes the service run out of memory, start slowly, or slows down tests, and coverage per test stage is enough. Requires Node.js later than 20.6.0.
See Lightweight V8 Coverage Mode for limitations and shutdown behavior.
Pull request builds
Use when: a pipeline runs for each pull request.
Replace config with prConfig; the rest of the pipeline stays the same.
Behind a proxy, a Collector, or TLS inspection
Add these to any configuration above:
Last updated
Was this helpful?

