Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using mocha testing with cloud9, execute mocha tests from node.js

I was wondering if there is a way to execute mocha tests programmatically from node.js so that I can integrate unit tests with Cloud 9. The cloud 9 IDE has a nice feature where whenever a javascript files is saved, it looks for a file with the same name, ending with either "_test" or "Test" and runs it automatically using node.js. For example it has this code snippet in a file demo_test.js which automatically runs.

if (typeof module !== "undefined" && module === require.main) {
    require("asyncjs").test.testcase(module.exports).exec()
}

Is there something like this I could use to run a mocha test? Something like a mocha(this).run()?

like image 737
MonkeyBonkey Avatar asked Feb 03 '12 13:02

MonkeyBonkey


2 Answers

The essentials to programmatically run mocha:

Require mocha:

var Mocha = require('./'); //The root mocha path (wherever you git cloned 
                               //or if you used npm in node_modules/mocha)

Instatiate call the constructor:

var mocha = new Mocha();

Add test files:

mocha.addFile('test/exampleTest');  // direct mocha to exampleTest.js

Run it!:

mocha.run();

Add chained functions to programmatically deal with passed and failed tests. In this case add a call back to print the results:

var Mocha = require('./'); //The root mocha path 

var mocha = new Mocha();

var passed = [];
var failed = [];

mocha.addFile('test/exampleTest'); // direct mocha to exampleTest.js

mocha.run(function(){

    console.log(passed.length + ' Tests Passed');
    passed.forEach(function(testName){
        console.log('Passed:', testName);
    });

    console.log("\n"+failed.length + ' Tests Failed');
    failed.forEach(function(testName){
        console.log('Failed:', testName);
    });

}).on('fail', function(test){
    failed.push(test.title);
}).on('pass', function(test){
    passed.push(test.title);
});
like image 66
Shwaydogg Avatar answered Sep 21 '22 13:09

Shwaydogg


Your mileage may vary, but I concocted the following one-liner a while back and it has served me pretty well:

if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);

Additionally, if you want it to be output in asyncjs format that Cloud9 is expecting, you'll need to provide a special reporter. Here's a really simple example of what a simple reporter would look like:

if (!module.parent){
    (new(require("mocha"))()).ui("exports").reporter(function(r){
        var i = 1, n = r.grepTotal(r.suite);
        r.on("fail", function(t){ console.log("\x1b[31m[%d/%d] %s FAIL\x1b[0m", i++, n, t.fullTitle()); });
        r.on("pass", function(t){ console.log("\x1b[32m[%d/%d] %s OK\x1b[0m", i++, n, t.fullTitle()); });
        r.on("pending", function(t){ console.log("\x1b[33m[%d/%d] %s SKIP\x1b[0m", i++, n, t.fullTitle()); });
    }).addFile(__filename).run(process.exit);
}
like image 37
KylePDavis Avatar answered Sep 19 '22 13:09

KylePDavis