/
api.js
98 lines (79 loc) · 2.49 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
'use strict';
import Mocha from 'mocha';
import Reporter from './lib/reporter';
import {createInstance as createRunnerInstance} from './lib/runner';
import {
addTest,
runTests,
setOptions as setWatcherOptions
} from './lib/watcher';
class MochaParallelTests extends Mocha {
constructor() {
super();
this._customRunner = createRunnerInstance();
this._filesTotal = 0;
this._reporterName = null;
this._reporterOptions = null;
// prevent uncaught exception listeners count warning
process.setMaxListeners(0);
}
addFile(file) {
addTest(file);
this._filesTotal++;
return this;
}
reporter(reporterName, reporterOptions) {
if (reporterName === undefined) {
return super.reporter.call(this, reporterName, reporterOptions);
}
this._reporterName = reporterName;
this._reporterOptions = reporterOptions;
return this;
}
setOwnOptions({maxParallel, retry}) {
setWatcherOptions({
maxParallelTests: maxParallel,
retryCount: retry
});
}
run(callback) {
this._customRunner.on('end', ({failsOccured, reporter}) => {
if (reporter.done) {
reporter.done(failsOccured, callback);
} else {
callback(failsOccured);
}
});
runTests({
options: Object.assign({}, {
reporterName: this._reporterName,
reporterOptions: this._reporterOptions,
reporter: Reporter,
testsLength: this._filesTotal
}),
throttledCalls: this._throttledCalls
});
return this._customRunner;
}
}
Object.keys(Mocha.prototype).forEach(key => {
if (typeof Mocha.prototype[key] !== 'function') {
return;
}
// we have our own implementations of these methods
// other methods should be saved and re-applied during runTests()
if (key === 'run' || key === 'addFile' || key === 'reporter') {
return;
}
MochaParallelTests.prototype[key] = function (...args) {
// mocha calls some of its methods inside constructor
// so MochaParallelTests own constructor function can still be in progress here
this._throttledCalls = this._throttledCalls || [];
this._throttledCalls.push({
args,
method: key
});
return this;
};
});
export default MochaParallelTests;