1 var path = require('path');
2 var fs = require('fs');
3 var nodeunit = require('nodeunit');
5 var filepaths = fs.readdirSync('test').map(function(filename) {
6 return path.join('test', filename);
11 function sendMessage(message) {
12 process.stdout.write(JSON.stringify(message) + '\n');
15 // If an exception is thrown, let the parent process know and exit.
16 process.on('uncaughtException', function (e) {
17 sendMessage({error: [e.name, e.message, e.stack]});
21 // If Nodeunit explodes because a test was missing test.done(), handle it.
23 process.on('exit', function (e) {
24 var len = Object.keys(unfinished).length
26 sendMessage({exit: ['UNFINISHED']});
27 // process.reallyExit(len);
29 sendMessage({exit: ['finished']});
34 nodeunit.reporters.test = {
35 run: function(files, options, callback) {
36 // Nodeunit needs absolute paths.
37 var paths = files.map(function (filepath) {
38 return path.resolve(filepath);
40 nodeunit.runFiles(paths, {
43 // Executed when the first test in a file is run. If no tests exist in
44 // the file, this doesn't execute.
45 moduleStart: function(name) {
46 // Keep track of this so that moduleDone output can be suppressed in
47 // cases where a test file contains no tests.
49 // Send back to the parent process.
50 sendMessage({moduleStart: [name.toString()]});
52 // Executed after a file is done being processed. This executes whether
53 // tests exist in the file or not.
54 moduleDone: function(name) {
55 // Abort if no tests actually ran.
56 if (name !== currentModule) { return; }
57 // Send back to the parent process.
58 sendMessage({moduleDone: [name.toString()]});
60 // Executed before each test is run.
61 testStart: function(name) {
62 // Keep track of the current test, in case test.done() was omitted
63 // and Nodeunit explodes.
64 unfinished[name] = name;
65 // Send back to the parent process.
66 sendMessage({testStart: [name.toString()]});
68 // Executed after each test and all its assertions are run.
69 testDone: function(name, assertions) {
70 delete unfinished[name];
71 // Send back to the parent process.
72 sendMessage({testDone: [
74 assertions.failures(),
75 assertions.map(function(assertion) {
76 var e = assertion.error;
88 // Executed when everything is all done.
89 done: function (assertions) {
90 // Send back to the parent process.
92 assertions.failures(),
101 nodeunit.reporters.test.run(filepaths, {});