UNPKG

6.23 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.run = run;
7exports.buildArgv = void 0;
8
9function path() {
10 const data = _interopRequireWildcard(require('path'));
11
12 path = function () {
13 return data;
14 };
15
16 return data;
17}
18
19function _chalk() {
20 const data = _interopRequireDefault(require('chalk'));
21
22 _chalk = function () {
23 return data;
24 };
25
26 return data;
27}
28
29function _exit() {
30 const data = _interopRequireDefault(require('exit'));
31
32 _exit = function () {
33 return data;
34 };
35
36 return data;
37}
38
39function _yargs() {
40 const data = _interopRequireDefault(require('yargs'));
41
42 _yargs = function () {
43 return data;
44 };
45
46 return data;
47}
48
49function _core() {
50 const data = require('@jest/core');
51
52 _core = function () {
53 return data;
54 };
55
56 return data;
57}
58
59function _jestConfig() {
60 const data = require('jest-config');
61
62 _jestConfig = function () {
63 return data;
64 };
65
66 return data;
67}
68
69function _jestUtil() {
70 const data = require('jest-util');
71
72 _jestUtil = function () {
73 return data;
74 };
75
76 return data;
77}
78
79function _jestValidate() {
80 const data = require('jest-validate');
81
82 _jestValidate = function () {
83 return data;
84 };
85
86 return data;
87}
88
89var _init = _interopRequireDefault(require('../init'));
90
91var args = _interopRequireWildcard(require('./args'));
92
93function _interopRequireDefault(obj) {
94 return obj && obj.__esModule ? obj : {default: obj};
95}
96
97function _getRequireWildcardCache(nodeInterop) {
98 if (typeof WeakMap !== 'function') return null;
99 var cacheBabelInterop = new WeakMap();
100 var cacheNodeInterop = new WeakMap();
101 return (_getRequireWildcardCache = function (nodeInterop) {
102 return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
103 })(nodeInterop);
104}
105
106function _interopRequireWildcard(obj, nodeInterop) {
107 if (!nodeInterop && obj && obj.__esModule) {
108 return obj;
109 }
110 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
111 return {default: obj};
112 }
113 var cache = _getRequireWildcardCache(nodeInterop);
114 if (cache && cache.has(obj)) {
115 return cache.get(obj);
116 }
117 var newObj = {};
118 var hasPropertyDescriptor =
119 Object.defineProperty && Object.getOwnPropertyDescriptor;
120 for (var key in obj) {
121 if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
122 var desc = hasPropertyDescriptor
123 ? Object.getOwnPropertyDescriptor(obj, key)
124 : null;
125 if (desc && (desc.get || desc.set)) {
126 Object.defineProperty(newObj, key, desc);
127 } else {
128 newObj[key] = obj[key];
129 }
130 }
131 }
132 newObj.default = obj;
133 if (cache) {
134 cache.set(obj, newObj);
135 }
136 return newObj;
137}
138
139/**
140 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
141 *
142 * This source code is licensed under the MIT license found in the
143 * LICENSE file in the root directory of this source tree.
144 */
145async function run(maybeArgv, project) {
146 try {
147 const argv = buildArgv(maybeArgv);
148
149 if (argv.init) {
150 await (0, _init.default)();
151 return;
152 }
153
154 const projects = getProjectListFromCLIArgs(argv, project);
155 const {results, globalConfig} = await (0, _core().runCLI)(argv, projects);
156 readResultsAndExit(results, globalConfig);
157 } catch (error) {
158 (0, _jestUtil().clearLine)(process.stderr);
159 (0, _jestUtil().clearLine)(process.stdout);
160
161 if (error !== null && error !== void 0 && error.stack) {
162 console.error(_chalk().default.red(error.stack));
163 } else {
164 console.error(_chalk().default.red(error));
165 }
166
167 (0, _exit().default)(1);
168 throw error;
169 }
170}
171
172const buildArgv = maybeArgv => {
173 const version =
174 (0, _core().getVersion)() +
175 (__dirname.includes(`packages${path().sep}jest-cli`) ? '-dev' : '');
176 const rawArgv = maybeArgv || process.argv.slice(2);
177 const argv = (0, _yargs().default)(rawArgv)
178 .usage(args.usage)
179 .version(version)
180 .alias('help', 'h')
181 .options(args.options)
182 .epilogue(args.docs)
183 .check(args.check).argv;
184 (0, _jestValidate().validateCLIOptions)(
185 argv,
186 {...args.options, deprecationEntries: _jestConfig().deprecationEntries}, // strip leading dashes
187 Array.isArray(rawArgv)
188 ? rawArgv.map(rawArgv => rawArgv.replace(/^--?/, ''))
189 : Object.keys(rawArgv)
190 ); // strip dashed args
191
192 return Object.keys(argv).reduce(
193 (result, key) => {
194 if (!key.includes('-')) {
195 result[key] = argv[key];
196 }
197
198 return result;
199 },
200 {
201 $0: argv.$0,
202 _: argv._
203 }
204 );
205};
206
207exports.buildArgv = buildArgv;
208
209const getProjectListFromCLIArgs = (argv, project) => {
210 const projects = argv.projects ? argv.projects : [];
211
212 if (project) {
213 projects.push(project);
214 }
215
216 if (!projects.length && process.platform === 'win32') {
217 try {
218 projects.push((0, _jestUtil().tryRealpath)(process.cwd()));
219 } catch {
220 // do nothing, just catch error
221 // process.binding('fs').realpath can throw, e.g. on mapped drives
222 }
223 }
224
225 if (!projects.length) {
226 projects.push(process.cwd());
227 }
228
229 return projects;
230};
231
232const readResultsAndExit = (result, globalConfig) => {
233 const code = !result || result.success ? 0 : globalConfig.testFailureExitCode; // Only exit if needed
234
235 process.on('exit', () => {
236 if (typeof code === 'number' && code !== 0) {
237 process.exitCode = code;
238 }
239 });
240
241 if (globalConfig.forceExit) {
242 if (!globalConfig.detectOpenHandles) {
243 console.warn(
244 _chalk().default.bold('Force exiting Jest: ') +
245 'Have you considered using `--detectOpenHandles` to detect ' +
246 'async operations that kept running after all tests finished?'
247 );
248 }
249
250 (0, _exit().default)(code);
251 } else if (!globalConfig.detectOpenHandles) {
252 setTimeout(() => {
253 console.warn(
254 _chalk().default.yellow.bold(
255 'Jest did not exit one second after the test run has completed.\n\n'
256 ) +
257 _chalk().default.yellow(
258 'This usually means that there are asynchronous operations that ' +
259 "weren't stopped in your tests. Consider running Jest with " +
260 '`--detectOpenHandles` to troubleshoot this issue.'
261 )
262 );
263 }, 1000).unref();
264 }
265};