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:
Option A: JSON Reports (Recommended) ​
Run your tests with the JSON reporter:
mocha tests --reporter json > results/mocha-report.json
Option B: JUnit XML Reports ​
First, install the JUnit reporter:
npm install mocha-junit-reporter --save-dev
Then generate JUnit reports:
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:
npx testbeats@latest publish \
--api-key '<your-api-key>' \
--slack '<your-webhook-url>' \
--mocha 'results/mocha-report.json'
For JUnit reports:
npx testbeats@latest publish \
--api-key '<your-api-key>' \
--slack '<your-webhook-url>' \
--junit 'results/junit-report.xml'
Option B: Configuration File (Recommended) ​
Create a testbeats.config.json
:
For JSON reports:
{
"api_key": "<your-api-key>",
"targets": [
{
"name": "slack",
"inputs": {
"url": "<your-webhook-url>"
}
}
],
"results": [
{
"type": "mocha",
"files": ["results/mocha-report.json"]
}
]
}
For JUnit reports:
{
"api_key": "<your-api-key>",
"targets": [
{
"name": "slack",
"inputs": {
"url": "<your-webhook-url>"
}
}
],
"results": [
{
"type": "junit",
"files": ["results/junit-report.xml"]
}
]
}
Then run:
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 ​
- Configure additional targets for multiple communication channels
- Set up CI/CD integration for automated reporting
- Explore advanced extensions for enhanced reporting
- Learn about TestBeats Portal features
Need help with a different framework? Check Framework Integrations →