-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSupervisedTestsRunner.js
More file actions
70 lines (58 loc) · 1.81 KB
/
SupervisedTestsRunner.js
File metadata and controls
70 lines (58 loc) · 1.81 KB
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
const childProcess = require('child_process');
const filewatcher = require('filewatcher');
const fastGlobSync = require('fast-glob').sync;
const TestsRunner = require('./TestsRunner');
class SupervisedTestsRunner {
constructor(config) {
this.config = config;
this.testsRunner = new TestsRunner(config);
}
start() {
let result = this.config.project.build().then(() => this.startAllTests());
if (this.config.live) {
this.config.project.addListener('buildStart', () => {
this.testsRunner.stop();
});
this.config.project.addListener('buildFinish', () => {
this.startAllTests();
});
this.trackFileChanges();
} else {
result = result.then(code => { process.exit(code); });
}
return result;
}
trackFileChanges() {
const watcher = filewatcher();
fastGlobSync(this.config.testFiles).forEach(file => {
watcher.add(file);
});
watcher.on('change', file => {
console.log(`File changed: ${file}`);
this.calculateTestFiles().then(({ files }) => {
if (files.indexOf(file) > -1) {
this.testsRunner.runTestFiles([file]);
}
});
});
}
startAllTests() {
return this.calculateTestFiles().then(async ({ files, filterEnabled }) => {
const testsForRun = this.config.filterTestsFiles
? await this.config.filterTestsFiles(files, filterEnabled, this.config)
: files;
return this.testsRunner.runTestFiles(testsForRun);
});
}
calculateTestFiles() {
const calc = childProcess.fork(`${__dirname}/TestFilesCalculator.js`);
return new Promise(resolve => {
calc.on('message', msg => {
calc.kill();
resolve(msg.result);
});
calc.send({ filesGlob: this.config.testFiles });
});
}
}
module.exports = SupervisedTestsRunner;