UNPKG

7.41 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 = _interopRequireDefault(require('path'));
11
12 _path = function _path() {
13 return data;
14 };
15
16 return data;
17}
18
19function _jestUtil() {
20 const data = require('jest-util');
21
22 _jestUtil = function _jestUtil() {
23 return data;
24 };
25
26 return data;
27}
28
29function _jestValidate() {
30 const data = require('jest-validate');
31
32 _jestValidate = function _jestValidate() {
33 return data;
34 };
35
36 return data;
37}
38
39function _jestConfig() {
40 const data = require('jest-config');
41
42 _jestConfig = function _jestConfig() {
43 return data;
44 };
45
46 return data;
47}
48
49function _core() {
50 const data = require('@jest/core');
51
52 _core = function _core() {
53 return data;
54 };
55
56 return data;
57}
58
59function _chalk() {
60 const data = _interopRequireDefault(require('chalk'));
61
62 _chalk = function _chalk() {
63 return data;
64 };
65
66 return data;
67}
68
69function _exit() {
70 const data = _interopRequireDefault(require('exit'));
71
72 _exit = function _exit() {
73 return data;
74 };
75
76 return data;
77}
78
79function _yargs() {
80 const data = _interopRequireDefault(require('yargs'));
81
82 _yargs = function _yargs() {
83 return data;
84 };
85
86 return data;
87}
88
89function _realpathNative() {
90 const data = require('realpath-native');
91
92 _realpathNative = function _realpathNative() {
93 return data;
94 };
95
96 return data;
97}
98
99var _init = _interopRequireDefault(require('../init'));
100
101var args = _interopRequireWildcard(require('./args'));
102
103function _interopRequireWildcard(obj) {
104 if (obj && obj.__esModule) {
105 return obj;
106 } else {
107 var newObj = {};
108 if (obj != null) {
109 for (var key in obj) {
110 if (Object.prototype.hasOwnProperty.call(obj, key)) {
111 var desc =
112 Object.defineProperty && Object.getOwnPropertyDescriptor
113 ? Object.getOwnPropertyDescriptor(obj, key)
114 : {};
115 if (desc.get || desc.set) {
116 Object.defineProperty(newObj, key, desc);
117 } else {
118 newObj[key] = obj[key];
119 }
120 }
121 }
122 }
123 newObj.default = obj;
124 return newObj;
125 }
126}
127
128function _interopRequireDefault(obj) {
129 return obj && obj.__esModule ? obj : {default: obj};
130}
131
132function _objectSpread(target) {
133 for (var i = 1; i < arguments.length; i++) {
134 var source = arguments[i] != null ? arguments[i] : {};
135 var ownKeys = Object.keys(source);
136 if (typeof Object.getOwnPropertySymbols === 'function') {
137 ownKeys = ownKeys.concat(
138 Object.getOwnPropertySymbols(source).filter(function(sym) {
139 return Object.getOwnPropertyDescriptor(source, sym).enumerable;
140 })
141 );
142 }
143 ownKeys.forEach(function(key) {
144 _defineProperty(target, key, source[key]);
145 });
146 }
147 return target;
148}
149
150function _defineProperty(obj, key, value) {
151 if (key in obj) {
152 Object.defineProperty(obj, key, {
153 value: value,
154 enumerable: true,
155 configurable: true,
156 writable: true
157 });
158 } else {
159 obj[key] = value;
160 }
161 return obj;
162}
163
164function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
165 try {
166 var info = gen[key](arg);
167 var value = info.value;
168 } catch (error) {
169 reject(error);
170 return;
171 }
172 if (info.done) {
173 resolve(value);
174 } else {
175 Promise.resolve(value).then(_next, _throw);
176 }
177}
178
179function _asyncToGenerator(fn) {
180 return function() {
181 var self = this,
182 args = arguments;
183 return new Promise(function(resolve, reject) {
184 var gen = fn.apply(self, args);
185 function _next(value) {
186 asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'next', value);
187 }
188 function _throw(err) {
189 asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'throw', err);
190 }
191 _next(undefined);
192 });
193 };
194}
195
196function run(_x, _x2) {
197 return _run.apply(this, arguments);
198}
199
200function _run() {
201 _run = _asyncToGenerator(function*(maybeArgv, project) {
202 try {
203 const argv = buildArgv(maybeArgv);
204
205 if (argv.init) {
206 yield (0, _init.default)();
207 return;
208 }
209
210 const projects = getProjectListFromCLIArgs(argv, project);
211
212 const _ref = yield (0, _core().runCLI)(argv, projects),
213 results = _ref.results,
214 globalConfig = _ref.globalConfig;
215
216 readResultsAndExit(results, globalConfig);
217 } catch (error) {
218 (0, _jestUtil().clearLine)(process.stderr);
219 (0, _jestUtil().clearLine)(process.stdout);
220
221 if (error.stack) {
222 console.error(_chalk().default.red(error.stack));
223 } else {
224 console.error(_chalk().default.red(error));
225 }
226
227 (0, _exit().default)(1);
228 throw error;
229 }
230 });
231 return _run.apply(this, arguments);
232}
233
234const buildArgv = maybeArgv => {
235 const version =
236 (0, _core().getVersion)() +
237 (__dirname.includes(`packages${_path().default.sep}jest-cli`)
238 ? '-dev'
239 : '');
240 const rawArgv = maybeArgv || process.argv.slice(2);
241 const argv = (0, _yargs().default)(rawArgv)
242 .usage(args.usage)
243 .version(version)
244 .alias('help', 'h')
245 .options(args.options)
246 .epilogue(args.docs)
247 .check(args.check).argv;
248 (0, _jestValidate().validateCLIOptions)(
249 argv,
250 _objectSpread({}, args.options, {
251 deprecationEntries: _jestConfig().deprecationEntries
252 }), // strip leading dashes
253 Array.isArray(rawArgv)
254 ? rawArgv.map(rawArgv => rawArgv.replace(/^--?/, ''))
255 : Object.keys(rawArgv)
256 ); // strip dashed args
257
258 return Object.keys(argv).reduce((result, key) => {
259 if (!key.includes('-')) {
260 result[key] = argv[key];
261 }
262
263 return result;
264 }, {});
265};
266
267exports.buildArgv = buildArgv;
268
269const getProjectListFromCLIArgs = (argv, project) => {
270 const projects = argv.projects ? argv.projects : [];
271
272 if (project) {
273 projects.push(project);
274 }
275
276 if (!projects.length && process.platform === 'win32') {
277 try {
278 projects.push((0, _realpathNative().sync)(process.cwd()));
279 } catch (err) {
280 // do nothing, just catch error
281 // process.binding('fs').realpath can throw, e.g. on mapped drives
282 }
283 }
284
285 if (!projects.length) {
286 projects.push(process.cwd());
287 }
288
289 return projects;
290};
291
292const readResultsAndExit = (result, globalConfig) => {
293 const code = !result || result.success ? 0 : globalConfig.testFailureExitCode; // Only exit if needed
294
295 process.on('exit', () => {
296 if (typeof code === 'number' && code !== 0) {
297 process.exitCode = code;
298 }
299 });
300
301 if (globalConfig.forceExit) {
302 if (!globalConfig.detectOpenHandles) {
303 console.warn(
304 _chalk().default.bold('Force exiting Jest: ') +
305 'Have you considered using `--detectOpenHandles` to detect ' +
306 'async operations that kept running after all tests finished?'
307 );
308 }
309
310 (0, _exit().default)(code);
311 } else if (!globalConfig.detectOpenHandles) {
312 setTimeout(() => {
313 console.warn(
314 _chalk().default.yellow.bold(
315 'Jest did not exit one second after the test run has completed.\n\n'
316 ) +
317 _chalk().default.yellow(
318 'This usually means that there are asynchronous operations that ' +
319 "weren't stopped in your tests. Consider running Jest with " +
320 '`--detectOpenHandles` to troubleshoot this issue.'
321 )
322 );
323 }, 1000).unref();
324 }
325};