Skip to content

How to Integrate Mocha with TestBeats ​

This guide shows you how to send your Mocha test results to Slack, Microsoft Teams, or Google Chat using TestBeats.

What You'll Achieve ​

After following this guide, your Mocha tests will automatically send results to your team communication channels, complete with AI-powered failure analysis and detailed reporting.

Prerequisites ​

Before starting, ensure you have:

  • A Mocha test project
  • TestBeats installed (npx testbeats@latest --version)
  • A webhook URL for your communication platform (Slack, Teams, or Chat)
  • (Optional) TestBeats API key from app.testbeats.com

Step 1: Choose Your Report Format ​

Mocha supports multiple report formats. Choose the one that best fits your needs:

Run your tests with the JSON reporter:

bash
mocha tests --reporter json > results/mocha-report.json

Option B: JUnit XML Reports ​

First, install the JUnit reporter:

bash
npm install mocha-junit-reporter --save-dev

Then generate JUnit reports:

bash
mocha test --reporter mocha-junit-reporter --reporter-options mochaFile=results/junit-report.xml

Step 2: Choose Your Integration Method ​

You have two options to send results to TestBeats:

Option A: Command Line (Quick Setup) ​

For JSON reports:

bash
npx testbeats@latest publish \
  --api-key '<your-api-key>' \
  --slack '<your-webhook-url>' \
  --mocha 'results/mocha-report.json'

For JUnit reports:

bash
npx testbeats@latest publish \
  --api-key '<your-api-key>' \
  --slack '<your-webhook-url>' \
  --junit 'results/junit-report.xml'

Create a testbeats.config.json:

For JSON reports:

json
{
  "api_key": "<your-api-key>",
  "targets": [
    {
      "name": "slack",
      "inputs": {
        "url": "<your-webhook-url>"
      }
    }
  ],
  "results": [
    {
      "type": "mocha",
      "files": ["results/mocha-report.json"]
    }
  ]
}

For JUnit reports:

json
{
  "api_key": "<your-api-key>",
  "targets": [
    {
      "name": "slack",
      "inputs": {
        "url": "<your-webhook-url>"
      }
    }
  ],
  "results": [
    {
      "type": "junit",
      "files": ["results/junit-report.xml"]
    }
  ]
}

Then run:

bash
npx testbeats@latest publish -c testbeats.config.json

Step 3: Enable Multiple Reporters (Optional) ​

To maintain console output while generating reports, use multiple reporters:

Troubleshooting ​

Issue: No test results appearing

  • Verify the report file path is correct
  • Check that Mocha generated the report successfully
  • Ensure webhook URL is valid

Issue: API key not working

  • Verify API key in TestBeats Portal
  • Use environment variable TEST_BEATS_API_KEY instead

Example Integration ​

For a complete working example, see the Mocha TestBeats example repository.

Next Steps ​


Need help with a different framework? Check Framework Integrations →

Released under the MIT License.