Integrating Probe Dock with Mocha

  • 11 October 2015

Mocha is one of the most popular JavaScript testing frameworks. We have integrated it with Probe Dock, so that test results can be collected and analyzed via our web interface.

Mocha provides a lot of very nice features, which make testing efficient and enjoyable. The framework allows you to test both server-side and client-side components. It supports asynchronous tests, custom reporters and much more.

To illustrate how Probe Dock can be integrated with existing testing frameworks, we have implemented a reference project where automated tests are written and executed with Mocha and where test results are sent to Probe Dock.

Have a look at this GitHub repo to see the result and get access to our Mocha runner. If you are using this framework, you will be able to collect your test results and feed them in your Probe Dock server.

What is our System-Under-Test?

In this demo, we consider two different systems that we want to validate with our Mocha tests:

  • the first one is a very simple Node.js module, which exposes two functions. We are going to check that the functions return values of the expected type.

  • the second one is a fictive distributed application (with a front-end, a back-end, etc.). There is actually no code at all: we only want to show that with Mocha, it is possible to recursively define test suites within test suites.

Here is the code of the node module that we will test:

/**
 * This is the first System-Under-Test for our demo. It is a 
 * very basic node module, which exposes two functions. They
 * both return a string value. The first function is synchronous,
 * the second one is asynchronous.
 */

exports.doSomething = function() {
	return "sweet";
}

exports.doSomethingAsynchronously = function ( done ) {
	setTimeout( function() {
		done("cool");
	}, 1000);
}

Write tests with Mocha

Writing tests with Mocha is easy. You declare test suites by calling the describe function. You then declare individual tests by calling the it function. In the callback function that you pass to it, you invoke the System-Under-Test (e.g. you make calls to your Node.js module) and then make assertions on the provided results.

Here is the test suite for validating the behavior of our simple Node.js module:

/**
 * Let's use the standard node.js module "assert" for making assertions. 
 * We could use a richer framework, such as should.js or chai.
 */
var assert = require("assert");


/**
 * Load the code that we want to test. It is a simple node module, 
 * which exposes two functions.
 */
var app = require("../app/index.js");


/**
 * Create a test suite for our "app" node module.
 */
describe("app", function() {

	/**
	 * Within the module test suite, create a test suite for 
	 * the "doSomething()" function
	 */
	describe("doSomething", function() {

		/**
		 * Define a first test for the function
		 */
		it("shouldReturnAString", function() {
			var result = app.doSomething();
			assert.equal('string', typeof result);
		});

		/**
		 * Define a second test for the function
		 */
		it("shouldReturnSweet", function() {
			var result = app.doSomething();
			assert.equal("sweet", result);
		});
	});

	/**
	 * Within the module test suite, create a test suite for the 
	 * "doSomethingAsynchronously()" function.
	 */
	describe("doSomethingAsynchronously", function() {
		it("shouldReturnCool", function(done) {
			this.timeout(5000);
			app.doSomethingAsynchronously(function(result) {
				assert.equal("cool", result);
				done();
			});
		});
	});
});

And here is the test suite for validating the behavior of our fictive distributed application:

/**
 * Let's use the standard node.js module "assert" for making assertions. We
 * could use a richer framework, such as should.js or chai.
 */
var assert = require("assert");


/**
 * Create a test suite for a distributed application, composed of several
 * sub-systems. This shows how test suites can be nested in multiple levels.
 */
describe("A distributed application", function() {

	describe("The backend sub-system", function() {

		describe("The REST API", function() {	
			it("should work", function() {});
		});

		describe("The business services", function() {	
			it("should work", function() {});
		});

		describe("The data access layer", function() {	
			it("should work", function() {
				throw Error("Technical error while talking to the database.");
			});
		});
	});

	describe("The front-end sub-system", function() {
		describe("The admin interface", function() {	
			describe("The monitoring pages", function() {	
				it("should work", function() {});
			});
			describe("The configuration pages", function() {	
				it("should work", function() {});
			});
		});
		describe("The end-user interface", function() {	
			describe("The account pages", function() {	
				it("should work", function() {
					var hasWorked = false;
					assert.equal(hasWorked, true);
				});
			});
			describe("The fun pages", function() {	
				it("should work", function() {});
			});
		});
	});

});

Configure your Probe Dock environment

To use Probe Dock, you can install your own server. You can also get a free trial account on our SaaS platform. You will find all details on this page. After registration, you will get credentials that you will need to store in a local configuration file (see below).

Once you have access to a server, you will also need to create a new Probe Dock project and get its API key.

Finally, you will need to make sure that two configuration files are properly setup on your machine. Firstly, in ~/.probedock/probedock.yml, you will need to enter the URL of your server and your user credentials. Secondly, in your project folder, you will need a probedock.yml file with the API key of your project.

Run the tests

To run the tests, use this command: node probedock-mocha-runner.js.

In our implementation, we have used the Mocha programmatic API to control the Mocha runner and be notified of test successes and failures. Once all tests have been executed, we send the results to the Probe Dock server with our probedock-node library. This is the npm module that you should use if you want to integrate Probe Dock in your development workflow and want to have a lot of control and flexibility.

When you run the command, you should see the following output on your console. Note that two tests are expected to fail (we generate exceptions on purpose).

image

See the test results in Probe Dock

If your setup is correct, you should see the test results in the Probe Dock web interface.

image

image

How do all pieces fit together?

You will find more details about the implementation on our GitHub repo, but the following diagram should already give you a pretty good idea about how Probe Dock, Mocha and your code base fit together.

image

Conclusion

Hopefully, this article and the companion GitHub repo have given you a good understanding about what needs to be done in order to post test results from a JavaScript environment. If you are using Mocha, then you can use our code either by invoking our runner script from the command line, or by integrating in your favorite build tool (e.g. Grunt, Gulp).

If you are using another test framework, it should not be too difficult to adapt our code to your needs. It is almost always about using the test framework to launch a runner, listen to test result events and ask the Probe Dock client to send the results to the server.

comments powered by Disqus