For the complete documentation index, see llms.txt. This page is also available as Markdown.

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:

  • slnodejs is 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.

slnodejs commands exit with status code 0 even when they fail, so SeaLights never breaks your pipeline. The examples add --failOnError true to config and scan, so that a build without a scan is noticed. Remove it if you prefer the default.

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 --ok
jobs:
  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 --ok

Notes:

  • Scan the files that the tests load. For TypeScript run through ts-node or a Mocha TypeScript loader, scan src and set SL_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 --es6Modules to scan and mocha for 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> to nycReport.

  • Framework-specific report setup: Karma, AVA.

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, uploadReports and closeTestStage commands and give the plugin the same lab ID. See Node.js Testing Plugins.

  • --httpServerColoring attributes 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:

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 baggage header in its CORS preflight response (Access-Control-Allow-Headers). See Context Propagation.

  • If the back end calls other instrumented services, also add --httpClientColoring to it.

  • Pass --allowCORS to scan --instrumentForBrowsers, not to instrument: instrument currently drops the list.

Containers and Kubernetes

Use when: the service under test runs in a container.

Kubernetes: mount the token from a secret.

Notes:

  • --forwardSignals passes SIGTERM to 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, install slnodejs as a regular dependency.

  • To keep the image start command unchanged, use the preload script instead: set SL_tokenFile, SL_buildSessionIdFile, SL_labId and NODE_OPTIONS=--require /app/node_modules/slnodejs/lib/preload.js as container environment variables.

  • Do not bake sltoken.txt into 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 --useModulePrefix when 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?