This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
index.js
118 lines (92 loc) Β· 3.82 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
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
'use strict';
const Promise = require('bluebird');
const _ = require('lodash');
const promiseUtils = require('q-promise-utils');
const pool = require('../browser-pool');
const BrowserRunner = require('./browser-runner');
const Events = require('../constants/events');
const Coverage = require('../coverage');
const Runner = require('./runner');
const SuiteMonitor = require('../suite-monitor');
module.exports = class TestsRunner extends Runner {
static create(config, stateProcessor) {
return new TestsRunner(config, stateProcessor);
}
constructor(config, stateProcessor) {
super();
this.config = config;
this.coverage = this.config.isCoverageEnabled() && Coverage.create(config);
this._stateProcessor = stateProcessor;
this._browserPool = pool.create(this.config, this);
this._suiteMonitor = SuiteMonitor.create(this);
this.passthroughEvent(this._suiteMonitor, Events.END_SUITE);
this._browserRunners = [];
}
run(suiteCollection) {
return Promise.resolve(this.emitAndWait(Events.START_RUNNER, this))
.then(() => this.emit(Events.BEGIN, this._formatBeginEventData(suiteCollection)))
.then(() => this._stateProcessor.prepare(this))
.then(() => !this._cancelled && this._runTests(suiteCollection))
.then(() => this.coverage && this.coverage.processStats())
.finally(() => {
this.emit(Events.END);
return this.emitAndWait(Events.END_RUNNER, this);
});
}
_formatBeginEventData(suiteCollection) {
return {
suiteCollection,
config: this.config,
totalStates: _.sumBy(suiteCollection.allSuites(), (suite) => suite.states.length),
browserIds: this.config.getBrowserIds()
};
}
_runTests(suiteCollection) {
this.emit(Events.BEGIN_SESSION);
return _(this._getBrowsersToRun())
.map((browserId) => this._runTestsInBrowser(suiteCollection, browserId))
.thru(promiseUtils.waitForResults)
.value()
.finally(() => this.emit(Events.END_SESSION));
}
_getBrowsersToRun() {
const allBrowsers = this.config.getBrowserIds();
const testBrowsers = this._testBrowsers;
return testBrowsers ? _.intersection(testBrowsers, allBrowsers) : allBrowsers;
}
_runTestsInBrowser(suiteCollection, browserId) {
const runner = BrowserRunner.create(browserId, this.config, this._browserPool);
this.passthroughEvent(runner, [
Events.RETRY,
Events.START_BROWSER,
Events.STOP_BROWSER,
Events.BEGIN_SUITE,
Events.SKIP_STATE,
Events.BEGIN_STATE,
Events.END_STATE,
Events.INFO,
Events.WARNING,
Events.ERROR
]);
runner.on(Events.END_SUITE, (data) => this._suiteMonitor.suiteFinished(data.suite, data.browserId));
runner.on(Events.CAPTURE, (result) => this._handleResult(result, Events.CAPTURE));
runner.on(Events.TEST_RESULT, (result) => this._handleResult(result, [Events.END_TEST, Events.TEST_RESULT]));
runner.on(Events.UPDATE_RESULT, (result) => this._handleResult(result, Events.UPDATE_RESULT));
this._browserRunners.push(runner);
return runner.run(suiteCollection, this._stateProcessor);
}
_handleResult(result, events) {
this._saveCoverage(result);
[].concat(events).forEach((event) => this.emit(event, result));
}
_saveCoverage(data) {
if (this.coverage) {
this.coverage.addStatsForBrowser(data.coverage, data.browserId);
}
}
cancel() {
this._cancelled = true;
this._browserRunners.forEach((runner) => runner.cancel());
this._browserPool.cancel();
}
};