-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
232 lines (182 loc) · 4.64 KB
/
api.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
'use strict';
var EventEmitter = require('events').EventEmitter;
var path = require('path');
var util = require('util');
var fs = require('fs');
var flatten = require('arr-flatten');
var Promise = require('bluebird');
var figures = require('figures');
var assign = require('object-assign');
var globby = require('globby');
var chalk = require('chalk');
var fork = require('./lib/fork');
function Api(files, options) {
if (!(this instanceof Api)) {
return new Api(files, options);
}
EventEmitter.call(this);
assign(this, options);
this.rejectionCount = 0;
this.exceptionCount = 0;
this.passCount = 0;
this.failCount = 0;
this.fileCount = 0;
this.testCount = 0;
this.errors = [];
this.stats = [];
this.tests = [];
this.files = files || [];
Object.keys(Api.prototype).forEach(function (key) {
this[key] = this[key].bind(this);
}, this);
}
util.inherits(Api, EventEmitter);
module.exports = Api;
Api.prototype._runFile = function (file) {
var args = [file];
if (this.failFast) {
args.push('--fail-fast');
}
if (this.serial) {
args.push('--serial');
}
// Forward the `time-require` `--sorted` flag.
// Intended for internal optimization tests only.
if (this._sorted) {
args.push('--sorted');
}
return fork(args)
.on('stats', this._handleStats)
.on('test', this._handleTest)
.on('unhandledRejections', this._handleRejections)
.on('uncaughtException', this._handleExceptions);
};
Api.prototype._handleRejections = function (data) {
this.rejectionCount += data.rejections.length;
data.rejections.forEach(function (err) {
err.type = 'rejection';
err.file = data.file;
this.emit('error', err);
this.errors.push(err);
}, this);
};
Api.prototype._handleExceptions = function (data) {
this.exceptionCount++;
var err = data.exception;
err.type = 'exception';
err.file = data.file;
this.emit('error', err);
this.errors.push(err);
};
Api.prototype._handleStats = function (stats) {
this.testCount += stats.testCount;
};
Api.prototype._handleTest = function (test) {
test.title = this._prefixTitle(test.file) + test.title;
var isError = test.error.message;
if (isError) {
this.errors.push(test);
} else {
test.error = null;
}
this.emit('test', test);
};
Api.prototype._prefixTitle = function (file) {
if (this.fileCount === 1) {
return '';
}
var separator = ' ' + chalk.gray.dim(figures.pointerSmall) + ' ';
var base = path.dirname(this.files[0]);
if (base === '.') {
base = this.files[0] || 'test';
}
base += path.sep;
var prefix = path.relative('.', file)
.replace(base, '')
.replace(/\.spec/, '')
.replace(/test\-/g, '')
.replace(/\.js$/, '')
.split(path.sep)
.join(separator);
if (prefix.length > 0) {
prefix += separator;
}
return prefix;
};
Api.prototype.run = function () {
var self = this;
return handlePaths(this.files)
.map(function (file) {
return path.resolve(file);
})
.then(function (files) {
if (files.length === 0) {
return Promise.reject(new Error('Couldn\'t find any files to test'));
}
self.fileCount = files.length;
var tests = files.map(self._runFile);
// receive test count from all files and then run the tests
var statsCount = 0;
var deferred = Promise.pending();
tests.forEach(function (test) {
var counted = false;
function tryRun() {
if (counted) {
return;
}
if (++statsCount === self.fileCount) {
self.emit('ready');
var method = self.serial ? 'mapSeries' : 'map';
deferred.resolve(Promise[method](files, function (file, index) {
return tests[index].run();
}));
}
}
test.on('stats', tryRun);
test.catch(tryRun);
});
return deferred.promise;
})
.then(function (results) {
// assemble stats from all tests
self.stats = results.map(function (result) {
return result.stats;
});
self.tests = results.map(function (result) {
return result.tests;
});
self.tests = flatten(self.tests);
self.passCount = sum(self.stats, 'passCount');
self.failCount = sum(self.stats, 'failCount');
});
};
function handlePaths(files) {
if (files.length === 0) {
files = [
'test.js',
'test-*.js',
'test/*.js'
];
}
files.push('!**/node_modules/**');
// convert pinkie-promise to Bluebird promise
files = Promise.resolve(globby(files));
return files
.map(function (file) {
if (fs.statSync(file).isDirectory()) {
return handlePaths([path.join(file, '*.js')]);
}
return file;
})
.then(flatten)
.filter(function (file) {
return path.extname(file) === '.js' && path.basename(file)[0] !== '_';
});
}
function sum(arr, key) {
var result = 0;
arr.forEach(function (item) {
result += item[key];
});
return result;
}