-
Notifications
You must be signed in to change notification settings - Fork 0
/
karma-story-editor.config.cjs
257 lines (224 loc) · 7.88 KB
/
karma-story-editor.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* 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 } = require('fs');
/**
* Internal dependencies
*/
const getWebpackConfig = require('./webpack.config.test.cjs');
module.exports = function (config) {
let specsToRetry;
if (config.retryFailed) {
// Loads names of failed specs and prepares them for use in a regex.
specsToRetry = readFileSync(
'build/karma-story-editor-failed-tests.txt',
'utf-8'
)
.replace(/\s+$/g, '')
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d')
.split('\n')
.join('|');
}
let totalShards = 1;
let shardNumber = 1;
if (config.shard) {
[shardNumber, totalShards] = config.shard.split('/');
}
const shardIndex = shardNumber - 1;
const enableParallelRuns = config.shard || config.parallel;
// Default is number of CPU cores minus 1.
const parallelExecutors =
config.parallel && true !== config.parallel
? Number(config.parallel)
: undefined;
config.set({
plugins: [
'karma-chrome-launcher',
'karma-jasmine',
'karma-sourcemap-loader',
'karma-webpack',
'karma-coverage-istanbul-reporter',
'karma-parallel',
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: [
enableParallelRuns && 'parallel',
'jasmine',
'@web-stories-wp/karma-puppeteer-client',
'webpack',
].filter(Boolean),
// list of files / patterns to load in the browser
files: [
{ pattern: 'packages/story-editor/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/story-editor/src/**/karma/**/*.js': ['webpack', 'sourcemap'],
},
proxies: {
'/__static__/': '/base/__static__/',
},
webpack: getWebpackConfig('wp-story-editor', 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 || false,
slowMo: config.slowMo || 0,
devtools: config.devtools || false,
snapshots: config.snapshots || false,
snapshotsDir: '.test_artifacts/karma_snapshots',
defaultViewport: getViewport(config.viewport),
},
},
client: {
args: [
specsToRetry && '--grep',
specsToRetry && `/${specsToRetry}/`,
].filter(Boolean),
jasmine: {
timeoutInterval: 10000,
},
useIframe: false,
runInParent: true,
},
coverageIstanbulReporter: {
dir: 'build/logs/karma-coverage/story-editor',
reports: ['text-summary', 'lcovonly'],
},
failedTestsReporter: {
outputFile: 'build/karma-story-editor-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,
// Sharding configuration for CI.
// We trick karma-parallel into using only 1 browser (parallelOptions.executors === 1)
// while using a custom strategy that splits up the tests into multiple shards (config.executors > 1).
// This allows us to run ony a subset of the tests like so:
// npm run test:karma:story-editor -- --headless --shard=1/3 # Run the first of 3 desired shards.
parallelOptions: {
shardStrategy: config.shard ? 'custom' : 'round-robin',
// If we're using custom sharding, just spin up 1 browser,
// but do the splitting in the custom strategy below.
executors: config.shard ? 1 : parallelExecutors,
// Re-implements a round-robin strategy, but with a custom shardIndex.
// Need to use the Function constructor here so we have access to the outer shardIndex and totalShards vars,
// because karma-parallel serializes this function.
// eslint-disable-next-line no-new-func -- karma-test
customShardStrategy: new Function(
'parallelOptions',
`
window.parallelDescribeCount = window.parallelDescribeCount || 0;
window.parallelDescribeCount++;
const shouldRunThisTest = (window.parallelDescribeCount % ${totalShards} === ${shardIndex});
return shouldRunThisTest;
`
),
},
// Allow not having any tests
failOnEmptyTestSuite: false,
// Prevent duplicate logging to console
browserConsoleLogOptions: {
terminal: false,
},
// When a browser crashes,try to relaunch more than just 2 times (which is the default)
retryLimit: 5,
// 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}"`);
}