Running UnitJS Tests with Mocha and SpiraTest

September 1st, 2017 by Adam Sandman

automated testing

We announced today that we have just released a new plugin for SpiraTest - a custom reporter for the popular Mocha JavaScript test execution framework. We thought it would be helpful to provide more information around using the new plugin as well as some background on why we decided to develop it in the first place.

Why Have a Mocha Custom Reporter?

Several of our customers are using JavaScript test frameworks such as UnitJS, Mocha, Protractor, Jasmine, Chai to test their web applications in the browser as well as NodeJS server applications. These new frameworks generally follow a BDD / TDD based approach and allow you to write tests in JavaScript and run them on either a web browser or NodeJS. They also generally include support for different "reporters", allowing to customize the display of the test results.

Based on our experience with other unit testing frameworks (NUnit, jUnit, etc.) and based on demand from our customers (Dear Inflectra, how can I get by test scripts written in UnitJS to report back into SpiraTest like my jUnit ones) we decided it made sense to write a special SpiraTest custom reporter

How Do You Use the New Mocha Reporter?

As described in the SpiraTest unit testing integration guide, we designed our reporter for the case of running a test from UnitJS via. Mocha, but the reporter itself is only dependent on Mocha, so it can be used for any test framework compatible with Mocha, not just UnitJS.

To use the reporter, you need to add the following code to your Mocha test:

    var SpiraReporter = require('../reporter/SpiraReporter.js');

    //set the SpiraTest options
    global.spiraOptions = {
      projectId: 1,
      testCaseId: 4,
      releaseId: 1,
      testSetId: null,      
      login: 'fredbloggs',
      apiKey: '{7A05FD06-83C3-4436-B37F-51BCF0060483}',
      protocol: 'http',
      host: '127.0.0.1',
      vdir: 'spira'
    };   

This tells Mocha how to find the SpiraTest server. Then all you do is call Mocha as you would normally, but you pass the name of the Spira Reporter module as an extra parameter:

node ./node_modules/mocha/bin/mocha .\test\example2.js  --reporter .\reporter\SpiraReporter

When you run the test with this reporter, you get the results output to the console:

and in addition, a new test run reported back into SpiraTest:

and when you click on the test run, you will see:

We include the source code to the two modules:

  • SpiraClient.js - this lets you send results to SpiraTest using our REST API
  • SpiraReporter.js - this is the custom Mocha reporter

So that you can tailor the data being sent back to SpiraTest or use the SpiraClient.js module directly to log results from other frameworks.

What Do We Have Planned for the Future

Depending on the feedback from our customers, we are considering adding integration with other NodeJS frameworks such as:

  • Protractor
  • Chai
  • Jasmine

So please let us known (in the Comments) section below if there are other NodeJS frameworks you'd like us to integrate with.

Spira Helps You Deliver Quality Software, Faster and with Lower Risk.

Get Started with Spira for Free

And if you have any questions, please email or call us at +1 (202) 558-6885

Free Trial