UNPKG

21.7 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.check = check;
7exports.options = exports.docs = exports.usage = void 0;
8
9function _jestConfig() {
10 const data = require('jest-config');
11
12 _jestConfig = function () {
13 return data;
14 };
15
16 return data;
17}
18
19/**
20 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
21 *
22 * This source code is licensed under the MIT license found in the
23 * LICENSE file in the root directory of this source tree.
24 */
25function check(argv) {
26 if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {
27 throw new Error(
28 'Both --runInBand and --maxWorkers were specified, but these two ' +
29 'options do not make sense together. Which is it?'
30 );
31 }
32
33 for (const key of [
34 'onlyChanged',
35 'lastCommit',
36 'changedFilesWithAncestor',
37 'changedSince'
38 ]) {
39 if (argv[key] && argv.watchAll) {
40 throw new Error(
41 `Both --${key} and --watchAll were specified, but these two ` +
42 'options do not make sense together. Try the --watch option which ' +
43 'reruns only tests related to changed files.'
44 );
45 }
46 }
47
48 if (argv.onlyFailures && argv.watchAll) {
49 throw new Error(
50 `Both --onlyFailures and --watchAll were specified, but these two ` +
51 'options do not make sense together.'
52 );
53 }
54
55 if (argv.findRelatedTests && argv._.length === 0) {
56 throw new Error(
57 'The --findRelatedTests option requires file paths to be specified.\n' +
58 'Example usage: jest --findRelatedTests ./src/source.js ' +
59 './src/index.js.'
60 );
61 }
62
63 if (argv.hasOwnProperty('maxWorkers') && argv.maxWorkers === undefined) {
64 throw new Error(
65 'The --maxWorkers (-w) option requires a number or string to be specified.\n' +
66 'Example usage: jest --maxWorkers 2\n' +
67 'Example usage: jest --maxWorkers 50%\n' +
68 'Or did you mean --watch?'
69 );
70 }
71
72 if (argv.selectProjects && argv.selectProjects.length === 0) {
73 throw new Error(
74 'The --selectProjects option requires the name of at least one project to be specified.\n' +
75 'Example usage: jest --selectProjects my-first-project my-second-project'
76 );
77 }
78
79 if (
80 argv.config &&
81 !(0, _jestConfig().isJSONString)(argv.config) &&
82 !argv.config.match(
83 new RegExp(
84 `\\.(${_jestConfig()
85 .constants.JEST_CONFIG_EXT_ORDER.map(e => e.substring(1))
86 .join('|')})$`,
87 'i'
88 )
89 )
90 ) {
91 throw new Error(
92 `The --config option requires a JSON string literal, or a file path with one of these extensions: ${_jestConfig().constants.JEST_CONFIG_EXT_ORDER.join(
93 ', '
94 )}.\nExample usage: jest --config ./jest.config.js`
95 );
96 }
97
98 return true;
99}
100
101const usage = 'Usage: $0 [--config=<pathToConfigFile>] [TestPathPattern]';
102exports.usage = usage;
103const docs = 'Documentation: https://jestjs.io/'; // The default values are all set in jest-config
104
105exports.docs = docs;
106const options = {
107 all: {
108 description:
109 'The opposite of `onlyChanged`. If `onlyChanged` is set by ' +
110 'default, running jest with `--all` will force Jest to run all tests ' +
111 'instead of running only tests related to changed files.',
112 type: 'boolean'
113 },
114 automock: {
115 description: 'Automock all files by default.',
116 type: 'boolean'
117 },
118 bail: {
119 alias: 'b',
120 description:
121 'Exit the test suite immediately after `n` number of failing tests.',
122 type: 'boolean'
123 },
124 cache: {
125 description:
126 'Whether to use the transform cache. Disable the cache ' +
127 'using --no-cache.',
128 type: 'boolean'
129 },
130 cacheDirectory: {
131 description:
132 'The directory where Jest should store its cached ' +
133 ' dependency information.',
134 type: 'string'
135 },
136 changedFilesWithAncestor: {
137 description:
138 'Runs tests related to the current changes and the changes made in the ' +
139 'last commit. Behaves similarly to `--onlyChanged`.',
140 type: 'boolean'
141 },
142 changedSince: {
143 description:
144 'Runs tests related to the changes since the provided branch. If the ' +
145 'current branch has diverged from the given branch, then only changes ' +
146 'made locally will be tested. Behaves similarly to `--onlyChanged`.',
147 nargs: 1,
148 type: 'string'
149 },
150 ci: {
151 description:
152 'Whether to run Jest in continuous integration (CI) mode. ' +
153 'This option is on by default in most popular CI environments. It will ' +
154 'prevent snapshots from being written unless explicitly requested.',
155 type: 'boolean'
156 },
157 clearCache: {
158 description:
159 'Clears the configured Jest cache directory and then exits. ' +
160 'Default directory can be found by calling jest --showConfig',
161 type: 'boolean'
162 },
163 clearMocks: {
164 description:
165 'Automatically clear mock calls and instances between every ' +
166 'test. Equivalent to calling jest.clearAllMocks() between each test.',
167 type: 'boolean'
168 },
169 collectCoverage: {
170 description: 'Alias for --coverage.',
171 type: 'boolean'
172 },
173 collectCoverageFrom: {
174 description:
175 'A glob pattern relative to <rootDir> matching the files that coverage ' +
176 'info needs to be collected from.',
177 type: 'string'
178 },
179 collectCoverageOnlyFrom: {
180 description: 'Explicit list of paths coverage will be restricted to.',
181 string: true,
182 type: 'array'
183 },
184 color: {
185 description:
186 'Forces test results output color highlighting (even if ' +
187 'stdout is not a TTY). Set to false if you would like to have no colors.',
188 type: 'boolean'
189 },
190 colors: {
191 description: 'Alias for `--color`.',
192 type: 'boolean'
193 },
194 config: {
195 alias: 'c',
196 description:
197 'The path to a jest config file specifying how to find ' +
198 'and execute tests. If no rootDir is set in the config, the directory ' +
199 'containing the config file is assumed to be the rootDir for the project.' +
200 'This can also be a JSON encoded value which Jest will use as configuration.',
201 type: 'string'
202 },
203 coverage: {
204 description:
205 'Indicates that test coverage information should be ' +
206 'collected and reported in the output.',
207 type: 'boolean'
208 },
209 coverageDirectory: {
210 description: 'The directory where Jest should output its coverage files.',
211 type: 'string'
212 },
213 coveragePathIgnorePatterns: {
214 description:
215 'An array of regexp pattern strings that are matched ' +
216 'against all file paths before executing the test. If the file path' +
217 'matches any of the patterns, coverage information will be skipped.',
218 string: true,
219 type: 'array'
220 },
221 coverageProvider: {
222 choices: ['babel', 'v8'],
223 description: 'Select between Babel and V8 to collect coverage'
224 },
225 coverageReporters: {
226 description:
227 'A list of reporter names that Jest uses when writing ' +
228 'coverage reports. Any istanbul reporter can be used.',
229 string: true,
230 type: 'array'
231 },
232 coverageThreshold: {
233 description:
234 'A JSON string with which will be used to configure ' +
235 'minimum threshold enforcement for coverage results',
236 type: 'string'
237 },
238 debug: {
239 description: 'Print debugging info about your jest config.',
240 type: 'boolean'
241 },
242 detectLeaks: {
243 description:
244 '**EXPERIMENTAL**: Detect memory leaks in tests. After executing a ' +
245 'test, it will try to garbage collect the global object used, and fail ' +
246 'if it was leaked',
247 type: 'boolean'
248 },
249 detectOpenHandles: {
250 description:
251 'Print out remaining open handles preventing Jest from exiting at the ' +
252 'end of a test run. Implies `runInBand`.',
253 type: 'boolean'
254 },
255 env: {
256 description:
257 'The test environment used for all tests. This can point to ' +
258 'any file or node module. Examples: `jsdom`, `node` or ' +
259 '`path/to/my-environment.js`',
260 type: 'string'
261 },
262 errorOnDeprecated: {
263 description: 'Make calling deprecated APIs throw helpful error messages.',
264 type: 'boolean'
265 },
266 expand: {
267 alias: 'e',
268 description: 'Use this flag to show full diffs instead of a patch.',
269 type: 'boolean'
270 },
271 filter: {
272 description:
273 'Path to a module exporting a filtering function. This method receives ' +
274 'a list of tests which can be manipulated to exclude tests from ' +
275 'running. Especially useful when used in conjunction with a testing ' +
276 'infrastructure to filter known broken tests.',
277 type: 'string'
278 },
279 findRelatedTests: {
280 description:
281 'Find related tests for a list of source files that were ' +
282 'passed in as arguments. Useful for pre-commit hook integration to run ' +
283 'the minimal amount of tests necessary.',
284 type: 'boolean'
285 },
286 forceExit: {
287 description:
288 'Force Jest to exit after all tests have completed running. ' +
289 'This is useful when resources set up by test code cannot be ' +
290 'adequately cleaned up.',
291 type: 'boolean'
292 },
293 globalSetup: {
294 description: 'The path to a module that runs before All Tests.',
295 type: 'string'
296 },
297 globalTeardown: {
298 description: 'The path to a module that runs after All Tests.',
299 type: 'string'
300 },
301 globals: {
302 description:
303 'A JSON string with map of global variables that need ' +
304 'to be available in all test environments.',
305 type: 'string'
306 },
307 haste: {
308 description:
309 'A JSON string with map of variables for the haste module system',
310 type: 'string'
311 },
312 init: {
313 description: 'Generate a basic configuration file',
314 type: 'boolean'
315 },
316 injectGlobals: {
317 description: 'Should Jest inject global variables or not',
318 type: 'boolean'
319 },
320 json: {
321 description:
322 'Prints the test results in JSON. This mode will send all ' +
323 'other test output and user messages to stderr.',
324 type: 'boolean'
325 },
326 lastCommit: {
327 description:
328 'Run all tests affected by file changes in the last commit made. ' +
329 'Behaves similarly to `--onlyChanged`.',
330 type: 'boolean'
331 },
332 listTests: {
333 description:
334 'Lists all tests Jest will run given the arguments and ' +
335 'exits. Most useful in a CI system together with `--findRelatedTests` ' +
336 'to determine the tests Jest will run based on specific files',
337 type: 'boolean'
338 },
339 logHeapUsage: {
340 description:
341 'Logs the heap usage after every test. Useful to debug ' +
342 'memory leaks. Use together with `--runInBand` and `--expose-gc` in ' +
343 'node.',
344 type: 'boolean'
345 },
346 maxConcurrency: {
347 description:
348 'Specifies the maximum number of tests that are allowed to run' +
349 'concurrently. This only affects tests using `test.concurrent`.',
350 type: 'number'
351 },
352 maxWorkers: {
353 alias: 'w',
354 description:
355 'Specifies the maximum number of workers the worker-pool ' +
356 'will spawn for running tests. This defaults to the number of the ' +
357 'cores available on your machine. (its usually best not to override ' +
358 'this default)',
359 type: 'string'
360 },
361 moduleDirectories: {
362 description:
363 'An array of directory names to be searched recursively ' +
364 "up from the requiring module's location.",
365 string: true,
366 type: 'array'
367 },
368 moduleFileExtensions: {
369 description:
370 'An array of file extensions your modules use. If you ' +
371 'require modules without specifying a file extension, these are the ' +
372 'extensions Jest will look for. ',
373 string: true,
374 type: 'array'
375 },
376 moduleNameMapper: {
377 description:
378 'A JSON string with a map from regular expressions to ' +
379 'module names or to arrays of module names that allow to stub ' +
380 'out resources, like images or styles with a single module',
381 type: 'string'
382 },
383 modulePathIgnorePatterns: {
384 description:
385 'An array of regexp pattern strings that are matched ' +
386 'against all module paths before those paths are to be considered ' +
387 '"visible" to the module loader.',
388 string: true,
389 type: 'array'
390 },
391 modulePaths: {
392 description:
393 'An alternative API to setting the NODE_PATH env variable, ' +
394 'modulePaths is an array of absolute paths to additional locations to ' +
395 'search when resolving modules.',
396 string: true,
397 type: 'array'
398 },
399 noStackTrace: {
400 description: 'Disables stack trace in test results output',
401 type: 'boolean'
402 },
403 notify: {
404 description: 'Activates notifications for test results.',
405 type: 'boolean'
406 },
407 notifyMode: {
408 description: 'Specifies when notifications will appear for test results.',
409 type: 'string'
410 },
411 onlyChanged: {
412 alias: 'o',
413 description:
414 'Attempts to identify which tests to run based on which ' +
415 "files have changed in the current repository. Only works if you're " +
416 'running tests in a git or hg repository at the moment.',
417 type: 'boolean'
418 },
419 onlyFailures: {
420 alias: 'f',
421 description: 'Run tests that failed in the previous execution.',
422 type: 'boolean'
423 },
424 outputFile: {
425 description:
426 'Write test results to a file when the --json option is ' +
427 'also specified.',
428 type: 'string'
429 },
430 passWithNoTests: {
431 description:
432 'Will not fail if no tests are found (for example while using `--testPathPattern`.)',
433 type: 'boolean'
434 },
435 preset: {
436 description: "A preset that is used as a base for Jest's configuration.",
437 type: 'string'
438 },
439 prettierPath: {
440 description: 'The path to the "prettier" module used for inline snapshots.',
441 type: 'string'
442 },
443 projects: {
444 description:
445 'A list of projects that use Jest to run all tests of all ' +
446 'projects in a single instance of Jest.',
447 string: true,
448 type: 'array'
449 },
450 reporters: {
451 description: 'A list of custom reporters for the test suite.',
452 string: true,
453 type: 'array'
454 },
455 resetMocks: {
456 description:
457 'Automatically reset mock state between every test. ' +
458 'Equivalent to calling jest.resetAllMocks() between each test.',
459 type: 'boolean'
460 },
461 resetModules: {
462 description:
463 'If enabled, the module registry for every test file will ' +
464 'be reset before running each individual test.',
465 type: 'boolean'
466 },
467 resolver: {
468 description: 'A JSON string which allows the use of a custom resolver.',
469 type: 'string'
470 },
471 restoreMocks: {
472 description:
473 'Automatically restore mock state and implementation between every test. ' +
474 'Equivalent to calling jest.restoreAllMocks() between each test.',
475 type: 'boolean'
476 },
477 rootDir: {
478 description:
479 'The root directory that Jest should scan for tests and ' +
480 'modules within.',
481 type: 'string'
482 },
483 roots: {
484 description:
485 'A list of paths to directories that Jest should use to ' +
486 'search for files in.',
487 string: true,
488 type: 'array'
489 },
490 runInBand: {
491 alias: 'i',
492 description:
493 'Run all tests serially in the current process (rather than ' +
494 'creating a worker pool of child processes that run tests). This ' +
495 'is sometimes useful for debugging, but such use cases are pretty ' +
496 'rare.',
497 type: 'boolean'
498 },
499 runTestsByPath: {
500 description:
501 'Used when provided patterns are exact file paths. This avoids ' +
502 'converting them into a regular expression and matching it against ' +
503 'every single file.',
504 type: 'boolean'
505 },
506 runner: {
507 description:
508 "Allows to use a custom runner instead of Jest's default test runner.",
509 type: 'string'
510 },
511 selectProjects: {
512 description:
513 'Run only the tests of the specified projects.' +
514 'Jest uses the attribute `displayName` in the configuration to identify each project.',
515 string: true,
516 type: 'array'
517 },
518 setupFiles: {
519 description:
520 'A list of paths to modules that run some code to configure or ' +
521 'set up the testing environment before each test. ',
522 string: true,
523 type: 'array'
524 },
525 setupFilesAfterEnv: {
526 description:
527 'A list of paths to modules that run some code to configure or ' +
528 'set up the testing framework before each test ',
529 string: true,
530 type: 'array'
531 },
532 showConfig: {
533 description: 'Print your jest config and then exits.',
534 type: 'boolean'
535 },
536 silent: {
537 description: 'Prevent tests from printing messages through the console.',
538 type: 'boolean'
539 },
540 skipFilter: {
541 description:
542 'Disables the filter provided by --filter. Useful for CI jobs, or ' +
543 'local enforcement when fixing tests.',
544 type: 'boolean'
545 },
546 snapshotSerializers: {
547 description:
548 'A list of paths to snapshot serializer modules Jest should ' +
549 'use for snapshot testing.',
550 string: true,
551 type: 'array'
552 },
553 testEnvironment: {
554 description: 'Alias for --env',
555 type: 'string'
556 },
557 testEnvironmentOptions: {
558 description:
559 'A JSON string with options that will be passed to the `testEnvironment`. ' +
560 'The relevant options depend on the environment.',
561 type: 'string'
562 },
563 testFailureExitCode: {
564 description: 'Exit code of `jest` command if the test run failed',
565 type: 'string' // number
566 },
567 testLocationInResults: {
568 description: 'Add `location` information to the test results',
569 type: 'boolean'
570 },
571 testMatch: {
572 description: 'The glob patterns Jest uses to detect test files.',
573 string: true,
574 type: 'array'
575 },
576 testNamePattern: {
577 alias: 't',
578 description: 'Run only tests with a name that matches the regex pattern.',
579 type: 'string'
580 },
581 testPathIgnorePatterns: {
582 description:
583 'An array of regexp pattern strings that are matched ' +
584 'against all test paths before executing the test. If the test path ' +
585 'matches any of the patterns, it will be skipped.',
586 string: true,
587 type: 'array'
588 },
589 testPathPattern: {
590 description:
591 'A regexp pattern string that is matched against all tests ' +
592 'paths before executing the test.',
593 string: true,
594 type: 'array'
595 },
596 testRegex: {
597 description:
598 'A string or array of string regexp patterns that Jest uses to detect test files.',
599 string: true,
600 type: 'array'
601 },
602 testResultsProcessor: {
603 description:
604 'Allows the use of a custom results processor. ' +
605 'This processor must be a node module that exports ' +
606 'a function expecting as the first argument the result object.',
607 type: 'string'
608 },
609 testRunner: {
610 description:
611 'Allows to specify a custom test runner. The default is' +
612 ' `jest-circus/runner`. A path to a custom test runner can be provided:' +
613 ' `<rootDir>/path/to/testRunner.js`.',
614 type: 'string'
615 },
616 testSequencer: {
617 description:
618 'Allows to specify a custom test sequencer. The default is ' +
619 '`@jest/test-sequencer`. A path to a custom test sequencer can be ' +
620 'provided: `<rootDir>/path/to/testSequencer.js`',
621 type: 'string'
622 },
623 testTimeout: {
624 description: 'This option sets the default timeouts of test cases.',
625 type: 'number'
626 },
627 testURL: {
628 description: 'This option sets the URL for the jsdom environment.',
629 type: 'string'
630 },
631 timers: {
632 description:
633 'Setting this value to fake allows the use of fake timers ' +
634 'for functions such as setTimeout.',
635 type: 'string'
636 },
637 transform: {
638 description:
639 'A JSON string which maps from regular expressions to paths ' +
640 'to transformers.',
641 type: 'string'
642 },
643 transformIgnorePatterns: {
644 description:
645 'An array of regexp pattern strings that are matched ' +
646 'against all source file paths before transformation.',
647 string: true,
648 type: 'array'
649 },
650 unmockedModulePathPatterns: {
651 description:
652 'An array of regexp pattern strings that are matched ' +
653 'against all modules before the module loader will automatically ' +
654 'return a mock for them.',
655 string: true,
656 type: 'array'
657 },
658 updateSnapshot: {
659 alias: 'u',
660 description:
661 'Use this flag to re-record snapshots. ' +
662 'Can be used together with a test suite pattern or with ' +
663 '`--testNamePattern` to re-record snapshot for test matching ' +
664 'the pattern',
665 type: 'boolean'
666 },
667 useStderr: {
668 description: 'Divert all output to stderr.',
669 type: 'boolean'
670 },
671 verbose: {
672 description:
673 'Display individual test results with the test suite hierarchy.',
674 type: 'boolean'
675 },
676 version: {
677 alias: 'v',
678 description: 'Print the version and exit',
679 type: 'boolean'
680 },
681 watch: {
682 description:
683 'Watch files for changes and rerun tests related to ' +
684 'changed files. If you want to re-run all tests when a file has ' +
685 'changed, use the `--watchAll` option.',
686 type: 'boolean'
687 },
688 watchAll: {
689 description:
690 'Watch files for changes and rerun all tests. If you want ' +
691 'to re-run only the tests related to the changed files, use the ' +
692 '`--watch` option.',
693 type: 'boolean'
694 },
695 watchPathIgnorePatterns: {
696 description:
697 'An array of regexp pattern strings that are matched ' +
698 'against all paths before trigger test re-run in watch mode. ' +
699 'If the test path matches any of the patterns, it will be skipped.',
700 string: true,
701 type: 'array'
702 },
703 watchman: {
704 description:
705 'Whether to use watchman for file crawling. Disable using ' +
706 '--no-watchman.',
707 type: 'boolean'
708 }
709};
710exports.options = options;