> For the complete documentation index, see [llms.txt](https://docs.sealights.io/knowledgebase/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/node.js-agent/capturing-tests/unit-tests.md).

# Unit Tests

Unit tests are captured **after** [**scanning**](https://file+.vscode-resource.vscode-cdn.net/c%3A/Work/Customers/GEVernova/ADO/qTestUpdate/sealights-nodejs-docs/scanning-your-application.md) **but before deployment**. The agent opens a test stage, you run your tests with coverage (NYC / Istanbul), and then you upload both the JUnit results and the coverage report.

{% hint style="info" %}
For unit tests, identify the execution by **Build Session ID** (`--buildSessionIdFile` / `--buildSessionId`) rather than a Lab ID.&#x20;
{% endhint %}

### Lifecycle <a href="#lifecycle" id="lifecycle"></a>

{% stepper %}
{% step %}

#### Start the test stage <a href="#start-the-test-stage" id="start-the-test-stage"></a>

{% endstep %}

{% step %}

### Run your unit tests (producing NYC coverage + JUnit XML)

{% endstep %}

{% step %}

### Upload JUnit results and the NYC coverage report

{% endstep %}

{% step %}

### End the test stage

{% endstep %}
{% endstepper %}

### Commands <a href="#commands" id="commands"></a>

{% tabs %}
{% tab title="Bash" %}

```sh
npx slnodejs start \
  --tokenFile ./sltoken.txt --buildSessionIdFile buildSessionId \
  --testStage "Unit Tests"

npm test   # runs your tests with nyc, producing coverage-final.json + junit.xml

npx slnodejs uploadReports \
  --tokenFile ./sltoken.txt --buildSessionIdFile buildSessionId \
  --file ./reports/junit.xml

npx slnodejs nycReport \
  --tokenFile ./sltoken.txt --buildSessionIdFile buildSessionId \
  --report ./coverage/coverage-final.json

npx slnodejs end \
  --tokenFile ./sltoken.txt --buildSessionIdFile buildSessionId
```

{% endtab %}

{% tab title="GitHub Actions" %}

```yaml
- name: Capture unit tests
  env:
    SL_TOKEN: ${{ secrets.SL_TOKEN }}
  run: |
    npx slnodejs start        --token "$SL_TOKEN" --buildSessionIdFile buildSessionId --testStage "Unit Tests"
    npm test
    npx slnodejs uploadReports --token "$SL_TOKEN" --buildSessionIdFile buildSessionId --file ./reports/junit.xml
    npx slnodejs nycReport    --token "$SL_TOKEN" --buildSessionIdFile buildSessionId --report ./coverage/coverage-final.json
    npx slnodejs end          --token "$SL_TOKEN" --buildSessionIdFile buildSessionId
```

{% endtab %}

{% tab title="PowerShell" %}

```powershell
npx slnodejs start --tokenFile .\sltoken.txt --buildSessionIdFile buildSessionId --testStage "Unit Tests"
npm test
npx slnodejs uploadReports --tokenFile .\sltoken.txt --buildSessionIdFile buildSessionId --file .\reports\junit.xml
npx slnodejs nycReport --tokenFile .\sltoken.txt --buildSessionIdFile buildSessionId --report .\coverage\coverage-final.json
npx slnodejs end --tokenFile .\sltoken.txt --buildSessionIdFile buildSessionId
```

{% endtab %}

{% tab title="Azure DevOps" %}

```yaml
- script: |
    npx slnodejs start        --token "$(SL_TOKEN)" --buildSessionIdFile buildSessionId --testStage "Unit Tests"
    npm test
    npx slnodejs uploadReports --token "$(SL_TOKEN)" --buildSessionIdFile buildSessionId --file ./reports/junit.xml
    npx slnodejs nycReport    --token "$(SL_TOKEN)" --buildSessionIdFile buildSessionId --report ./coverage/coverage-final.json
    npx slnodejs end          --token "$(SL_TOKEN)" --buildSessionIdFile buildSessionId
  displayName: Capture unit tests
```

{% endtab %}
{% endtabs %}

### Parameters <a href="#parameters" id="parameters"></a>

| Parameter                                   | Purpose                                                            |
| ------------------------------------------- | ------------------------------------------------------------------ |
| `--tokenFile` / `--token`                   | SeaLights authentication                                           |
| `--buildSessionIdFile` / `--buildSessionId` | Build session identifier (preferred over Lab ID for unit tests)    |
| `--testStage`                               | Stage name (e.g. `"Unit Tests"`)                                   |
| `--file`                                    | Path to the JUnit XML results (`uploadReports`)                    |
| `--report`                                  | Path to the NYC coverage file, `coverage-final.json` (`nycReport`) |
| `--projectRoot`                             | *(nycReport, optional)* Project root directory — see note below    |

{% hint style="info" %}
&#x20;**`--projectRoot` for `nycReport`:**&#x20;

NYC embeds absolute file paths in `coverage-final.json`. In most local environments these paths already resolve correctly. In CI/Docker environments — where the workspace path may differ from where the code was built — SeaLights may fail to match coverage data to source files.&#x20;

If that happens, add `--projectRoot <path>` (e.g. `--projectRoot ./` or `--projectRoot $(pwd)`) so the agent can normalize the paths in the report.
{% endhint %}

### Framework-specific guides <a href="#framework-specific-guides" id="framework-specific-guides"></a>

The exact way to produce `coverage-final.json` and `junit.xml` depends on your runner:

* [Mocha](https://file+.vscode-resource.vscode-cdn.net/c%3A/Work/Customers/GEVernova/ADO/qTestUpdate/sealights-nodejs-docs/mocha.md)
* [Karma](https://file+.vscode-resource.vscode-cdn.net/c%3A/Work/Customers/GEVernova/ADO/qTestUpdate/sealights-nodejs-docs/karma.md)
* [AVA](https://file+.vscode-resource.vscode-cdn.net/c%3A/Work/Customers/GEVernova/ADO/qTestUpdate/sealights-nodejs-docs/ava.md)

***

**Back to →** [Capturing Tests](https://file+.vscode-resource.vscode-cdn.net/c%3A/Work/Customers/GEVernova/ADO/qTestUpdate/sealights-nodejs-docs/capturing-tests.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/node.js-agent/capturing-tests/unit-tests.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
