Skip to content

Mocha ​

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.

This guide explains how to integrate Mocha with TestBeats for centralized and AI-powered reporting.

Prerequisites ​

  • TestBeats API Key (optional) - Sign in for free with your Work Email at testbeats portal to have a centralized and AI powered reporting. Once you're signed in, create a new organization and generate an API Key.

  • In-coming Webhook URL - Create an incoming webhook url for your Slack or Microsoft Teams or Google Chat.

Integration ​

TestBeats can consume the following reports for publishing test results.

JSON Report ​

1. Configure Mocha to generate JSON reports ​

Run tests by updating the default reporter to json.

shell
mocha tests --reporter json

2. Configure TestBeats to consume JSON reports ​

Assume the generated test result file is result.json. Now the test results can be either published using the command line mode or the configuration mode.

2.1 Command Line Mode ​
  1. First, specify the target name using the appropriate target flag. For example, use the --slack flag to specify the incoming webhook URL.

  2. Next, specify the path to the result files using the appropriate results format flag. For example, use the --mocha flag for Mocha results.

sh
npx testbeats@latest publish --api-key '<api-key>' --slack '<slack-incoming-webhook-url>' --mocha '<path-to-mocha-report>'
sh
# Download the executable via `curl`.
curl https://raw.githubusercontent.com/test-results-reporter/testbeats/main/scripts/download-latest.sh | bash

# After download completes, run the executable based on you operating system.
./testbeats-linux publish --api-key '<api-key>' --slack '<slack-incoming-webhook-url>' --mocha '<path-to-mocha-report>'

TIP

  • TestBeats API Key is optional.
  • Environment variable TEST_BEATS_API_KEY can also be used to specify the API key.
2.2 Configuration Mode ​

Sample testbeats.config.json file

json
{
  "api_key": "<api-key>",
  "targets": [
    {
      "name": "slack",
      "inputs": {
        "url": "<incoming-webhook-url>"
      }
    }
  ],
  "results": [
    {
      "type": "mocha",
      "files": ["path/to/result.json"]
    }
  ]
}
sh
npx testbeats@latest publish -c path/to/testbeats.config.json
sh
# Download the executable via `curl`.
curl https://raw.githubusercontent.com/test-results-reporter/testbeats/main/scripts/download-latest.sh | bash

# After download completes, run the executable based on you operating system.
./testbeats-linux publish -c path/to/testbeats.config.json

JUnit Report ​

1. Configure Mocha to generate JUnit reports ​

Run tests by installing and updating the default reporter to junit.

shell
# install mocha-junit-reporter
npm install mocha-junit-reporter --save-dev

# run tests with mocha-junit-reporter
mocha test --reporter mocha-junit-reporter

2. Configure TestBeats to consume JSON reports ​

Assume the generated test result file is result.xml. Now the test results can be either published using the command line mode or the configuration mode.

2.1 Command Line Mode ​
  • First, specify the target name using the appropriate target flag. For example, use the --slack flag to specify the incoming webhook URL.

  • Next, specify the path to the result files using the appropriate results format flag. For example, use the --junit flag for Mocha results.

sh
npx testbeats@latest publish --api-key '<api-key>' --slack '<slack-incoming-webhook-url>' --junit '<path-to-junit-report>'
sh
# Download the executable via `curl`.
curl https://raw.githubusercontent.com/test-results-reporter/testbeats/main/scripts/download-latest.sh | bash

# After download completes, run the executable based on you operating system.
./testbeats-linux publish --api-key '<api-key>' --slack '<slack-incoming-webhook-url>' --junit '<path-to-junit-report>'

TIP

  • TestBeats API Key is optional.
  • Environment variable TEST_BEATS_API_KEY can also be used to specify the API key.
2.2 Configuration Mode ​

Sample testbeats.config.json file

json
{
  "api_key": "<api-key>",
  "targets": [
    {
      "name": "slack",
      "inputs": {
        "url": "<incoming-webhook-url>"
      }
    }
  ],
  "results": [
    {
      "type": "junit",
      "files": ["path/to/result.xml"]
    }
  ]
}
sh
npx testbeats@latest publish -c path/to/testbeats.config.json
sh
# Download the executable via `curl`.
curl https://raw.githubusercontent.com/test-results-reporter/testbeats/main/scripts/download-latest.sh | bash

# After download completes, run the executable based on you operating system.
./testbeats-linux publish -c path/to/testbeats.config.json

TIP

Enable multiple reporters by using one of the following packages

Example Integration ​

For a complete example of integrating Mocha with TestBeats, refer to this GitHub repository example.

Released under the MIT License.