index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.runCLI = runCLI;
  6. function _chalk() {
  7. const data = _interopRequireDefault(require('chalk'));
  8. _chalk = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _exit() {
  14. const data = _interopRequireDefault(require('exit'));
  15. _exit = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _rimraf() {
  21. const data = _interopRequireDefault(require('rimraf'));
  22. _rimraf = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _console() {
  28. const data = require('@jest/console');
  29. _console = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _jestConfig() {
  35. const data = require('jest-config');
  36. _jestConfig = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _jestHasteMap() {
  42. const data = _interopRequireDefault(require('jest-haste-map'));
  43. _jestHasteMap = function () {
  44. return data;
  45. };
  46. return data;
  47. }
  48. function _jestRuntime() {
  49. const data = _interopRequireDefault(require('jest-runtime'));
  50. _jestRuntime = function () {
  51. return data;
  52. };
  53. return data;
  54. }
  55. function _jestUtil() {
  56. const data = require('jest-util');
  57. _jestUtil = function () {
  58. return data;
  59. };
  60. return data;
  61. }
  62. function _TestWatcher() {
  63. const data = _interopRequireDefault(require('../TestWatcher'));
  64. _TestWatcher = function () {
  65. return data;
  66. };
  67. return data;
  68. }
  69. function _collectHandles() {
  70. const data = require('../collectHandles');
  71. _collectHandles = function () {
  72. return data;
  73. };
  74. return data;
  75. }
  76. function _getChangedFilesPromise() {
  77. const data = _interopRequireDefault(require('../getChangedFilesPromise'));
  78. _getChangedFilesPromise = function () {
  79. return data;
  80. };
  81. return data;
  82. }
  83. function _getConfigsOfProjectsToRun() {
  84. const data = _interopRequireDefault(require('../getConfigsOfProjectsToRun'));
  85. _getConfigsOfProjectsToRun = function () {
  86. return data;
  87. };
  88. return data;
  89. }
  90. function _getProjectNamesMissingWarning() {
  91. const data = _interopRequireDefault(
  92. require('../getProjectNamesMissingWarning')
  93. );
  94. _getProjectNamesMissingWarning = function () {
  95. return data;
  96. };
  97. return data;
  98. }
  99. function _getSelectProjectsMessage() {
  100. const data = _interopRequireDefault(require('../getSelectProjectsMessage'));
  101. _getSelectProjectsMessage = function () {
  102. return data;
  103. };
  104. return data;
  105. }
  106. function _createContext() {
  107. const data = _interopRequireDefault(require('../lib/createContext'));
  108. _createContext = function () {
  109. return data;
  110. };
  111. return data;
  112. }
  113. function _handleDeprecationWarnings() {
  114. const data = _interopRequireDefault(
  115. require('../lib/handleDeprecationWarnings')
  116. );
  117. _handleDeprecationWarnings = function () {
  118. return data;
  119. };
  120. return data;
  121. }
  122. function _logDebugMessages() {
  123. const data = _interopRequireDefault(require('../lib/logDebugMessages'));
  124. _logDebugMessages = function () {
  125. return data;
  126. };
  127. return data;
  128. }
  129. function _pluralize() {
  130. const data = _interopRequireDefault(require('../pluralize'));
  131. _pluralize = function () {
  132. return data;
  133. };
  134. return data;
  135. }
  136. function _runJest() {
  137. const data = _interopRequireDefault(require('../runJest'));
  138. _runJest = function () {
  139. return data;
  140. };
  141. return data;
  142. }
  143. function _watch() {
  144. const data = _interopRequireDefault(require('../watch'));
  145. _watch = function () {
  146. return data;
  147. };
  148. return data;
  149. }
  150. function _interopRequireDefault(obj) {
  151. return obj && obj.__esModule ? obj : {default: obj};
  152. }
  153. /**
  154. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  155. *
  156. * This source code is licensed under the MIT license found in the
  157. * LICENSE file in the root directory of this source tree.
  158. */
  159. const {print: preRunMessagePrint} = _jestUtil().preRunMessage;
  160. async function runCLI(argv, projects) {
  161. let results; // If we output a JSON object, we can't write anything to stdout, since
  162. // it'll break the JSON structure and it won't be valid.
  163. const outputStream =
  164. argv.json || argv.useStderr ? process.stderr : process.stdout;
  165. const {globalConfig, configs, hasDeprecationWarnings} = await (0,
  166. _jestConfig().readConfigs)(argv, projects);
  167. if (argv.debug) {
  168. (0, _logDebugMessages().default)(globalConfig, configs, outputStream);
  169. }
  170. if (argv.showConfig) {
  171. (0, _logDebugMessages().default)(globalConfig, configs, process.stdout);
  172. (0, _exit().default)(0);
  173. }
  174. if (argv.clearCache) {
  175. configs.forEach(config => {
  176. _rimraf().default.sync(config.cacheDirectory);
  177. process.stdout.write(`Cleared ${config.cacheDirectory}\n`);
  178. });
  179. (0, _exit().default)(0);
  180. }
  181. let configsOfProjectsToRun = configs;
  182. if (argv.selectProjects) {
  183. const namesMissingWarning = (0, _getProjectNamesMissingWarning().default)(
  184. configs
  185. );
  186. if (namesMissingWarning) {
  187. outputStream.write(namesMissingWarning);
  188. }
  189. configsOfProjectsToRun = (0, _getConfigsOfProjectsToRun().default)(
  190. argv.selectProjects,
  191. configs
  192. );
  193. outputStream.write(
  194. (0, _getSelectProjectsMessage().default)(configsOfProjectsToRun)
  195. );
  196. }
  197. await _run10000(
  198. globalConfig,
  199. configsOfProjectsToRun,
  200. hasDeprecationWarnings,
  201. outputStream,
  202. r => (results = r)
  203. );
  204. if (argv.watch || argv.watchAll) {
  205. // If in watch mode, return the promise that will never resolve.
  206. // If the watch mode is interrupted, watch should handle the process
  207. // shutdown.
  208. return new Promise(() => {});
  209. }
  210. if (!results) {
  211. throw new Error(
  212. 'AggregatedResult must be present after test run is complete'
  213. );
  214. }
  215. const {openHandles} = results;
  216. if (openHandles && openHandles.length) {
  217. const formatted = (0, _collectHandles().formatHandleErrors)(
  218. openHandles,
  219. configs[0]
  220. );
  221. const openHandlesString = (0, _pluralize().default)(
  222. 'open handle',
  223. formatted.length,
  224. 's'
  225. );
  226. const message =
  227. _chalk().default.red(
  228. `\nJest has detected the following ${openHandlesString} potentially keeping Jest from exiting:\n\n`
  229. ) + formatted.join('\n\n');
  230. console.error(message);
  231. }
  232. return {
  233. globalConfig,
  234. results
  235. };
  236. }
  237. const buildContextsAndHasteMaps = async (
  238. configs,
  239. globalConfig,
  240. outputStream
  241. ) => {
  242. const hasteMapInstances = Array(configs.length);
  243. const contexts = await Promise.all(
  244. configs.map(async (config, index) => {
  245. (0, _jestUtil().createDirectory)(config.cacheDirectory);
  246. const hasteMapInstance = _jestRuntime().default.createHasteMap(config, {
  247. console: new (_console().CustomConsole)(outputStream, outputStream),
  248. maxWorkers: Math.max(
  249. 1,
  250. Math.floor(globalConfig.maxWorkers / configs.length)
  251. ),
  252. resetCache: !config.cache,
  253. watch: globalConfig.watch || globalConfig.watchAll,
  254. watchman: globalConfig.watchman
  255. });
  256. hasteMapInstances[index] = hasteMapInstance;
  257. return (0, _createContext().default)(
  258. config,
  259. await hasteMapInstance.build()
  260. );
  261. })
  262. );
  263. return {
  264. contexts,
  265. hasteMapInstances
  266. };
  267. };
  268. const _run10000 = async (
  269. globalConfig,
  270. configs,
  271. hasDeprecationWarnings,
  272. outputStream,
  273. onComplete
  274. ) => {
  275. // Queries to hg/git can take a while, so we need to start the process
  276. // as soon as possible, so by the time we need the result it's already there.
  277. const changedFilesPromise = (0, _getChangedFilesPromise().default)(
  278. globalConfig,
  279. configs
  280. ); // Filter may need to do an HTTP call or something similar to setup.
  281. // We will wait on an async response from this before using the filter.
  282. let filter;
  283. if (globalConfig.filter && !globalConfig.skipFilter) {
  284. const rawFilter = require(globalConfig.filter);
  285. let filterSetupPromise;
  286. if (rawFilter.setup) {
  287. // Wrap filter setup Promise to avoid "uncaught Promise" error.
  288. // If an error is returned, we surface it in the return value.
  289. filterSetupPromise = (async () => {
  290. try {
  291. await rawFilter.setup();
  292. } catch (err) {
  293. return err;
  294. }
  295. return undefined;
  296. })();
  297. }
  298. filter = async testPaths => {
  299. if (filterSetupPromise) {
  300. // Expect an undefined return value unless there was an error.
  301. const err = await filterSetupPromise;
  302. if (err) {
  303. throw err;
  304. }
  305. }
  306. return rawFilter(testPaths);
  307. };
  308. }
  309. const {contexts, hasteMapInstances} = await buildContextsAndHasteMaps(
  310. configs,
  311. globalConfig,
  312. outputStream
  313. );
  314. globalConfig.watch || globalConfig.watchAll
  315. ? await runWatch(
  316. contexts,
  317. configs,
  318. hasDeprecationWarnings,
  319. globalConfig,
  320. outputStream,
  321. hasteMapInstances,
  322. filter
  323. )
  324. : await runWithoutWatch(
  325. globalConfig,
  326. contexts,
  327. outputStream,
  328. onComplete,
  329. changedFilesPromise,
  330. filter
  331. );
  332. };
  333. const runWatch = async (
  334. contexts,
  335. _configs,
  336. hasDeprecationWarnings,
  337. globalConfig,
  338. outputStream,
  339. hasteMapInstances,
  340. filter
  341. ) => {
  342. if (hasDeprecationWarnings) {
  343. try {
  344. await (0, _handleDeprecationWarnings().default)(
  345. outputStream,
  346. process.stdin
  347. );
  348. return (0, _watch().default)(
  349. globalConfig,
  350. contexts,
  351. outputStream,
  352. hasteMapInstances,
  353. undefined,
  354. undefined,
  355. filter
  356. );
  357. } catch {
  358. (0, _exit().default)(0);
  359. }
  360. }
  361. return (0, _watch().default)(
  362. globalConfig,
  363. contexts,
  364. outputStream,
  365. hasteMapInstances,
  366. undefined,
  367. undefined,
  368. filter
  369. );
  370. };
  371. const runWithoutWatch = async (
  372. globalConfig,
  373. contexts,
  374. outputStream,
  375. onComplete,
  376. changedFilesPromise,
  377. filter
  378. ) => {
  379. const startRun = async () => {
  380. if (!globalConfig.listTests) {
  381. preRunMessagePrint(outputStream);
  382. }
  383. return (0, _runJest().default)({
  384. changedFilesPromise,
  385. contexts,
  386. failedTestsCache: undefined,
  387. filter,
  388. globalConfig,
  389. onComplete,
  390. outputStream,
  391. startRun,
  392. testWatcher: new (_TestWatcher().default)({
  393. isWatchMode: false
  394. })
  395. });
  396. };
  397. return startRun();
  398. };