-
Notifications
You must be signed in to change notification settings - Fork 48.3k
/
Copy pathjest-cli.js
401 lines (354 loc) · 10.2 KB
/
jest-cli.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
'use strict';
const {spawn} = require('child_process');
const chalk = require('chalk');
const yargs = require('yargs');
const fs = require('fs');
const path = require('path');
const semver = require('semver');
const ossConfig = './scripts/jest/config.source.js';
const wwwConfig = './scripts/jest/config.source-www.js';
const xplatConfig = './scripts/jest/config.source-xplat.js';
const devToolsConfig = './scripts/jest/config.build-devtools.js';
// TODO: These configs are separate but should be rolled into the configs above
// so that the CLI can provide them as options for any of the configs.
const persistentConfig = './scripts/jest/config.source-persistent.js';
const buildConfig = './scripts/jest/config.build.js';
const argv = yargs
.parserConfiguration({
// Important: This option tells yargs to move all other options not
// specified here into the `_` key. We use this to send all of the
// Jest options that we don't use through to Jest (like --watch).
'unknown-options-as-args': true,
})
.wrap(yargs.terminalWidth())
.options({
debug: {
alias: 'd',
describe: 'Run with node debugger attached.',
requiresArg: false,
type: 'boolean',
default: false,
},
project: {
alias: 'p',
describe: 'Run the given project.',
requiresArg: true,
type: 'string',
default: 'default',
choices: ['default', 'devtools'],
},
releaseChannel: {
alias: 'r',
describe: 'Run with the given release channel.',
requiresArg: true,
type: 'string',
default: 'experimental',
choices: ['experimental', 'stable', 'www-classic', 'www-modern', 'xplat'],
},
env: {
alias: 'e',
describe: 'Run with the given node environment.',
requiresArg: true,
type: 'string',
choices: ['development', 'production'],
},
prod: {
describe: 'Run with NODE_ENV=production.',
requiresArg: false,
type: 'boolean',
default: false,
},
dev: {
describe: 'Run with NODE_ENV=development.',
requiresArg: false,
type: 'boolean',
default: false,
},
variant: {
alias: 'v',
describe: 'Run with www variant set to true.',
requiresArg: false,
type: 'boolean',
},
build: {
alias: 'b',
describe: 'Run tests on builds.',
requiresArg: false,
type: 'boolean',
default: false,
},
persistent: {
alias: 'n',
describe: 'Run with persistence.',
requiresArg: false,
type: 'boolean',
default: false,
},
ci: {
describe: 'Run tests in CI',
requiresArg: false,
type: 'boolean',
default: false,
},
compactConsole: {
alias: 'c',
describe: 'Compact console output (hide file locations).',
requiresArg: false,
type: 'boolean',
default: false,
},
reactVersion: {
describe: 'DevTools testing for specific version of React',
requiresArg: true,
type: 'string',
},
sourceMaps: {
describe:
'Enable inline source maps when transforming source files with Jest. Useful for debugging, but makes it slower.',
type: 'boolean',
default: false,
},
}).argv;
function logError(message) {
console.error(chalk.red(`\n${message}`));
}
function isWWWConfig() {
return (
(argv.releaseChannel === 'www-classic' ||
argv.releaseChannel === 'www-modern') &&
argv.project !== 'devtools'
);
}
function isXplatConfig() {
return argv.releaseChannel === 'xplat' && argv.project !== 'devtools';
}
function isOSSConfig() {
return (
argv.releaseChannel === 'stable' || argv.releaseChannel === 'experimental'
);
}
function validateOptions() {
let success = true;
if (argv.project === 'devtools') {
if (argv.prod) {
logError(
'DevTool tests do not support --prod. Remove this option to continue.'
);
success = false;
}
if (argv.dev) {
logError(
'DevTool tests do not support --dev. Remove this option to continue.'
);
success = false;
}
if (argv.env) {
logError(
'DevTool tests do not support --env. Remove this option to continue.'
);
success = false;
}
if (argv.persistent) {
logError(
'DevTool tests do not support --persistent. Remove this option to continue.'
);
success = false;
}
if (argv.variant) {
logError(
'DevTool tests do not support --variant. Remove this option to continue.'
);
success = false;
}
if (!argv.build) {
logError('DevTool tests require --build.');
success = false;
}
if (argv.reactVersion && !semver.validRange(argv.reactVersion)) {
success = false;
logError('please specify a valid version range for --reactVersion');
}
} else {
if (argv.compactConsole) {
logError('Only DevTool tests support compactConsole flag.');
success = false;
}
if (argv.reactVersion) {
logError('Only DevTools tests supports the --reactVersion flag.');
success = false;
}
}
if (isWWWConfig() || isXplatConfig()) {
if (argv.variant === undefined) {
// Turn internal experiments on by default
argv.variant = true;
}
} else {
if (argv.variant) {
logError(
'Variant is only supported for the www release channels. Update these options to continue.'
);
success = false;
}
}
if (argv.build && argv.persistent) {
logError(
'Persistence is not supported for build targets. Update these options to continue.'
);
success = false;
}
if (!isOSSConfig() && argv.persistent) {
logError(
'Persistence only supported for oss release channels. Update these options to continue.'
);
success = false;
}
if (argv.build && isWWWConfig()) {
logError(
'Build targets are only not supported for www release channels. Update these options to continue.'
);
success = false;
}
if (argv.build && isXplatConfig()) {
logError(
'Build targets are only not supported for xplat release channels. Update these options to continue.'
);
success = false;
}
if (argv.env && argv.env !== 'production' && argv.prod) {
logError(
'Build type does not match --prod. Update these options to continue.'
);
success = false;
}
if (argv.env && argv.env !== 'development' && argv.dev) {
logError(
'Build type does not match --dev. Update these options to continue.'
);
success = false;
}
if (argv.prod && argv.dev) {
logError(
'Cannot supply both --prod and --dev. Remove one of these options to continue.'
);
success = false;
}
if (argv.build) {
// TODO: We could build this if it hasn't been built yet.
const buildDir = path.resolve('./build');
if (!fs.existsSync(buildDir)) {
logError(
'Build directory does not exist, please run `yarn build` or remove the --build option.'
);
success = false;
} else if (Date.now() - fs.statSync(buildDir).mtimeMs > 1000 * 60 * 15) {
logError(
'Warning: Running a build test with a build directory older than 15 minutes.\nPlease remember to run `yarn build` when using --build.'
);
}
}
if (!success) {
console.log(''); // Extra newline.
process.exit(1);
}
}
function getCommandArgs() {
// Add the correct Jest config.
const args = ['./scripts/jest/jest.js', '--config'];
if (argv.project === 'devtools') {
args.push(devToolsConfig);
} else if (argv.build) {
args.push(buildConfig);
} else if (argv.persistent) {
args.push(persistentConfig);
} else if (isWWWConfig()) {
args.push(wwwConfig);
} else if (isXplatConfig()) {
args.push(xplatConfig);
} else if (isOSSConfig()) {
args.push(ossConfig);
} else {
// We should not get here.
logError('Unrecognized release channel');
process.exit(1);
}
// Set the debug options, if necessary.
if (argv.debug) {
args.unshift('--inspect-brk');
args.push('--runInBand');
// Prevent console logs from being hidden until test completes.
args.push('--useStderr');
}
if (argv.ci === true) {
args.push('--maxConcurrency=10');
}
// Push the remaining args onto the command.
// This will send args like `--watch` to Jest.
args.push(...argv._);
return args;
}
function getEnvars() {
const envars = {
NODE_ENV: argv.env || 'development',
RELEASE_CHANNEL: argv.releaseChannel.match(/modern|experimental/)
? 'experimental'
: 'stable',
// Pass this flag through to the config environment
// so the base config can conditionally load the console setup file.
compactConsole: argv.compactConsole,
};
if (argv.prod) {
envars.NODE_ENV = 'production';
}
if (argv.dev) {
envars.NODE_ENV = 'development';
}
if (argv.variant) {
envars.VARIANT = true;
}
if (argv.reactVersion) {
envars.REACT_VERSION = semver.coerce(argv.reactVersion);
}
if (argv.sourceMaps) {
// This is off by default because it slows down the test runner, but it's
// super useful when running the debugger.
envars.JEST_ENABLE_SOURCE_MAPS = 'inline';
}
return envars;
}
function main() {
validateOptions();
const args = getCommandArgs();
const envars = getEnvars();
const env = Object.entries(envars).map(([k, v]) => `${k}=${v}`);
if (argv.ci !== true) {
// Print the full command we're actually running.
const command = `$ ${env.join(' ')} node ${args.join(' ')}`;
console.log(chalk.dim(command));
// Print the release channel and project we're running for quick confirmation.
console.log(
chalk.blue(
`\nRunning tests for ${argv.project} (${argv.releaseChannel})...`
)
);
}
// Print a message that the debugger is starting just
// for some extra feedback when running the debugger.
if (argv.debug) {
console.log(chalk.green('\nStarting debugger...'));
console.log(chalk.green('Open chrome://inspect and press "inspect"\n'));
}
// Run Jest.
const jest = spawn('node', args, {
stdio: 'inherit',
env: {...envars, ...process.env},
});
// Ensure we close our process when we get a failure case.
jest.on('close', code => {
// Forward the exit code from the Jest process.
if (code === 1) {
process.exit(1);
}
});
}
main();