-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathrunner.js
127 lines (106 loc) · 2.54 KB
/
runner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Promise = require('bluebird');
var Test = require('./test');
function Runner(opts) {
if (!(this instanceof Runner)) {
return new Runner(opts);
}
EventEmitter.call(this);
this.results = [];
this.stats = {
failCount: 0,
testCount: 0
};
this.tests = {
concurrent: [],
serial: [],
before: [],
after: []
};
this._assertModule = null;
}
util.inherits(Runner, EventEmitter);
module.exports = Runner;
Runner.prototype.addTest = function (title, cb) {
this.stats.testCount++;
this.tests.concurrent.push(new Test(title, this._assertModule, cb));
};
Runner.prototype.addSerialTest = function (title, cb) {
this.stats.testCount++;
this.tests.serial.push(new Test(title, this._assertModule, cb));
};
Runner.prototype.addBeforeHook = function (title, cb) {
this.tests.before.push(new Test(title, this._assertModule, cb));
};
Runner.prototype.addAfterHook = function (title, cb) {
this.tests.after.push(new Test(title, this._assertModule, cb));
};
Runner.prototype.concurrent = function (tests) {
var self = this;
// run all tests
return Promise.all(tests.map(function (test) {
return test.run()
.catch(function () {
// in case of error, don't reject a promise
return;
})
.then(function () {
self._addTestResult(test);
});
}));
};
Runner.prototype.serial = function (tests) {
var self = this;
return Promise.resolve(tests).each(function (test) {
return test.run()
.catch(function () {
return;
})
.then(function () {
self._addTestResult(test);
});
});
};
Runner.prototype._addTestResult = function (test) {
if (test.assertError) {
this.stats.failCount++;
}
this.results.push({
duration: test.duration,
title: test.title,
error: test.assertError
});
this.emit('test', test.assertError, test.title, test.duration);
};
Runner.prototype.run = function () {
var self = this;
var tests = this.tests;
var stats = this.stats;
return this.serial(tests.before)
.then(function () {
if (stats.failCount > 0) {
return Promise.reject();
}
})
.then(function () {
return self.serial(tests.serial);
})
.then(function () {
return self.concurrent(tests.concurrent);
})
.then(function () {
return self.serial(tests.after);
})
.catch(function () {
return;
})
.then(function () {
stats.passCount = stats.testCount - stats.failCount;
});
};
// Set custom assert module
Runner.prototype.setAssertModule = function (assertModule) {
this._assertModule = assertModule;
};