UNPKG

1.04 kBJavaScriptView Raw
1'use strict';
2
3var EventEmitter = require('events').EventEmitter;
4
5function Runner() {
6 this._queue = [];
7}
8
9Runner.prototype = Object.create(EventEmitter.prototype);
10Runner.prototype.constructor = Runner;
11
12Runner.prototype.errorCount = 0;
13
14Runner.prototype._countErrors = function _countErrors(node) {
15 var self = this;
16 node.on('assertNotOk', function () { self.errorCount++; });
17 node.on('childTest', _countErrors.bind(this));
18};
19
20Runner.prototype.run = function run() {
21 if (this._currentTest) return;
22
23 var self = this;
24
25 if (this._queue.length === 0)
26 this._done();
27 else {
28 var node = this._queue.shift();
29 this.emit('test', node);
30
31 node.once('treeDone', function () {
32 self._currentTest = null;
33 run.call(self)
34 });
35
36 this._countErrors(node);
37
38 this._currentTest = node;
39 node.run();
40 }
41};
42
43Runner.prototype.enqueue = function (node) {
44 this._queue.push(node);
45};
46
47Runner.prototype._done = function () {
48 this.emit('done');
49
50 process.exit(this.errorCount);
51};
52
53module.exports = Runner;
\No newline at end of file