-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-test-runner.js
More file actions
180 lines (145 loc) · 4.21 KB
/
web-test-runner.js
File metadata and controls
180 lines (145 loc) · 4.21 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import chalk from 'chalk';
import { escapeSpecialCharacters } from '../helpers/strings.cjs';
import { ReportBuilder } from '../helpers/report-builder.cjs';
import { SESSION_STATUS } from '@web/test-runner-core';
const { yellow, red, cyan, bold } = chalk;
class WebTestRunnerLogger {
info(message) { console.log(`\n${message}\n`); }
warning(message) { this.info(yellow(bold(message))); }
error(message) { this.info(red(bold(message))); }
location(message, location) { this.info(`${message}: ${cyan(bold(location))}`); }
}
const sanitizeName = (name) => {
return escapeSpecialCharacters(name).trim();
};
const makeDetailName = (prefix, testName) => {
return `${prefix}${sanitizeName(testName)}`;
};
const makeDetailId = (sessionId, file, name) => {
return `${sessionId}/${file}/${name}`;
};
const nullReporter = {
start() {},
stop() {}
};
export function reporter(options = {}) {
let overallStarted;
let testConfig;
const sessionStarts = new Map();
const logger = new WebTestRunnerLogger();
let report;
try {
report = new ReportBuilder('@web/test-runner', logger, options);
} catch ({ message }) {
logger.error('Failed to initialize D2L test report builder, report will not be generated');
logger.error(message);
return nullReporter;
}
const summary = report
.getSummary()
.addContext();
const collectTests = (session, prefix, tests) => {
const { id: sessionId, browser: { name: browserName }, testFile } = session;
const started = sessionStarts.get(sessionId) ?? (new Date()).toISOString();
if (report.ignoreFilePath(testFile)) {
return;
}
const browser = browserName.toLowerCase();
const { testsFinishTimeout } = testConfig;
for (const test of tests) {
const { skipped, passed, duration, name } = test;
const testName = makeDetailName(prefix, name);
const id = makeDetailId(sessionId, testFile, testName);
const detail = report
.getDetail(id)
.setName(testName)
.setLocationFile(testFile)
.setStarted(started)
.setBrowser(browser)
.setTimeout(testsFinishTimeout);
if (passed) {
detail.setPassed();
} else if (skipped) {
detail.setSkipped();
} else {
detail.setFailed();
}
if (duration !== undefined) {
detail.addDuration(duration);
} else {
detail
.setDurationFinal(0)
.setDurationTotal(0);
}
}
};
const collectSuite = (session, prefix, suite) => {
if (!suite) {
return;
}
collectTests(session, prefix, suite.tests);
for (const childSuite of suite.suites) {
const newPrefix = `${prefix}${sanitizeName(childSuite.name)} > `;
collectSuite(session, newPrefix, childSuite);
}
};
const gatherTestInfo = (sessions) => {
let overallPassed = true;
for (const session of sessions) {
const { passed, group: { name: groupName }, testResults } = session;
const isGroupName = groupName && testConfig.groups?.some(({ name }) => groupName === name);
const prefix = isGroupName ? `[${sanitizeName(groupName)}] > ` : '';
overallPassed &= passed;
collectSuite(session, prefix, testResults);
}
if (overallPassed) {
summary.setPassed();
} else {
summary.setFailed();
}
};
return {
name: 'd2l-test-reporting',
start({ config, sessions, startTime }) {
if (sessions.length === 0) {
return;
}
overallStarted = (new Date(startTime)).toISOString();
testConfig = config;
summary.setStarted(overallStarted);
},
onTestRunFinished({ sessions }) {
if (sessions.length === 0) {
return;
}
const started = new Date(overallStarted);
const ended = new Date();
const duration = Math.abs(ended - started);
summary.setDurationTotal(duration);
gatherTestInfo(sessions);
report.finalize();
},
getTestProgress({ sessions }) {
for (const session of sessions) {
const { id, status } = session;
switch (status) {
case SESSION_STATUS.SCHEDULED:
case SESSION_STATUS.INITIALIZING:
case SESSION_STATUS.TEST_STARTED:
sessionStarts.set(id, (new Date()).toISOString());
break;
case SESSION_STATUS.TEST_FINISHED:
case SESSION_STATUS.FINISHED:
default:
if (!sessionStarts.has(id)) {
sessionStarts.set(id, (new Date()).toISOString());
}
break;
}
}
},
stop() {
report.save();
}
};
}