UNPKG

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