runJest.js 9.3 KB

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