UNPKG

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