runJest.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _path() {
  7. const data = _interopRequireDefault(require('path'));
  8. _path = function _path() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _chalk() {
  14. const data = _interopRequireDefault(require('chalk'));
  15. _chalk = function _chalk() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _realpathNative() {
  21. const data = require('realpath-native');
  22. _realpathNative = function _realpathNative() {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _console() {
  28. const data = require('@jest/console');
  29. _console = function _console() {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _jestUtil() {
  35. const data = require('jest-util');
  36. _jestUtil = function _jestUtil() {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _exit() {
  42. const data = _interopRequireDefault(require('exit'));
  43. _exit = function _exit() {
  44. return data;
  45. };
  46. return data;
  47. }
  48. function _gracefulFs() {
  49. const data = _interopRequireDefault(require('graceful-fs'));
  50. _gracefulFs = function _gracefulFs() {
  51. return data;
  52. };
  53. return data;
  54. }
  55. function _jestWatcher() {
  56. const data = require('jest-watcher');
  57. _jestWatcher = function _jestWatcher() {
  58. return data;
  59. };
  60. return data;
  61. }
  62. function _testResult() {
  63. const data = require('@jest/test-result');
  64. _testResult = function _testResult() {
  65. return data;
  66. };
  67. return data;
  68. }
  69. var _getNoTestsFoundMessage = _interopRequireDefault(
  70. require('./getNoTestsFoundMessage')
  71. );
  72. var _runGlobalHook = _interopRequireDefault(require('./runGlobalHook'));
  73. var _SearchSource = _interopRequireDefault(require('./SearchSource'));
  74. var _TestScheduler = _interopRequireDefault(require('./TestScheduler'));
  75. var _collectHandles = _interopRequireDefault(require('./collectHandles'));
  76. function _interopRequireDefault(obj) {
  77. return obj && obj.__esModule ? obj : {default: obj};
  78. }
  79. function _objectSpread(target) {
  80. for (var i = 1; i < arguments.length; i++) {
  81. var source = arguments[i] != null ? arguments[i] : {};
  82. var ownKeys = Object.keys(source);
  83. if (typeof Object.getOwnPropertySymbols === 'function') {
  84. ownKeys = ownKeys.concat(
  85. Object.getOwnPropertySymbols(source).filter(function(sym) {
  86. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  87. })
  88. );
  89. }
  90. ownKeys.forEach(function(key) {
  91. _defineProperty(target, key, source[key]);
  92. });
  93. }
  94. return target;
  95. }
  96. function _defineProperty(obj, key, value) {
  97. if (key in obj) {
  98. Object.defineProperty(obj, key, {
  99. value: value,
  100. enumerable: true,
  101. configurable: true,
  102. writable: true
  103. });
  104. } else {
  105. obj[key] = value;
  106. }
  107. return obj;
  108. }
  109. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  110. try {
  111. var info = gen[key](arg);
  112. var value = info.value;
  113. } catch (error) {
  114. reject(error);
  115. return;
  116. }
  117. if (info.done) {
  118. resolve(value);
  119. } else {
  120. Promise.resolve(value).then(_next, _throw);
  121. }
  122. }
  123. function _asyncToGenerator(fn) {
  124. return function() {
  125. var self = this,
  126. args = arguments;
  127. return new Promise(function(resolve, reject) {
  128. var gen = fn.apply(self, args);
  129. function _next(value) {
  130. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'next', value);
  131. }
  132. function _throw(err) {
  133. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'throw', err);
  134. }
  135. _next(undefined);
  136. });
  137. };
  138. }
  139. const getTestPaths =
  140. /*#__PURE__*/
  141. (function() {
  142. var _ref = _asyncToGenerator(function*(
  143. globalConfig,
  144. context,
  145. outputStream,
  146. changedFiles,
  147. jestHooks,
  148. filter
  149. ) {
  150. const source = new _SearchSource.default(context);
  151. const data = yield source.getTestPaths(
  152. globalConfig,
  153. changedFiles,
  154. filter
  155. );
  156. if (!data.tests.length && globalConfig.onlyChanged && data.noSCM) {
  157. new (_console()).CustomConsole(outputStream, outputStream).log(
  158. 'Jest can only find uncommitted changed files in a git or hg ' +
  159. 'repository. If you make your project a git or hg ' +
  160. 'repository (`git init` or `hg init`), Jest will be able ' +
  161. 'to only run tests related to files changed since the last ' +
  162. 'commit.'
  163. );
  164. }
  165. const shouldTestArray = yield Promise.all(
  166. data.tests.map(test =>
  167. jestHooks.shouldRunTestSuite({
  168. config: test.context.config,
  169. duration: test.duration,
  170. testPath: test.path
  171. })
  172. )
  173. );
  174. const filteredTests = data.tests.filter((_test, i) => shouldTestArray[i]);
  175. return _objectSpread({}, data, {
  176. allTests: filteredTests.length,
  177. tests: filteredTests
  178. });
  179. });
  180. return function getTestPaths(_x, _x2, _x3, _x4, _x5, _x6) {
  181. return _ref.apply(this, arguments);
  182. };
  183. })();
  184. const processResults = (runResults, options) => {
  185. const outputFile = options.outputFile,
  186. isJSON = options.json,
  187. onComplete = options.onComplete,
  188. outputStream = options.outputStream,
  189. testResultsProcessor = options.testResultsProcessor,
  190. collectHandles = options.collectHandles;
  191. if (collectHandles) {
  192. runResults.openHandles = collectHandles();
  193. } else {
  194. runResults.openHandles = [];
  195. }
  196. if (testResultsProcessor) {
  197. runResults = require(testResultsProcessor)(runResults);
  198. }
  199. if (isJSON) {
  200. if (outputFile) {
  201. const cwd = (0, _realpathNative().sync)(process.cwd());
  202. const filePath = _path().default.resolve(cwd, outputFile);
  203. _gracefulFs().default.writeFileSync(
  204. filePath,
  205. JSON.stringify((0, _jestUtil().formatTestResults)(runResults))
  206. );
  207. outputStream.write(
  208. `Test results written to: ${_path().default.relative(cwd, filePath)}\n`
  209. );
  210. } else {
  211. process.stdout.write(
  212. JSON.stringify((0, _jestUtil().formatTestResults)(runResults))
  213. );
  214. }
  215. }
  216. return onComplete && onComplete(runResults);
  217. };
  218. const testSchedulerContext = {
  219. firstRun: true,
  220. previousSuccess: true
  221. };
  222. var _default =
  223. /*#__PURE__*/
  224. (function() {
  225. var _runJest = _asyncToGenerator(function*({
  226. contexts,
  227. globalConfig,
  228. outputStream,
  229. testWatcher,
  230. jestHooks = new (_jestWatcher()).JestHook().getEmitter(),
  231. startRun,
  232. changedFilesPromise,
  233. onComplete,
  234. failedTestsCache,
  235. filter
  236. }) {
  237. const Sequencer = (0, _jestUtil().interopRequireDefault)(
  238. require(globalConfig.testSequencer)
  239. ).default;
  240. const sequencer = new Sequencer();
  241. let allTests = [];
  242. if (changedFilesPromise && globalConfig.watch) {
  243. const _ref2 = yield changedFilesPromise,
  244. repos = _ref2.repos;
  245. const noSCM = Object.keys(repos).every(scm => repos[scm].size === 0);
  246. if (noSCM) {
  247. process.stderr.write(
  248. '\n' +
  249. _chalk().default.bold('--watch') +
  250. ' is not supported without git/hg, please use --watchAll ' +
  251. '\n'
  252. );
  253. (0, _exit().default)(1);
  254. }
  255. }
  256. const testRunData = yield Promise.all(
  257. contexts.map(
  258. /*#__PURE__*/
  259. (function() {
  260. var _ref3 = _asyncToGenerator(function*(context) {
  261. const matches = yield getTestPaths(
  262. globalConfig,
  263. context,
  264. outputStream,
  265. changedFilesPromise && (yield changedFilesPromise),
  266. jestHooks,
  267. filter
  268. );
  269. allTests = allTests.concat(matches.tests);
  270. return {
  271. context,
  272. matches
  273. };
  274. });
  275. return function(_x8) {
  276. return _ref3.apply(this, arguments);
  277. };
  278. })()
  279. )
  280. );
  281. allTests = yield sequencer.sort(allTests);
  282. if (globalConfig.listTests) {
  283. const testsPaths = Array.from(new Set(allTests.map(test => test.path)));
  284. if (globalConfig.json) {
  285. console.log(JSON.stringify(testsPaths));
  286. } else {
  287. console.log(testsPaths.join('\n'));
  288. }
  289. onComplete &&
  290. onComplete((0, _testResult().makeEmptyAggregatedTestResult)());
  291. return null;
  292. }
  293. if (globalConfig.onlyFailures && failedTestsCache) {
  294. allTests = failedTestsCache.filterTests(allTests);
  295. globalConfig = failedTestsCache.updateConfig(globalConfig);
  296. }
  297. const hasTests = allTests.length > 0;
  298. if (!hasTests) {
  299. const noTestsFoundMessage = (0, _getNoTestsFoundMessage.default)(
  300. testRunData,
  301. globalConfig
  302. );
  303. if (
  304. globalConfig.passWithNoTests ||
  305. globalConfig.findRelatedTests ||
  306. globalConfig.lastCommit ||
  307. globalConfig.onlyChanged
  308. ) {
  309. new (_console()).CustomConsole(outputStream, outputStream).log(
  310. noTestsFoundMessage
  311. );
  312. } else {
  313. new (_console()).CustomConsole(outputStream, outputStream).error(
  314. noTestsFoundMessage
  315. );
  316. (0, _exit().default)(1);
  317. }
  318. } else if (
  319. allTests.length === 1 &&
  320. globalConfig.silent !== true &&
  321. globalConfig.verbose !== false
  322. ) {
  323. const newConfig = _objectSpread({}, globalConfig, {
  324. verbose: true
  325. });
  326. globalConfig = Object.freeze(newConfig);
  327. }
  328. let collectHandles;
  329. if (globalConfig.detectOpenHandles) {
  330. collectHandles = (0, _collectHandles.default)();
  331. }
  332. if (hasTests) {
  333. yield (0, _runGlobalHook.default)({
  334. allTests,
  335. globalConfig,
  336. moduleName: 'globalSetup'
  337. });
  338. }
  339. if (changedFilesPromise) {
  340. testSchedulerContext.changedFiles = (yield changedFilesPromise).changedFiles;
  341. }
  342. const results = yield new _TestScheduler.default(
  343. globalConfig,
  344. {
  345. startRun
  346. },
  347. testSchedulerContext
  348. ).scheduleTests(allTests, testWatcher);
  349. sequencer.cacheResults(allTests, results);
  350. if (hasTests) {
  351. yield (0, _runGlobalHook.default)({
  352. allTests,
  353. globalConfig,
  354. moduleName: 'globalTeardown'
  355. });
  356. }
  357. return processResults(results, {
  358. collectHandles,
  359. json: globalConfig.json,
  360. onComplete,
  361. outputFile: globalConfig.outputFile,
  362. outputStream,
  363. testResultsProcessor: globalConfig.testResultsProcessor
  364. });
  365. });
  366. function runJest(_x7) {
  367. return _runJest.apply(this, arguments);
  368. }
  369. return runJest;
  370. })();
  371. exports.default = _default;