1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = runJest;
|
7 |
|
8 | function path() {
|
9 | const data = _interopRequireWildcard(require('path'));
|
10 |
|
11 | path = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function _chalk() {
|
19 | const data = _interopRequireDefault(require('chalk'));
|
20 |
|
21 | _chalk = function () {
|
22 | return data;
|
23 | };
|
24 |
|
25 | return data;
|
26 | }
|
27 |
|
28 | function _console() {
|
29 | const data = require('@jest/console');
|
30 |
|
31 | _console = function () {
|
32 | return data;
|
33 | };
|
34 |
|
35 | return data;
|
36 | }
|
37 |
|
38 | function _jestUtil() {
|
39 | const data = require('jest-util');
|
40 |
|
41 | _jestUtil = function () {
|
42 | return data;
|
43 | };
|
44 |
|
45 | return data;
|
46 | }
|
47 |
|
48 | function _exit() {
|
49 | const data = _interopRequireDefault(require('exit'));
|
50 |
|
51 | _exit = function () {
|
52 | return data;
|
53 | };
|
54 |
|
55 | return data;
|
56 | }
|
57 |
|
58 | function fs() {
|
59 | const data = _interopRequireWildcard(require('graceful-fs'));
|
60 |
|
61 | fs = function () {
|
62 | return data;
|
63 | };
|
64 |
|
65 | return data;
|
66 | }
|
67 |
|
68 | function _jestWatcher() {
|
69 | const data = require('jest-watcher');
|
70 |
|
71 | _jestWatcher = function () {
|
72 | return data;
|
73 | };
|
74 |
|
75 | return data;
|
76 | }
|
77 |
|
78 | function _testResult() {
|
79 | const data = require('@jest/test-result');
|
80 |
|
81 | _testResult = function () {
|
82 | return data;
|
83 | };
|
84 |
|
85 | return data;
|
86 | }
|
87 |
|
88 | var _getNoTestsFoundMessage = _interopRequireDefault(
|
89 | require('./getNoTestsFoundMessage')
|
90 | );
|
91 |
|
92 | var _runGlobalHook = _interopRequireDefault(require('./runGlobalHook'));
|
93 |
|
94 | var _SearchSource = _interopRequireDefault(require('./SearchSource'));
|
95 |
|
96 | var _TestScheduler = _interopRequireDefault(require('./TestScheduler'));
|
97 |
|
98 | var _collectHandles = _interopRequireDefault(require('./collectHandles'));
|
99 |
|
100 | function _interopRequireDefault(obj) {
|
101 | return obj && obj.__esModule ? obj : {default: obj};
|
102 | }
|
103 |
|
104 | function _getRequireWildcardCache() {
|
105 | if (typeof WeakMap !== 'function') return null;
|
106 | var cache = new WeakMap();
|
107 | _getRequireWildcardCache = function () {
|
108 | return cache;
|
109 | };
|
110 | return cache;
|
111 | }
|
112 |
|
113 | function _interopRequireWildcard(obj) {
|
114 | if (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();
|
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 (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 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | const 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 |
|
185 | const processResults = (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 = 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 | return onComplete && onComplete(runResults);
|
224 | };
|
225 |
|
226 | const testSchedulerContext = {
|
227 | firstRun: true,
|
228 | previousSuccess: true
|
229 | };
|
230 |
|
231 | async function runJest({
|
232 | contexts,
|
233 | globalConfig,
|
234 | outputStream,
|
235 | testWatcher,
|
236 | jestHooks = new (_jestWatcher().JestHook)().getEmitter(),
|
237 | startRun,
|
238 | changedFilesPromise,
|
239 | onComplete,
|
240 | failedTestsCache,
|
241 | filter
|
242 | }) {
|
243 | const Sequencer = (0, _jestUtil().interopRequireDefault)(
|
244 | require(globalConfig.testSequencer)
|
245 | ).default;
|
246 | const sequencer = new Sequencer();
|
247 | let allTests = [];
|
248 |
|
249 | if (changedFilesPromise && globalConfig.watch) {
|
250 | const {repos} = await changedFilesPromise;
|
251 | const noSCM = Object.keys(repos).every(scm => repos[scm].size === 0);
|
252 |
|
253 | if (noSCM) {
|
254 | process.stderr.write(
|
255 | '\n' +
|
256 | _chalk().default.bold('--watch') +
|
257 | ' is not supported without git/hg, please use --watchAll ' +
|
258 | '\n'
|
259 | );
|
260 | (0, _exit().default)(1);
|
261 | }
|
262 | }
|
263 |
|
264 | const searchSources = contexts.map(
|
265 | context => new _SearchSource.default(context)
|
266 | );
|
267 | const testRunData = await Promise.all(
|
268 | contexts.map(async (context, index) => {
|
269 | const searchSource = searchSources[index];
|
270 | const matches = await getTestPaths(
|
271 | globalConfig,
|
272 | searchSource,
|
273 | outputStream,
|
274 | changedFilesPromise && (await changedFilesPromise),
|
275 | jestHooks,
|
276 | filter
|
277 | );
|
278 | allTests = allTests.concat(matches.tests);
|
279 | return {
|
280 | context,
|
281 | matches
|
282 | };
|
283 | })
|
284 | );
|
285 | allTests = await sequencer.sort(allTests);
|
286 |
|
287 | if (globalConfig.listTests) {
|
288 | const testsPaths = Array.from(new Set(allTests.map(test => test.path)));
|
289 |
|
290 | if (globalConfig.json) {
|
291 | console.log(JSON.stringify(testsPaths));
|
292 | } else {
|
293 | console.log(testsPaths.join('\n'));
|
294 | }
|
295 |
|
296 | onComplete &&
|
297 | onComplete((0, _testResult().makeEmptyAggregatedTestResult)());
|
298 | return;
|
299 | }
|
300 |
|
301 | if (globalConfig.onlyFailures && failedTestsCache) {
|
302 | allTests = failedTestsCache.filterTests(allTests);
|
303 | globalConfig = failedTestsCache.updateConfig(globalConfig);
|
304 | }
|
305 |
|
306 | const hasTests = allTests.length > 0;
|
307 |
|
308 | if (!hasTests) {
|
309 | const noTestsFoundMessage = (0, _getNoTestsFoundMessage.default)(
|
310 | testRunData,
|
311 | globalConfig
|
312 | );
|
313 |
|
314 | if (
|
315 | globalConfig.passWithNoTests ||
|
316 | globalConfig.findRelatedTests ||
|
317 | globalConfig.lastCommit ||
|
318 | globalConfig.onlyChanged
|
319 | ) {
|
320 | new (_console().CustomConsole)(outputStream, outputStream).log(
|
321 | noTestsFoundMessage
|
322 | );
|
323 | } else {
|
324 | new (_console().CustomConsole)(outputStream, outputStream).error(
|
325 | noTestsFoundMessage
|
326 | );
|
327 | (0, _exit().default)(1);
|
328 | }
|
329 | } else if (
|
330 | allTests.length === 1 &&
|
331 | globalConfig.silent !== true &&
|
332 | globalConfig.verbose !== false
|
333 | ) {
|
334 | const newConfig = {...globalConfig, verbose: true};
|
335 | globalConfig = Object.freeze(newConfig);
|
336 | }
|
337 |
|
338 | let collectHandles;
|
339 |
|
340 | if (globalConfig.detectOpenHandles) {
|
341 | collectHandles = (0, _collectHandles.default)();
|
342 | }
|
343 |
|
344 | if (hasTests) {
|
345 | await (0, _runGlobalHook.default)({
|
346 | allTests,
|
347 | globalConfig,
|
348 | moduleName: 'globalSetup'
|
349 | });
|
350 | }
|
351 |
|
352 | if (changedFilesPromise) {
|
353 | const changedFilesInfo = await changedFilesPromise;
|
354 |
|
355 | if (changedFilesInfo.changedFiles) {
|
356 | testSchedulerContext.changedFiles = changedFilesInfo.changedFiles;
|
357 | const sourcesRelatedToTestsInChangedFilesArray = contexts
|
358 | .map((_, index) => {
|
359 | const searchSource = searchSources[index];
|
360 | const relatedSourceFromTestsInChangedFiles = searchSource.findRelatedSourcesFromTestsInChangedFiles(
|
361 | changedFilesInfo
|
362 | );
|
363 | return relatedSourceFromTestsInChangedFiles;
|
364 | })
|
365 | .reduce((total, paths) => total.concat(paths), []);
|
366 | testSchedulerContext.sourcesRelatedToTestsInChangedFiles = new Set(
|
367 | sourcesRelatedToTestsInChangedFilesArray
|
368 | );
|
369 | }
|
370 | }
|
371 |
|
372 | const results = await new _TestScheduler.default(
|
373 | globalConfig,
|
374 | {
|
375 | startRun
|
376 | },
|
377 | testSchedulerContext
|
378 | ).scheduleTests(allTests, testWatcher);
|
379 | sequencer.cacheResults(allTests, results);
|
380 |
|
381 | if (hasTests) {
|
382 | await (0, _runGlobalHook.default)({
|
383 | allTests,
|
384 | globalConfig,
|
385 | moduleName: 'globalTeardown'
|
386 | });
|
387 | }
|
388 |
|
389 | await processResults(results, {
|
390 | collectHandles,
|
391 | json: globalConfig.json,
|
392 | onComplete,
|
393 | outputFile: globalConfig.outputFile,
|
394 | outputStream,
|
395 | testResultsProcessor: globalConfig.testResultsProcessor
|
396 | });
|
397 | }
|