-
Notifications
You must be signed in to change notification settings - Fork 81
/
web-test-runner.config.js
214 lines (201 loc) · 6.73 KB
/
web-test-runner.config.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { browserstackLauncher } from '@web/test-runner-browserstack';
import { defaultReporter } from '@web/test-runner';
import { puppeteerLauncher } from '@web/test-runner-puppeteer';
import { visualRegressionPlugin } from '@web/test-runner-visual-regression/plugin';
import isDocker from 'is-docker';
import snowpackWebTestRunner from '@snowpack/web-test-runner-plugin';
// Set NODE_ENV to test to ensure snowpack builds in test mode.
process.env.NODE_ENV = 'test';
/**
* Test result reporter which supports detailed output of chai/jasmine/etc test
* results. Inspired by code here:
* https://github.com/modernweb-dev/web/issues/229#issuecomment-732005741
*/
class SpecReporter {
constructor() {
// TODO(https://github.com/eslint/eslint/issues/14343): Change to class field when eslint supports it.
this.color = {
reset: '\x1b[0m',
cyan: '\x1b[36m',
red: '\x1b[31m',
green: '\x1b[32m',
dim: '\x1b[2m',
yellow: '\x1b[33m',
};
}
/**
* @param {import('@web/test-runner').TestSuiteResult} suite
* @param indent
* @returns
*/
outputSuite(suite, indent = '') {
if (suite === undefined) {
return 'Suite is undefined; top level error';
}
let results = `${indent}${suite.name}\n`;
results +=
suite.tests
.map((test) => {
let result = indent;
if (test.skipped) {
result += `${this.color.cyan} - ${test.name}`;
} else if (test.passed) {
result += `${this.color.green} ✓ ${this.color.reset}${this.color.dim}${test.name}`;
} else {
if (test.error === undefined) {
test.error = {};
test.error.message = 'Test failed with no error message';
test.error.stack = '<no stack trace>';
}
result += `${this.color.red} ${test.name}\n\n${test.error.message}\n${test.error.stack}`;
}
result +=
test.duration > 100
? ` ${this.color.reset}${this.color.red}(${test.duration}ms)`
: test.duration > 50
? ` ${this.color.reset}${this.color.yellow}(${test.duration}ms)`
: '';
result += `${this.color.reset}`;
return result;
})
.join('\n') + '\n';
if (suite.suites) {
results += suite.suites
.map((suite) => this.outputSuite(suite, indent + ' '))
.join('\n');
}
return results;
}
/**
* @param testFile
* @param {import('@web/test-runner').TestSession[]} sessionsForTestFile
* @returns
*/
async generateTestReport(testFile, sessionsForTestFile) {
let results = '';
sessionsForTestFile.forEach((session) => {
if (session.testResults === undefined) {
return session.status + '\n\n';
}
results += session.testResults.suites
.map((suite) => this.outputSuite(suite, ''))
.join('\n\n');
});
return results;
}
specReporter({ reportResults = true } = {}) {
return {
onTestRunFinished: () => {
// Do nothing
},
reportTestFileResults: async ({
logger,
sessionsForTestFile,
testFile,
}) => {
if (!reportResults) {
return;
}
const testReport = await this.generateTestReport(
testFile,
sessionsForTestFile
);
logger.group();
console.log(testReport);
logger.groupEnd();
},
};
}
}
// disable-gpu required if no X server is available.
// Leave it off by default, as it may add variance to visual tests.
const chromeArgs = ['--disable-gpu', '--remote-debugging-port=9333'];
if (isDocker) {
// Inside of docker, Chrome's sandbox causes an error.
chromeArgs.push('--no-sandbox');
}
/** @type {import('@web/test-runner').TestRunnerGroupConfig[]} */
const defaultConfig = {
coverageConfig: {
exclude: ['**/snowpack/**/*', '**/*_test.ts*'],
},
browsers: [
puppeteerLauncher({
launchOptions: {
executablePath: process.env.RIKAIKUN_PUPPETEER_EXEC_PATH
? process.env.RIKAIKUN_PUPPETEER_EXEC_PATH
: '/usr/bin/google-chrome',
headless: true,
args: chromeArgs,
},
}),
],
plugins: [
snowpackWebTestRunner(),
visualRegressionPlugin({
update: process.argv.includes('--update-visual-baseline'),
// When not running in Github Actions, save to an unpushed local folder.
baseDir: process.env.CI ? 'screenshots' : 'local-screenshots',
}),
],
// Use custom runner HTML to add chrome stubs early since chrome APIs are used during
// file initialization in rikaikun.
testRunnerHtml: (testFramework) =>
`<html>
<body>
<script type="module" src="test/chrome_stubs.js"></script>
<script type="module" src="${testFramework}"></script>
</body>
</html>`,
reporters: [
// Gives overall test progress across all tests.
defaultReporter({ reportTestResults: true, reportTestProgress: true }),
// Gives detailed description of chai test spec results.
new SpecReporter().specReporter(),
],
};
/** @type {import('@web/test-runner').TestRunnerGroupConfig[]} */
let config = defaultConfig;
if (process.argv.includes('--browserstack')) {
config = {
...config,
testFramework: {
config: {
ui: 'bdd',
timeout: '40000',
},
},
browserStartTimeout: 1000 * 60 * 1,
testsStartTimeout: 1000 * 60 * 1,
testsFinishTimeout: 1000 * 60 * 5,
// how many browsers to run concurrently in browserstack. increasing this significantly
// reduces testing time, but your subscription might limit concurrent connections
concurrentBrowsers: 2,
// Set concurrency to 1 so tests don't interfere during screenshots.
concurrency: 1,
browsers: [
browserstackLauncher({
capabilities: {
browserName: 'Chrome',
browserVersion: 'latest',
os: 'windows',
os_version: '10',
// Used when creating the browser directory for screenshots
platform: 'windows 10',
// your username and key for browserstack, you can get this from your browserstack account
// it's recommended to store these as environment variables
'browserstack.user': process.env.BROWSER_STACK_USERNAME,
'browserstack.key': process.env.BROWSER_STACK_ACCESS_KEY,
project: 'rikaikun',
name: 'CI Testing',
// if you are running tests in a CI, the build id might be available as an
// environment variable. this is useful for identifying test runs
// this is for example the name for github actions
build: `build ${process.env.GITHUB_RUN_NUMBER || 'local'}`,
'browserstack.console': 'verbose',
},
}),
],
};
}
export default config;