-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathrunner.js
132 lines (109 loc) · 2.8 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
128
129
130
131
132
'use strict';
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Promise = require('bluebird');
var optionChain = require('option-chain');
var matcher = require('matcher');
var TestCollection = require('./test-collection');
function noop() {}
var chainableMethods = {
spread: true,
defaults: {
type: 'test',
serial: false,
exclusive: false,
skipped: false,
todo: false,
callback: false
},
chainableMethods: {
test: {},
serial: {serial: true},
before: {type: 'before'},
after: {type: 'after'},
skip: {skipped: true},
todo: {todo: true},
only: {exclusive: true},
beforeEach: {type: 'beforeEach'},
afterEach: {type: 'afterEach'},
cb: {callback: true}
}
};
function Runner(options) {
if (!(this instanceof Runner)) {
throw new TypeError('Class constructor Runner cannot be invoked without \'new\'');
}
options = options || {};
EventEmitter.call(this);
this.tests = new TestCollection();
this._bail = options.bail;
this._serial = options.serial;
this._match = options.match || [];
this._addTestResult = this._addTestResult.bind(this);
}
util.inherits(Runner, EventEmitter);
module.exports = Runner;
optionChain(chainableMethods, function (opts, title, fn) {
if (typeof title === 'function') {
fn = title;
title = null;
}
if (opts.todo) {
fn = noop;
if (typeof title !== 'string') {
throw new TypeError('`todo` tests require a title');
}
} else if (typeof fn !== 'function') {
throw new TypeError('Expected a function. Use `test.todo()` for tests without a function.');
}
if (this._serial) {
opts.serial = true;
}
if (opts.type === 'test' && this._match.length > 0) {
opts.exclusive = title !== null && matcher([title], this._match).length === 1;
}
this.tests.add({
metadata: opts,
fn: fn,
title: title
});
}, Runner.prototype);
Runner.prototype._addTestResult = function (result) {
var test = result.result;
if (test.metadata.type === 'test') {
this.stats.testCount++;
if (test.metadata.todo) {
this.stats.todoCount++;
} else if (test.metadata.skipped) {
this.stats.skipCount++;
}
}
if (!result.passed) {
this.stats.failCount++;
}
var props = {
duration: test.duration,
title: test.title,
error: result.reason,
type: test.metadata.type,
skip: test.metadata.skipped,
todo: test.metadata.todo
};
this.emit('test', props);
};
Runner.prototype.run = function (options) {
var stats = this.stats = {
failCount: 0,
passCount: 0,
skipCount: 0,
todoCount: 0,
testCount: 0
};
if (options.runOnlyExclusive && !this.tests.hasExclusive) {
return Promise.resolve();
}
this.tests.on('test', this._addTestResult);
return Promise.resolve(this.tests.build(this._bail).run()).then(function () {
stats.passCount = stats.testCount - stats.failCount - stats.skipCount - stats.todoCount;
});
};