Mocha / JavaScript

Testmo works with any test automation tool, including the popular Mocha JavaScript unit testing framework. This quickstart guide provides an overview of how to report JavaScript & Mocha test automation results to Testmo.

Mocha can generate JUnit-style XML files, which has become a standard format to exchange test results between tools. You just need to install the popular mocha-junit-reporter package. Then simply enable this reporter in your Mocha config and configure the filename and recommended settings:

.mocharc.js
"use strict";

module.exports = {
  reporter: 'mocha-junit-reporter',
  reporterOptions: [
    "mochaFile=results/test-results.xml",
    "jenkinsMode=1",
    "testCaseSwitchClassnameAndName=1",
    "outputs=1"
  ]
}
$ npx mocha

This will run your Mocha tests and automatically write all test results to an XML file in the results directory. You can learn more about available configuration options in the package documentation.

To submit your test results to Testmo, you simply use our cross-platform testmo CLI tool. The CLI tool is distributed as an NPM package and is easy to install on any system. Simply install our official @testmo/testmo-cli NPM package:

$ npm install -g @testmo/testmo-cli
$ testmo -h

Usage: testmo [options] [command]
[...]

We can now send the Mocha test results to Testmo. To do this, make sure to generate an API key in Testmo from your profile page. The API key is used to authenticate with Testmo to send the results. We can then use the testmo CLI tool so submit our results (note that we first set the TESTMO_TOKEN variable, which the tool expects):

$ export TESTMO_TOKEN=********
$ testmo automation:run:submit \
  --instance https://<your-name>.testmo.net \
  --project-id 1 \
  --name "Mocha test run" \
  --source "unittests" \
  --results results/*.xml
See command output
Collecting log files ..
Found 1 result file with a total of 855 tests
Created new automation run (ID: 254)
Created new thread (ID: 608)
Sending tests to Testmo ..
Uploading: [|||||||||||||||||||||||||] 100% | ETA: 0s | 855/855 tests
Successfully sent tests and completed run
Marked the run as completed  

That's it! 🎉 This will automatically analyze the XML result file, create a new test run in Testmo, submit all tests & results and mark the run as completed. There's no need to manually create any tests, map tests or IDs or build any custom API code. Everything is handled automatically for you.

➡️ Bonus: Launch Mocha with Testmo CLI

In the above example, we first launched Mocha to generate the XML file and then used our CLI tool to submit the results in a second step. As an improvement to the above example, we can ask our CLI tool to launch Mocha (testmo then starts and waits for Mocha to finish). This has the following additional benefits:

a) Capture full console output and send it to Testmo b) Accurately measure test times c) Record the Mocha exit code

Launch Mocha with CLI tool

We can ask our CLI tool to launch Mocha, so we can capture the console output and more:

$ export TESTMO_TOKEN=********
$ testmo automation:run:submit \
  --instance https://<your-name>.testmo.net \
  --project-id 1 \
  --name "Mocha test run" \
  --source "unittests" \
  --results results/*.xml \
  -- npx mocha
    ^ space!

Also learn more and see other examples in the full Testmo CLI guide.

➡️ Bonus: Custom fields

Testmo also supports additional custom fields for test runs, threads and tests. You can learn more here:

Mocha does not yet directly support fields/properties per test, but Testmo can also parse and read fields/properties from the test output. And Mocha lets you specify the output it should write to the results XML file. So we can use Testmo's output property syntax to record any additional custom fields for our tests and submit these as fields to Testmo. Testmo supports many automation field types including strings, links, rich HTML, steps and attachment links.

Add custom fields with Mocha

To log additional fields for a test, simply use Testmo's output property syntax with Mocha's consoleOutputs fields.

describe('user profile', function () {
  it ('should update user email', function () {
    // Initialize as array
    this.test.consoleOutputs = [];

    // Add simple properties to output
    this.test.consoleOutputs.push('[[PROPERTY|author=Adrian]]');
    this.test.consoleOutputs.push('[[PROPERTY|language=english]]');
    this.test.consoleOutputs.push('[[PROPERTY|url:github=https://github.com/project/code.js]]');
    
    // Testmo also supports automation steps, including statuses & 
    // sub fields. This example uses multi-line properties.
    this.test.consoleOutputs.push('[[PROPERTY|step[passed]]]\nThe first step\n[[/PROPERTY]]');
    this.test.consoleOutputs.push('[[PROPERTY|step[passed]]]\nThe second step\n[[/PROPERTY]]');
    this.test.consoleOutputs.push('[[PROPERTY|step[failed]]]\nThe third step\n[[/PROPERTY]]');

    // You can also include attachment links for test artifacts
    this.test.consoleOutputs.push('[[PROPERTY|attachment=https://ci-server/browser.log]]');
    this.test.consoleOutputs.push('[[PROPERTY|attachment=https://ci-server/screenshot.png]]');

    // [..]
  });  
});

You can learn more about Mocha's consoleOutput feature. And you can learn more about fields and types supported by Testmo.

Now that you are familiar with submitting your Mocha test results to Testmo, you might also find the following additional examples, topics and references useful for more advanced workflows.

Last updated