UNPKG

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