> 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/karma-test-framework.md).

# Karma Test Framework

**Karma** runs tests in a browser, so SeaLights captures coverage from Karma's coverage reporter and test results from its JUnit reporter. You configure Karma to emit both reports, then drive the standard test-stage lifecycle (`start` → run → `nycReport` → `uploadReports` → `end`).

### Karma configuration <a href="#karma-configuration" id="karma-configuration"></a>

Add the JUnit and coverage reporters to `karma.conf.js`. This requires the `karma-junit-reporter` and `karma-coverage` npm packages.

```js
// karma.conf.js
module.exports = function (config) {
  config.set({
    reporters: ['progress', 'junit', 'coverage'],

    junitReporter: {
      outputDir: 'karma-reports',
      outputFile: 'junit.xml',
    },

    coverageReporter: {
      dir: 'karma-reports',
      reporters: [
        { type: 'json', subdir: '.', file: 'coverage-final.json' },
      ],
    },
  });
};
```

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

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

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

npx karma start   # runs your tests, producing karma-reports/coverage-final.json + junit.xml

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

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

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

{% endtab %}

{% tab title="GitHub Actions" %}

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

{% endtab %}

{% tab title="PowerShell" %}

```powershell
npx slnodejs start --tokenFile .\sltoken.txt --buildSessionIdFile buildSessionId --testStage "Unit Tests"
npx karma start
npx slnodejs nycReport --tokenFile .\sltoken.txt --buildSessionIdFile buildSessionId --report .\karma-reports\coverage-final.json
npx slnodejs uploadReports --tokenFile .\sltoken.txt --buildSessionIdFile buildSessionId --file .\karma-reports\junit.xml
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"
    npx karma start
    npx slnodejs nycReport    --token "$(SL_TOKEN)" --buildSessionIdFile buildSessionId --report ./karma-reports/coverage-final.json
    npx slnodejs uploadReports --token "$(SL_TOKEN)" --buildSessionIdFile buildSessionId --file ./karma-reports/junit.xml
    npx slnodejs end          --token "$(SL_TOKEN)" --buildSessionIdFile buildSessionId
  displayName: Karma tests with SeaLights
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Unit vs. functional:** the flow above captures **unit** coverage from Karma's coverage report. For **functional** tests, instrument the application first (see [Frontend Instrumentation](https://file+.vscode-resource.vscode-cdn.net/c%3A/Work/Customers/GEVernova/ADO/qTestUpdate/sealights-nodejs-docs/frontend-instrumentation.md)) and drive it with a Lab ID — footprints come from the running app rather than `nycReport`.&#x20;
{% endhint %}

{% hint style="info" %}
**`--projectRoot` for `nycReport`:** if SeaLights fails to match coverage to source files (common in CI/Docker environments where workspace paths differ from build paths), add `--projectRoot <path>` to the `nycReport` call. See [Unit Tests → Parameters](https://file+.vscode-resource.vscode-cdn.net/c%3A/Work/Customers/GEVernova/ADO/qTestUpdate/sealights-nodejs-docs/unit-tests.md#parameters) for details.&#x20;
{% endhint %}

***

**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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
