UNPKG

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