forked from codeceptjs/CodeceptJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrerun.js
81 lines (77 loc) · 2.38 KB
/
rerun.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
const fsPath = require('path');
const container = require('./container');
const event = require('./event');
const BaseCodecept = require('./codecept');
const output = require('./output');
class CodeceptRerunner extends BaseCodecept {
runOnce(test) {
return new Promise((resolve, reject) => {
// @ts-ignore
container.createMocha();
const mocha = container.mocha();
this.testFiles.forEach((file) => {
delete require.cache[file];
});
mocha.files = this.testFiles;
if (test) {
if (!fsPath.isAbsolute(test)) {
test = fsPath.join(global.codecept_dir, test);
}
mocha.files = mocha.files.filter(t => fsPath.basename(t, '.js') === test || t === test);
}
try {
mocha.run((failures) => {
if (failures === 0) {
resolve();
} else {
reject(new Error(`${failures} tests fail`));
}
});
} catch (e) {
reject(e);
}
});
}
async runTests(test) {
const configRerun = this.config.rerun || {};
const minSuccess = configRerun.minSuccess || 1;
const maxReruns = configRerun.maxReruns || 1;
if (minSuccess > maxReruns) throw new Error('minSuccess must be less than maxReruns');
if (maxReruns === 1) {
await this.runOnce(test);
return;
}
let successCounter = 0;
let rerunsCounter = 0;
while (rerunsCounter < maxReruns && successCounter < minSuccess) {
rerunsCounter++;
try {
await this.runOnce(test);
successCounter++;
output.success(`\nProcess run ${rerunsCounter} of max ${maxReruns}, success runs ${successCounter}/${minSuccess}\n`);
} catch (e) {
output.error(`\nFail run ${rerunsCounter} of max ${maxReruns}, success runs ${successCounter}/${minSuccess} \n`);
console.error(e);
}
}
if (successCounter < minSuccess) {
throw new Error(`Flaky tests detected! ${successCounter} success runs achieved instead of ${minSuccess} success runs expected`);
}
}
run(test) {
const done = () => {
event.emit(event.all.result, this);
event.emit(event.all.after, this);
};
event.emit(event.all.before, this);
return this.runTests(test)
.then(() => {
this.teardown(done);
})
.catch((e) => {
this.teardown(done);
throw e;
});
}
}
module.exports = CodeceptRerunner;