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

Unit Tests

Use this flow for unit tests that already produce an NYC (Istanbul) JSON coverage report and a JUnit XML report, for example Karma, AVA, or Jest without the SeaLights plugin. For Mocha, use slnodejs mocha instead: it collects coverage per test and applies Test Impact Analysis.

Unit tests are captured after the scan and before deployment. The test stage is identified by the build session ID; no lab ID is needed.

Lifecycle

1

Open the test stage

2

Run the unit tests, producing coverage-final.json and a JUnit XML report

3

Upload the NYC coverage report and the JUnit results

4

Close the test stage

Commands

printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt

npx slnodejs openTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --testStage "Unit Tests"

npm test   # writes coverage/coverage-final.json and reports/junit.xml

npx slnodejs nycReport --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --report coverage/coverage-final.json
npx slnodejs uploadReports --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --file reports/junit.xml

npx slnodejs closeTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --testStage "Unit Tests"
npx slnodejs openTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --testStage "Unit Tests"
npm test
npx slnodejs nycReport --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --report coverage\coverage-final.json
npx slnodejs uploadReports --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --file reports\junit.xml
npx slnodejs closeTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --testStage "Unit Tests"
- name: Open the SeaLights test stage
  run: npx slnodejs openTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --testStage "Unit Tests"
- name: Unit tests
  run: npm test
- name: Upload results and close the SeaLights test stage
  if: always()
  run: |
    npx slnodejs nycReport --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --report coverage/coverage-final.json
    npx slnodejs uploadReports --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --file reports/junit.xml
    npx slnodejs closeTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --testStage "Unit Tests"

Upload the results and close the test stage even when tests fail (if: always(), condition: always()), so failed tests are reported and the test stage does not stay open.

Producing the reports

  • Coverage: NYC (Istanbul) JSON reporter, which writes coverage-final.json. For example nyc --reporter=json, or Karma's karma-coverage with a json reporter. Jest writes it with --coverage --coverageReporters=json.

  • Results: a JUnit XML reporter for your framework.

Framework examples: Karma, AVA. To merge several NYC reports into one, see Merge multiple NYC coverage files into one.

Flags

Flag
Command
When to use

--testStage

openTestStage, closeTestStage

Name of the test stage, for example "Unit Tests".

--report

nycReport

Path of coverage-final.json (default: coverage/coverage-final.json in the working directory).

--projectRoot

nycReport

The tests ran in another directory than the scan (for example in a container), so the absolute paths in the report do not resolve. Set it to the project root in which the tests ran.

--file

uploadReports

JUnit XML report(s), comma-separated.

Before the first CI run, check that the NYC report matches the scan: npx slnodejs dryRun --scanDir src --hasNycReport --report coverage/coverage-final.json --verbose. See dryRun.

Last updated

Was this helpful?