-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (42 loc) · 1.78 KB
/
index.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
'use strict';
const ConfigDecorator = require('./lib/config-decorator');
const parseOpts = require('./lib/plugin-opts');
const Limiter = require('./lib/limiter');
const logger = require('./lib/logger');
module.exports = (testplane, opts) => {
if (testplane.isWorker()) {
return;
}
opts = parseOpts(opts);
if (!opts.enabled) {
return;
}
const configDecorator = ConfigDecorator.create(testplane.config);
let limiter;
testplane.on(testplane.events.AFTER_TESTS_READ, (collection) => {
let totalTestsCount = 0;
collection.eachTest((test) => test.pending || test.disabled || ++totalTestsCount);
limiter = Limiter.create(totalTestsCount, {limit: opts.limit, timeLimit: opts.timeLimit});
});
if (Number.isFinite(opts.timeLimit)) {
logger.info(`will stop retrying tests after ${opts.timeLimit} seconds`);
testplane.once(testplane.events.TEST_BEGIN, () => limiter.startCountdown());
testplane.on(testplane.events.TEST_BEGIN, testBeginCallback);
}
testplane.on(testplane.events.RETRY, retryCallback);
if (Number.isFinite(opts.setRetriesOnTestFail)) {
logger.info(`will set retries to ${opts.setRetriesOnTestFail} after the first failed test`);
testplane.once(testplane.events.TEST_FAIL, () => configDecorator.setRetries(opts.setRetriesOnTestFail));
}
function testBeginCallback() {
limiter.exceedTimeLimit() && disableRetries();
}
function retryCallback() {
limiter.exceedRetriesLimit() && disableRetries();
}
function disableRetries() {
configDecorator.disableRetries();
testplane.removeListener(testplane.events.TEST_BEGIN, testBeginCallback);
testplane.removeListener(testplane.events.RETRY, retryCallback);
}
};