Skip to content

Instantly share code, notes, and snippets.

@mikehadlow
Created January 20, 2011 12:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehadlow/787815 to your computer and use it in GitHub Desktop.
Save mikehadlow/787815 to your computer and use it in GitHub Desktop.
A little javascript test runner for node.js
// run unit tests in node like this:
// node Suteki.TestRunner.js
//
// Unit tests should be named like this:
// Suteki.<name target>.Tests.js
//
// A test looks like this:
//
// (function(){
//
// exports.this_test_is_ok = function(assert) {
// assert(true, "should work fine?");
// };
//
// exports.this_test_will_fail = function(assert) {
// assert(false, "hi, I've failed");
// };
//
// exports.this_test_throws = function(assert) {
// throw new Error("Damn!");
// };
//
// }());
(function(){
var getFilesInCurrentDirectory = function(){
var fs = require('fs');
return fs.readdirSync('.');
};
var filterUnitTests = function(allFiles){
var i, fileName;
var unitTests = [];
for(i=0; i<allFiles.length; i++){
fileName = allFiles[i];
// identify test:
// Suteki.<name of target>.Tests.js
if(/Suteki\.([0-9\.\-_A-Za-z]+)\.Tests\.js/.test(fileName)){
// add the filename minus the .js extension
unitTests.push(fileName.replace(/\.js$/, ""));
}
}
return unitTests;
};
var runTest = function(testName){
var testFunction;
var unitTest = require("./" + testName);
var run = 0;
var succeeded = 0;
var failed = false;
console.log("");
console.log(testName);
console.log("-------------------------------------------------------");
if(unitTest){
for(testFunction in unitTest){
if(unitTest.hasOwnProperty(testFunction)){
run++;
failed = false;
console.log(">>> " + testFunction.replace(/_/g, " "));
try{
unitTest[testFunction](function(passed, message){
if(!passed){
console.log("FAILED " + (message || ""));
failed = true;
}
});
} catch(exception) {
failed = true;
console.log("FAILED with error: '" + exception.message + "'");
console.log(exception.stack);
}
if(!failed){
succeeded++;
}
}
}
console.log("Ran " + run + ", Failed " + (run - succeeded) + ", Passed " + succeeded);
}
else {
console.log("Could not load test '" + testName + "'")
}
return { ran:run, succeeded:succeeded };
};
// main
(function(){
var i, unitTest, testResult;
var unitTests = filterUnitTests(getFilesInCurrentDirectory());
var ran = 0;
var succeeded = 0;
for(i=0; i<unitTests.length; i++){
unitTest = unitTests[i];
testResult = runTest(unitTest);
ran = ran + testResult.ran;
succeeded = succeeded + testResult.succeeded;
}
console.log("");
console.log("Tests complete");
console.log("Fixtures: " + unitTests.length + ", Ran: " + ran + ", Failed: " + (ran - succeeded));
console.log("");
}());
}());
@mikehadlow
Copy link
Author

Output looks like this:

$ node Suteki.TestRunner.js

Suteki.EventBus.Tests
-------------------------------------------------------
>>> should be able to publish and subscribe
>>> should throw an exception
FAILED with error: 'Shit!'
Error: Shit!
    at Object.should_throw_an_exception (/cygdrive/d/Source/Suteki.NodePlay/js/Suteki.EventBus.Tests.js:38:
15)
    at /cygdrive/d/Source/Suteki.NodePlay/js/Suteki.TestRunner.js:49:46
    at /cygdrive/d/Source/Suteki.NodePlay/js/Suteki.TestRunner.js:85:26
    at /cygdrive/d/Source/Suteki.NodePlay/js/Suteki.TestRunner.js:94:5
    at Object.<anonymous> (/cygdrive/d/Source/Suteki.NodePlay/js/Suteki.TestRunner.js:96:1)
    at Module._compile (node.js:462:23)
    at Module._loadScriptSync (node.js:469:10)
    at Module.loadSync (node.js:338:12)
    at Object.runMain (node.js:522:24)
    at Array.0 (node.js:756:12)
>>> should be able to subscribe and unsubscribe
Ran 3, Failed 1, Passed 2

Tests complete
Fixtures: 1, Ran: 3, Failed: 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment