# Mocha test framework

If your testing framework is mocha, SeaLights agent integrates directly with it and reports the test results to the server directly

{% hint style="info" %}
See '[Generating an Agent token](https://docs.sealights.io/knowledgebase/settings/token-access-and-management#token-list-and-token-creation-2)' for instructions on how to generate a token
{% endhint %}

Here is a sample of configuring use instead of the command 'mocha `--recursive test`'

{% tabs %}
{% tab title="Unix" %}
{% code overflow="wrap" lineNumbers="true" %}

```sh
npx slnodejs mocha --tokenfile /path/to/sltoken.txt --buildsessionidfile buildSessionId --teststage "Unit Tests" --useslnode2 -- --recursive test
```

{% endcode %}
{% endtab %}

{% tab title="Windows" %}
{% code overflow="wrap" lineNumbers="true" %}

```powershell
call npx slnodejs mocha --tokenfile \path\to\sltoken.txt --buildsessionidfile buildSessionId --teststage "Unit Tests" --useslnode2 -- --recursive test
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="warning" %}

* "`--recursive test`" are sample parameters passed to mocha and should be replaced by your own
* SeaLights runs as a mocha reporter, you should remove any parameters setting the reporter otherwise they will conflict
* Note the double dash delimiter (`--`) between the sealights options and parameters passed to mocha
  {% endhint %}

{% hint style="info" %}
See [#running-tests-using-mocha](https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/command-reference#running-tests-using-mocha "mention") for full parameter details
{% endhint %}

### Multiple Reporters Configuration <a href="#multiple-reporters-configuration" id="multiple-reporters-configuration"></a>

In case you’re already using a mocha reporter, like `allure-mocha` or `mochawesome`, it may conflict with the Sealights agent - which adds a reporter of its own (`SeaLightsReporter`) which passes the agent the test-events data.

A common solution is to configure the Multi-Reporters plugin:

1. Install [npm: mocha-multi-reporters](https://www.npmjs.com/package/mocha-multi-reporters)
2. Create a file `config.json` - with content similar to the one below (that allows `sealights`, `mocha-allure`, and `JUnit` reporters together) for example:<br>

   <pre class="language-json" data-overflow="wrap" data-line-numbers><code class="lang-json">{
       "reporterEnabled": "slnodejs/tsOutputs/mocha-reporter/index, allure-mocha, mocha-junit-reporter",
       "seaLightsReporterReporterOptions": {},
       "mochaJunitReporterReporterOptions": {
           "mochaFile": "results/junit-custom.xml"
       },
       "allureMochaReporterOptions": {
   		"resultsDir": "results/allure"
   	}
   }
   </code></pre>
3. When you run slnodejs mocha run it with the following additional parameters:

   <pre data-overflow="wrap" data-line-numbers><code>npx slnodejs mocha --tokenfile /path/to/sltoken.txt --buildsessionidfile buildSessionId ... -- --reporter-options configFile=config.json
   </code></pre>

### Mapping Functional Tests to Mocha Suites <a href="#mapping-functional-tests-to-mocha-suites" id="mapping-functional-tests-to-mocha-suites"></a>

In some functional testing configurations, a Mocha Suite may represent the overall functional test, while the Mocha tests within the suite correspond to individual test steps. For compatibility with this configuration, we allow you to pass the `reportSuites` flag (as a `reporterOption`) to map Sealights Tests to Mocha Suites instead of the Mocha tests themselves.

The flag is disabled by default (`false`), but when set to `true`, Sealights treats each suite as a single functional test, providing a better alignment with Coverage Analytics and Test Optimization.

{% code overflow="wrap" lineNumbers="true" %}

```
npx slnodejs mocha --tokenfile /path/to/sltoken.txt --buildsessionidfile buildSessionId ... -- --reporter-options reportSuites=true
```

{% endcode %}
