-
Notifications
You must be signed in to change notification settings - Fork 178
/
karma-dashboard.config.cjs
216 lines (186 loc) · 6.02 KB
/
karma-dashboard.config.cjs
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
215
216
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
/**
* External dependencies
*/
const { readFileSync, existsSync } = require('fs');
/**
* Internal dependencies
*/
const getWebpackConfig = require('./webpack.config.test.cjs');
module.exports = function (config) {
let specsToRetry;
if (
config.retryFailed &&
existsSync('build/karma-dashboard-failed-tests.txt')
) {
// Loads names of failed specs and prepares them for use in a regex.
specsToRetry = readFileSync(
'build/karma-dashboard-failed-tests.txt',
'utf-8'
)
.replace(/\s+$/g, '')
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d')
.split('\n')
.join('|');
}
config.set({
plugins: [
'karma-chrome-launcher',
'karma-jasmine',
'karma-sourcemap-loader',
'karma-webpack',
'karma-coverage-istanbul-reporter',
require('@web-stories-wp/karma-puppeteer-launcher'),
require('@web-stories-wp/karma-puppeteer-client'),
require('@web-stories-wp/karma-failed-tests-reporter'),
],
// Frameworks to use.
// Available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: [
'jasmine',
'@web-stories-wp/karma-puppeteer-client',
'webpack',
],
// list of files / patterns to load in the browser
files: [
{ pattern: 'packages/dashboard/src/**/karma/**/*.js', watched: false },
{ pattern: 'packages/karma-fixture/src/init.js', watched: false },
{
pattern: '__static__/**/*',
watched: false,
included: false,
served: true,
nocache: false,
},
'node_modules/axe-core/axe.js',
],
// list of files / patterns to exclude
exclude: ['**/test/**/*.js', '**/*.test.js'],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'packages/dashboard/src/**/karma/**/*.js': ['webpack', 'sourcemap'],
},
proxies: {
'/__static__/': '/base/__static__/',
},
webpack: getWebpackConfig('web-stories-dashboard', config),
webpackMiddleware: {
// webpack-dev-middleware configuration
// i. e.
stats: 'errors-only',
},
webpackServer: {
noInfo: true,
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: [
'dots',
'@web-stories-wp/karma-failed-tests-reporter',
config.coverage && 'coverage-istanbul',
].filter(Boolean),
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['karma-puppeteer-launcher'], // @web-stories-wp/karma-puppeteer-launcher package
puppeteerLauncher: {
puppeteer: {
headless: config.headless ? 'new' : false,
slowMo: config.slowMo || 0,
devtools: config.devtools || false,
snapshots: config.snapshots || false,
defaultViewport: getViewport(config.viewport),
},
},
client: {
args: [
specsToRetry && '--grep',
specsToRetry && `/${specsToRetry}/`,
].filter(Boolean),
jasmine: {
timeoutInterval: 20000,
},
useIframe: false,
runInParent: true,
},
coverageIstanbulReporter: {
dir: 'build/logs/karma-coverage/dashboard',
reports: ['text-summary', 'lcovonly'],
},
failedTestsReporter: {
outputFile: 'build/karma-dashboard-failed-tests.txt',
},
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browsers should be started simultaneously
concurrency: Infinity,
// Allow not having any tests
failOnEmptyTestSuite: false,
// Prevent duplicate logging to console
browserConsoleLogOptions: {
terminal: false,
},
// Bump browserNoActivityTimeout to 100s to prevent Github Actions timeout
browserNoActivityTimeout: 100000,
// Wait a bit longer for browser to reconnect.
browserDisconnectTimeout: 10000,
// Custom context file.
customClientContextFile:
'packages/karma-fixture/src/client_with_context.html',
});
};
/**
* Returns a viewport object for a given flag.
*
* The following viewports are supported:
* - default: no special viewport is used.
* - 1600:1000: empirical laptop size. Also used for screenshots.
*
* A custom W:H viewport is intentionally not supported to reduce number of
* test variations.
*
* Todo: Support the viewport sizes from Figma:
* - 1920:1080: the canonical desktop size.
* - 1024:680: the canonical iPad size.
*
* @param {string} flag Viewport flag.
* @return {{width: number, height: number}|null} Viewport.
*/
function getViewport(flag) {
if (!flag) {
// @todo: switch to 1600:1000 default once Percy is fully launched.
return null;
}
if (flag === '1600:1000') {
return { width: 1600, height: 1000 };
}
throw new Error(`Unsupported viewport: "${flag}"`);
}