-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
runtime-test-base.js
346 lines (317 loc) · 9.4 KB
/
runtime-test-base.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
'use strict';
const argv = require('minimist')(process.argv.slice(2));
const karmaConfig = require('../../test-configs/karma.conf');
const {
commonIntegrationTestPaths,
commonUnitTestPaths,
integrationTestPaths,
karmaHtmlFixturesPath,
karmaJsPaths,
unitTestCrossBrowserPaths,
unitTestPaths,
} = require('../../test-configs/config');
const {
createCtrlcHandler,
exitCtrlcHandler,
} = require('../../common/ctrlcHandler');
const {app} = require('../../server/test-server');
const {createKarmaServer, getAdTypes} = require('./helpers');
const {customLaunchers} = require('./custom-launchers');
const {cyan, green, red, yellow} = require('kleur/colors');
const {dotWrappingWidth} = require('../../common/logging');
const {getEsbuildBabelPlugin} = require('../../common/esbuild-babel');
const {getFilesFromArgv, getFilesFromFileList} = require('../../common/utils');
const {isCiBuild, isCircleciBuild} = require('../../common/ci');
const {log} = require('../../common/logging');
const {SERVER_TRANSFORM_PATH} = require('../../server/typescript-compile');
const {startServer, stopServer} = require('../serve');
const {unitTestsToRun} = require('./helpers-unit');
/**
* Used to print dots during esbuild + babel transforms
*/
let wrapCounter = 0;
/**
* Used to lazy-require the HTML transformer function after the server is built.
*/
let transform;
/**
* Consumes {@link karmaConfig} and dynamically populates fields based on test
* type and command line arguments.
*/
class RuntimeTestConfig {
/** @type {Array<string|Record<string, [string, *]>>} */
plugins = [];
/**@type {Record<string, string|string[]>} */
preprocessors = {};
/** @type {string[]} */
reporters = [];
client = {};
/**
* @param {string} testType
*/
constructor(testType) {
this.testType = testType;
/**
* TypeScript is used for typechecking here and is unable to infer the type
* after using Object.assign. This results in errors relating properties of
* which can never be `null` being treated as though they could be.
*/
Object.assign(this, karmaConfig);
this.updateBrowsers();
this.updateReporters();
this.updateFiles();
this.updatePreprocessors();
this.updateEsbuildConfig();
this.updateClient();
this.updateMiddleware();
this.updateCoverageSettings();
}
/**
* Updates the set of preprocessors to run on HTML and JS files before testing.
* Notes:
* - The HTML transform is lazy-required because the server is built at startup.
* - We must use babel on windows until esbuild can natively downconvert to ES5.
*/
updatePreprocessors() {
const createHtmlTransformer = function () {
return function (content, _file, done) {
if (!transform) {
const outputDir = `../../../${SERVER_TRANSFORM_PATH}/dist/transform`;
transform = require(outputDir).transformSync;
}
done(transform(content));
};
};
createHtmlTransformer.$inject = [];
this.plugins.push({
'preprocessor:htmlTransformer': ['factory', createHtmlTransformer],
});
this.preprocessors[karmaHtmlFixturesPath] = ['htmlTransformer', 'html2js'];
for (const karmaJsPath of karmaJsPaths) {
this.preprocessors[karmaJsPath] = ['esbuild'];
}
}
/**
* Picks a browser config based on the the test type and command line flags.
* Defaults to Chrome.
*/
updateBrowsers() {
const browser = argv.edge
? 'EdgeCustom'
: argv.firefox
? 'FirefoxCustom'
: argv.safari
? 'SafariCustom'
: 'ChromeCustom';
Object.assign(this, {browsers: [browser], customLaunchers});
}
/**
* Adds reporters to the default karma spec per test settings.
* Overrides default reporters for verbose settings.
*/
updateReporters() {
if (
(argv.testnames || argv.local_changes || argv.files || argv.verbose) &&
!isCiBuild()
) {
this.reporters = ['mocha'];
}
if (isCircleciBuild()) {
this.reporters.push('junit');
this.junitReporter = {
outputFile: `result-reports/${this.testType}.xml`,
useBrowserName: false,
};
}
if (argv.coverage) {
this.reporters.push('coverage-istanbul');
}
if (argv.report) {
this.reporters.push('json-result');
this.jsonResultReporter = {
outputFile: `result-reports/${this.testType}.json`,
};
}
}
/**
* Computes the set of files for Karma to load based on factors like test type,
* target browser, and flags.
*/
updateFiles() {
switch (this.testType) {
case 'unit':
if (argv.files || argv.filelist) {
this.files = commonUnitTestPaths
.concat(getFilesFromArgv())
.concat(getFilesFromFileList());
return;
}
if (argv.firefox || argv.safari || argv.edge) {
this.files = commonUnitTestPaths.concat(unitTestCrossBrowserPaths);
return;
}
if (argv.local_changes) {
this.files = commonUnitTestPaths.concat(unitTestsToRun());
return;
}
this.files = commonUnitTestPaths.concat(unitTestPaths);
return;
case 'integration':
if (argv.files) {
this.files = commonIntegrationTestPaths.concat(getFilesFromArgv());
return;
}
this.files = commonIntegrationTestPaths.concat(integrationTestPaths);
return;
default:
throw new Error(`Test type ${this.testType} was not recognized`);
}
}
/**
* Logs a message indicating the start of babel transforms.
*/
logBabelStart() {
wrapCounter = 0;
log(
green('Transforming tests with'),
cyan('esbuild'),
green('and'),
cyan('babel') + green('...')
);
}
/**
* Prints a dot for every babel transform, with wrapping if needed.
*/
printBabelDot() {
process.stdout.write('.');
if (++wrapCounter >= dotWrappingWidth) {
wrapCounter = 0;
process.stdout.write('\n');
}
}
/**
* Updates the esbuild config in the karma spec so esbuild can run with it.
*/
updateEsbuildConfig() {
const importPathPlugin = {
name: 'import-path',
setup(build) {
build.onResolve({filter: /^[\w-]+$/}, (file) => {
if (file.path === 'stream') {
return {path: require.resolve('stream-browserify'), namespace: ''};
}
});
},
};
const babelPlugin = getEsbuildBabelPlugin(
/* callerName */ 'test',
/* enableCache */ true,
/* preSetup */ this.logBabelStart,
/* postLoad */ this.printBabelDot
);
this.esbuild = {
target: 'es5',
define: {
'process.env.NODE_DEBUG': 'false',
'process.env.NODE_ENV': '"test"',
},
plugins: [importPathPlugin, babelPlugin],
sourcemap: 'inline',
};
}
/**
* Updates the client so that tests can access karma state. This is available in
* the browser via window.parent.karma.config.
*/
updateClient() {
this.singleRun = !argv.watch;
this.client.mocha.grep = !!argv.grep;
this.client.verboseLogging = !!argv.verbose;
this.client.captureConsole = !!argv.verbose || !!argv.files;
this.client.amp = {
useMinifiedJs: !!argv.minified,
adTypes: getAdTypes(),
mochaTimeout: this.client.mocha.timeout,
testServerPort: this.client.testServerPort,
isModuleBuild: !!argv.esm, // Used by skip matchers in testing/test-config.js
};
}
/**
* Inserts the AMP dev server into the middleware used by the Karma server.
*/
updateMiddleware() {
const createDevServerMiddleware = function () {
return require(require.resolve('../../server/app.js'));
};
this.plugins.push({
'middleware:devServer': ['factory', createDevServerMiddleware],
});
this.beforeMiddleware = ['devServer'];
}
/**
* Updates the Karma config to gather coverage info if coverage is enabled.
*/
updateCoverageSettings() {
if (argv.coverage) {
this.plugins.push('karma-coverage-istanbul-reporter');
this.coverageIstanbulReporter = {
dir: 'test/coverage',
reports: isCiBuild() ? ['lcovonly'] : ['html', 'text', 'text-summary'],
'report-config': {lcovonly: {file: `lcov-${this.testType}.info`}},
};
}
}
}
class RuntimeTestRunner {
/**
*
* @param {RuntimeTestConfig} config
*/
constructor(config) {
this.config = config;
this.env = null;
this.exitCode = 0;
}
/**
* @return {Promise<void>}
*/
async maybeBuild() {
throw new Error('maybeBuild method must be overridden');
}
/**
* @return {Promise<void>}
*/
async setup() {
await this.maybeBuild();
await startServer({
name: 'AMP Test Server',
host: 'localhost',
port: this.config.client.testServerPort,
middleware: () => [app],
});
const handlerProcess = createCtrlcHandler(`amp ${this.config.testType}`);
this.env = new Map().set('handlerProcess', handlerProcess);
}
/**
* @return {Promise<void>}
*/
async run() {
this.exitCode = await createKarmaServer(this.config);
}
/**
* @return {Promise<void>}
*/
async teardown() {
await stopServer();
exitCtrlcHandler(/** @type {Map} */ (this.env).get('handlerProcess'));
if (this.exitCode != 0) {
const message = `Karma test failed with exit code ${this.exitCode}`;
log(red('ERROR:'), yellow(message));
throw new Error(message);
}
}
}
module.exports = {
RuntimeTestRunner,
RuntimeTestConfig,
};