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
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 examplenyc --reporter=json, or Karma'skarma-coveragewith ajsonreporter. 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
--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.
Last updated
Was this helpful?

