I've setup a suite of tests in mocha using ZombieJS and Chai. The tests load up a website and check if various services are booked in correctly and are displaying to visitors of the website.
What I'm aiming for is that the tests will run daily and then email the results to my team. The tests are all running as expected but the blockage I've hit is the following.
How do I pass the JSON reporter results to another node.js script where I can email the results. Building the email and sending it is going to be straight forward using nodemailer and underscore templating.
My current thinking is there are two approaches. Run the mocha test with a shell script and pipe the JSON output to a node script and process the JSON from a command line argument. Something like...
mocha test/services/homepage.js > node email.js
The other alternative is to run the tests from within a node script and get the returned result in a variable. I've been using information from here to run the tests within node.
https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically
This runs correctly but I'm lost with how to get the JSON reporter results into a variable from the below code.
var Mocha = require('mocha'), Suite = Mocha.Suite, Runner = Mocha.Runner, Test = Mocha.Test; // First, you need to instantiate a Mocha instance var mocha = new Mocha({ reporter: 'json' }); var suite = new Suite('JSON suite', 'root'); var runner = new Runner(suite); var mochaReporter = new mocha._reporter(runner); mocha.addFile( '/Users/dominic/Git/testing-rig/test/services/homepage.js' ); runner.run(function(failures) { // the json reporter gets a testResults JSON object on end var testResults = mochaReporter.testResults; console.log(testResults); // send your email here });
Here is an example of using Mocha programmatically: var Mocha = require('mocha'), fs = require('fs'), path = require('path'); // Instantiate a Mocha instance. var mocha = new Mocha(); var testDir = 'some/dir/test' // Add each . js file to the mocha instance fs.
Mocha is a feature-rich JavaScript test framework running on Node. js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.
You can listen to the runner events as in https://github.com/mochajs/mocha/blob/master/lib/runner.js#L40 and build your own report.
var Mocha = require('mocha'); var mocha = new Mocha({}); mocha.addFile('/Users/dominic/Git/testing-rig/test/services/homepage.js') mocha.run() .on('test', function(test) { console.log('Test started: '+test.title); }) .on('test end', function(test) { console.log('Test done: '+test.title); }) .on('pass', function(test) { console.log('Test passed'); console.log(test); }) .on('fail', function(test, err) { console.log('Test fail'); console.log(test); console.log(err); }) .on('end', function() { console.log('All done'); });
I'd suggest using a mocha reporter as explained here https://github.com/mochajs/mocha/wiki/Third-party-reporters
invoke mocha like this
MyReporter = require('./MyReporter'); mocha({ reporter: MyReporter })`
and the MyReporter.js
file will look like this
var mocha = require('mocha'); module.exports = MyReporter; function MyReporter(runner) { mocha.reporters.Base.call(this, runner); var passes = 0; var failures = 0; runner.on('pass', function(test){ passes++; console.log('pass: %s', test.fullTitle()); }); runner.on('fail', function(test, err){ failures++; console.log('fail: %s -- error: %s', test.fullTitle(), err.message); }); runner.on('end', function(){ console.log('end: %d/%d', passes, passes + failures); process.exit(failures); }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With