| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | var path = require('path'); |
| 2 | var fs = require('fs'); |
| 3 | var nodeunit = require('nodeunit'); |
| 4 | |
| 5 | var filepaths = fs.readdirSync('test').map(function(filename) { |
| 6 | return path.join('test', filename); |
| 7 | }); |
| 8 | |
| 9 | var unfinished = {}; |
| 10 | var currentModule; |
| 11 | function sendMessage(message) { |
| 12 | process.stdout.write(JSON.stringify(message) + '\n'); |
| 13 | } |
| 14 | |
| 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]}); |
| 18 | process.exit(); |
| 19 | }); |
| 20 | |
| 21 | // If Nodeunit explodes because a test was missing test.done(), handle it. |
| 22 | var unfinished = {}; |
| 23 | process.on('exit', function (e) { |
| 24 | var len = Object.keys(unfinished).length |
| 25 | if (len > 0) { |
| 26 | sendMessage({exit: ['UNFINISHED']}); |
| 27 | // process.reallyExit(len); |
| 28 | } else { |
| 29 | sendMessage({exit: ['finished']}); |
| 30 | } |
| 31 | // process.exit(); |
| 32 | }); |
| 33 | |
| 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); |
| 39 | }); |
| 40 | nodeunit.runFiles(paths, { |
| 41 | // No idea. |
| 42 | testspec: undefined, |
| 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. |
| 48 | currentModule = name; |
| 49 | // Send back to the parent process. |
| 50 | sendMessage({moduleStart: [name.toString()]}); |
| 51 | }, |
| 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()]}); |
| 59 | }, |
| 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()]}); |
| 67 | }, |
| 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: [ |
| 73 | name.toString(), |
| 74 | assertions.failures(), |
| 75 | assertions.map(function(assertion) { |
| 76 | var e = assertion.error; |
| 77 | if (e) { |
| 78 | assertion.error = { |
| 79 | name: e.name, |
| 80 | message: e.message, |
| 81 | stack: e.stack |
| 82 | }; |
| 83 | } |
| 84 | return assertion; |
| 85 | }) |
| 86 | ]}); |
| 87 | }, |
| 88 | // Executed when everything is all done. |
| 89 | done: function (assertions) { |
| 90 | // Send back to the parent process. |
| 91 | sendMessage({done: [ |
| 92 | assertions.failures(), |
| 93 | assertions.duration, |
| 94 | assertions |
| 95 | ]}); |
| 96 | } |
| 97 | }); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | nodeunit.reporters.test.run(filepaths, {}); |