index.js 6.1 KB

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