runJest.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = runJest;
  6. function path() {
  7. const data = _interopRequireWildcard(require('path'));
  8. path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _chalk() {
  14. const data = _interopRequireDefault(require('chalk'));
  15. _chalk = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _exit() {
  21. const data = _interopRequireDefault(require('exit'));
  22. _exit = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function fs() {
  28. const data = _interopRequireWildcard(require('graceful-fs'));
  29. fs = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _console() {
  35. const data = require('@jest/console');
  36. _console = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _testResult() {
  42. const data = require('@jest/test-result');
  43. _testResult = function () {
  44. return data;
  45. };
  46. return data;
  47. }
  48. function _jestResolve() {
  49. const data = _interopRequireDefault(require('jest-resolve'));
  50. _jestResolve = 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 _jestWatcher() {
  63. const data = require('jest-watcher');
  64. _jestWatcher = function () {
  65. return data;
  66. };
  67. return data;
  68. }
  69. var _SearchSource = _interopRequireDefault(require('./SearchSource'));
  70. var _TestScheduler = require('./TestScheduler');
  71. var _collectHandles = _interopRequireDefault(require('./collectHandles'));
  72. var _getNoTestsFoundMessage = _interopRequireDefault(
  73. require('./getNoTestsFoundMessage')
  74. );
  75. var _runGlobalHook = _interopRequireDefault(require('./runGlobalHook'));
  76. function _interopRequireDefault(obj) {
  77. return obj && obj.__esModule ? obj : {default: obj};
  78. }
  79. function _getRequireWildcardCache(nodeInterop) {
  80. if (typeof WeakMap !== 'function') return null;
  81. var cacheBabelInterop = new WeakMap();
  82. var cacheNodeInterop = new WeakMap();
  83. return (_getRequireWildcardCache = function (nodeInterop) {
  84. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  85. })(nodeInterop);
  86. }
  87. function _interopRequireWildcard(obj, nodeInterop) {
  88. if (!nodeInterop && obj && obj.__esModule) {
  89. return obj;
  90. }
  91. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  92. return {default: obj};
  93. }
  94. var cache = _getRequireWildcardCache(nodeInterop);
  95. if (cache && cache.has(obj)) {
  96. return cache.get(obj);
  97. }
  98. var newObj = {};
  99. var hasPropertyDescriptor =
  100. Object.defineProperty && Object.getOwnPropertyDescriptor;
  101. for (var key in obj) {
  102. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  103. var desc = hasPropertyDescriptor
  104. ? Object.getOwnPropertyDescriptor(obj, key)
  105. : null;
  106. if (desc && (desc.get || desc.set)) {
  107. Object.defineProperty(newObj, key, desc);
  108. } else {
  109. newObj[key] = obj[key];
  110. }
  111. }
  112. }
  113. newObj.default = obj;
  114. if (cache) {
  115. cache.set(obj, newObj);
  116. }
  117. return newObj;
  118. }
  119. /**
  120. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  121. *
  122. * This source code is licensed under the MIT license found in the
  123. * LICENSE file in the root directory of this source tree.
  124. */
  125. const getTestPaths = async (
  126. globalConfig,
  127. source,
  128. outputStream,
  129. changedFiles,
  130. jestHooks,
  131. filter
  132. ) => {
  133. const data = await source.getTestPaths(globalConfig, changedFiles, filter);
  134. if (!data.tests.length && globalConfig.onlyChanged && data.noSCM) {
  135. new (_console().CustomConsole)(outputStream, outputStream).log(
  136. 'Jest can only find uncommitted changed files in a git or hg ' +
  137. 'repository. If you make your project a git or hg ' +
  138. 'repository (`git init` or `hg init`), Jest will be able ' +
  139. 'to only run tests related to files changed since the last ' +
  140. 'commit.'
  141. );
  142. }
  143. const shouldTestArray = await Promise.all(
  144. data.tests.map(test =>
  145. jestHooks.shouldRunTestSuite({
  146. config: test.context.config,
  147. duration: test.duration,
  148. testPath: test.path
  149. })
  150. )
  151. );
  152. const filteredTests = data.tests.filter((_test, i) => shouldTestArray[i]);
  153. return {...data, allTests: filteredTests.length, tests: filteredTests};
  154. };
  155. const processResults = async (runResults, options) => {
  156. const {
  157. outputFile,
  158. json: isJSON,
  159. onComplete,
  160. outputStream,
  161. testResultsProcessor,
  162. collectHandles
  163. } = options;
  164. if (collectHandles) {
  165. runResults.openHandles = await collectHandles();
  166. } else {
  167. runResults.openHandles = [];
  168. }
  169. if (testResultsProcessor) {
  170. const processor = await (0, _jestUtil().requireOrImportModule)(
  171. testResultsProcessor
  172. );
  173. runResults = processor(runResults);
  174. }
  175. if (isJSON) {
  176. if (outputFile) {
  177. const cwd = (0, _jestUtil().tryRealpath)(process.cwd());
  178. const filePath = path().resolve(cwd, outputFile);
  179. fs().writeFileSync(
  180. filePath,
  181. JSON.stringify((0, _testResult().formatTestResults)(runResults))
  182. );
  183. outputStream.write(
  184. `Test results written to: ${path().relative(cwd, filePath)}\n`
  185. );
  186. } else {
  187. process.stdout.write(
  188. JSON.stringify((0, _testResult().formatTestResults)(runResults))
  189. );
  190. }
  191. }
  192. onComplete === null || onComplete === void 0
  193. ? void 0
  194. : onComplete(runResults);
  195. };
  196. const testSchedulerContext = {
  197. firstRun: true,
  198. previousSuccess: true
  199. };
  200. async function runJest({
  201. contexts,
  202. globalConfig,
  203. outputStream,
  204. testWatcher,
  205. jestHooks = new (_jestWatcher().JestHook)().getEmitter(),
  206. startRun,
  207. changedFilesPromise,
  208. onComplete,
  209. failedTestsCache,
  210. filter
  211. }) {
  212. // Clear cache for required modules - there might be different resolutions
  213. // from Jest's config loading to running the tests
  214. _jestResolve().default.clearDefaultResolverCache();
  215. const Sequencer = await (0, _jestUtil().requireOrImportModule)(
  216. globalConfig.testSequencer
  217. );
  218. const sequencer = new Sequencer();
  219. let allTests = [];
  220. if (changedFilesPromise && globalConfig.watch) {
  221. const {repos} = await changedFilesPromise;
  222. const noSCM = Object.keys(repos).every(scm => repos[scm].size === 0);
  223. if (noSCM) {
  224. process.stderr.write(
  225. '\n' +
  226. _chalk().default.bold('--watch') +
  227. ' is not supported without git/hg, please use --watchAll ' +
  228. '\n'
  229. );
  230. (0, _exit().default)(1);
  231. }
  232. }
  233. const searchSources = contexts.map(
  234. context => new _SearchSource.default(context)
  235. );
  236. const testRunData = await Promise.all(
  237. contexts.map(async (context, index) => {
  238. const searchSource = searchSources[index];
  239. const matches = await getTestPaths(
  240. globalConfig,
  241. searchSource,
  242. outputStream,
  243. changedFilesPromise && (await changedFilesPromise),
  244. jestHooks,
  245. filter
  246. );
  247. allTests = allTests.concat(matches.tests);
  248. return {
  249. context,
  250. matches
  251. };
  252. })
  253. );
  254. allTests = await sequencer.sort(allTests);
  255. if (globalConfig.listTests) {
  256. const testsPaths = Array.from(new Set(allTests.map(test => test.path)));
  257. /* eslint-disable no-console */
  258. if (globalConfig.json) {
  259. console.log(JSON.stringify(testsPaths));
  260. } else {
  261. console.log(testsPaths.join('\n'));
  262. }
  263. /* eslint-enable */
  264. onComplete &&
  265. onComplete((0, _testResult().makeEmptyAggregatedTestResult)());
  266. return;
  267. }
  268. if (globalConfig.onlyFailures) {
  269. if (failedTestsCache) {
  270. allTests = failedTestsCache.filterTests(allTests);
  271. } else {
  272. allTests = await sequencer.allFailedTests(allTests);
  273. }
  274. }
  275. const hasTests = allTests.length > 0;
  276. if (!hasTests) {
  277. const noTestsFoundMessage = (0, _getNoTestsFoundMessage.default)(
  278. testRunData,
  279. globalConfig
  280. );
  281. if (
  282. globalConfig.passWithNoTests ||
  283. globalConfig.findRelatedTests ||
  284. globalConfig.lastCommit ||
  285. globalConfig.onlyChanged
  286. ) {
  287. new (_console().CustomConsole)(outputStream, outputStream).log(
  288. noTestsFoundMessage
  289. );
  290. } else {
  291. new (_console().CustomConsole)(outputStream, outputStream).error(
  292. noTestsFoundMessage
  293. );
  294. (0, _exit().default)(1);
  295. }
  296. } else if (
  297. allTests.length === 1 &&
  298. globalConfig.silent !== true &&
  299. globalConfig.verbose !== false
  300. ) {
  301. const newConfig = {...globalConfig, verbose: true};
  302. globalConfig = Object.freeze(newConfig);
  303. }
  304. let collectHandles;
  305. if (globalConfig.detectOpenHandles) {
  306. collectHandles = (0, _collectHandles.default)();
  307. }
  308. if (hasTests) {
  309. await (0, _runGlobalHook.default)({
  310. allTests,
  311. globalConfig,
  312. moduleName: 'globalSetup'
  313. });
  314. }
  315. if (changedFilesPromise) {
  316. const changedFilesInfo = await changedFilesPromise;
  317. if (changedFilesInfo.changedFiles) {
  318. testSchedulerContext.changedFiles = changedFilesInfo.changedFiles;
  319. const sourcesRelatedToTestsInChangedFilesArray = (
  320. await Promise.all(
  321. contexts.map(async (_, index) => {
  322. const searchSource = searchSources[index];
  323. return searchSource.findRelatedSourcesFromTestsInChangedFiles(
  324. changedFilesInfo
  325. );
  326. })
  327. )
  328. ).reduce((total, paths) => total.concat(paths), []);
  329. testSchedulerContext.sourcesRelatedToTestsInChangedFiles = new Set(
  330. sourcesRelatedToTestsInChangedFilesArray
  331. );
  332. }
  333. }
  334. const scheduler = await (0, _TestScheduler.createTestScheduler)(
  335. globalConfig,
  336. {
  337. startRun
  338. },
  339. testSchedulerContext
  340. );
  341. const results = await scheduler.scheduleTests(allTests, testWatcher);
  342. await sequencer.cacheResults(allTests, results);
  343. if (hasTests) {
  344. await (0, _runGlobalHook.default)({
  345. allTests,
  346. globalConfig,
  347. moduleName: 'globalTeardown'
  348. });
  349. }
  350. await processResults(results, {
  351. collectHandles,
  352. json: globalConfig.json,
  353. onComplete,
  354. outputFile: globalConfig.outputFile,
  355. outputStream,
  356. testResultsProcessor: globalConfig.testResultsProcessor
  357. });
  358. }