@@ -9,31 +9,16 @@ import {performance} from 'perf_hooks';
99import chalk = require( 'chalk' ) ;
1010import exit = require( 'exit' ) ;
1111import * as fs from 'graceful-fs' ;
12- import { CustomConsole } from '@jest/console' ;
13- import type { AggregatedResult , TestContext } from '@jest/test-result' ;
12+ import type { AggregatedResult } from '@jest/test-result' ;
1413import type { Config } from '@jest/types' ;
15- import type { ChangedFilesPromise } from 'jest-changed-files' ;
1614import { readConfigs } from 'jest-config' ;
17- import type { IHasteMap } from 'jest-haste-map' ;
18- import Runtime from 'jest-runtime' ;
19- import { createDirectory , preRunMessage } from 'jest-util' ;
20- import { TestWatcher } from 'jest-watcher' ;
2115import { formatHandleErrors } from '../collectHandles' ;
22- import getChangedFilesPromise from '../getChangedFilesPromise' ;
2316import getConfigsOfProjectsToRun from '../getConfigsOfProjectsToRun' ;
2417import getProjectNamesMissingWarning from '../getProjectNamesMissingWarning' ;
2518import getSelectProjectsMessage from '../getSelectProjectsMessage' ;
26- import createContext from '../lib/createContext' ;
27- import handleDeprecationWarnings from '../lib/handleDeprecationWarnings' ;
2819import logDebugMessages from '../lib/logDebugMessages' ;
2920import pluralize from '../pluralize' ;
30- import runJest from '../runJest' ;
31- import type { Filter } from '../types' ;
32- import watch from '../watch' ;
33-
34- const { print : preRunMessagePrint } = preRunMessage ;
35-
36- type OnCompleteCallback = ( results : AggregatedResult ) => void | undefined ;
21+ import { _run } from '../runCore' ;
3722
3823export async function runCLI (
3924 argv : Config . Argv ,
@@ -95,7 +80,7 @@ export async function runCLI(
9580 ) ;
9681 }
9782
98- const results = await runCore (
83+ const results = await _run (
9984 globalConfig ,
10085 configsOfProjectsToRun ,
10186 hasDeprecationWarnings ,
@@ -126,196 +111,3 @@ export async function runCLI(
126111 performance . mark ( 'jest/runCLI:end' ) ;
127112 return { globalConfig, results} ;
128113}
129-
130- const buildContextsAndHasteMaps = async (
131- configs : Array < Config . ProjectConfig > ,
132- globalConfig : Config . GlobalConfig ,
133- outputStream : NodeJS . WriteStream ,
134- ) => {
135- const hasteMapInstances = Array ( configs . length ) ;
136- const contexts = await Promise . all (
137- configs . map ( async ( config , index ) => {
138- createDirectory ( config . cacheDirectory ) ;
139- const hasteMapInstance = await Runtime . createHasteMap ( config , {
140- console : new CustomConsole ( outputStream , outputStream ) ,
141- maxWorkers : Math . max (
142- 1 ,
143- Math . floor ( globalConfig . maxWorkers / configs . length ) ,
144- ) ,
145- resetCache : ! config . cache ,
146- watch : globalConfig . watch || globalConfig . watchAll ,
147- watchman : globalConfig . watchman ,
148- workerThreads : globalConfig . workerThreads ,
149- } ) ;
150- hasteMapInstances [ index ] = hasteMapInstance ;
151- return createContext ( config , await hasteMapInstance . build ( ) ) ;
152- } ) ,
153- ) ;
154-
155- return { contexts, hasteMapInstances} ;
156- } ;
157-
158- /**
159- * Runs Jest either in watch mode or as a one-off. This is a lower-level API than `runCLI` and is intended for internal use by `runCLI` or externally.
160- * Note that `process.exit` might be called when using `globalConfig.watch` or `globalConfig.watchAll` is true.
161- *
162- * @param globalConfig The global configuration to use for this run. It can be obtained using `readConfigs` (imported from 'jest-config').
163- * @param configs The project configurations to run. It can be obtained using `readConfigs` (imported from 'jest-config').
164- * @param warnForDeprecations Whether or not to warn for deprecation messages when `globalConfig.watch` or `globalConfig.watchAll` is true.
165- * @param outputStream The stream to write output to. If not provided, it defaults to `process.stdout`.
166- * @returns A Promise that resolves to the result, or never resolves when `globalConfig.watch` or `globalConfig.watchAll` is true.
167- * @example
168- * import { runCore, readConfigs } from 'jest';
169- *
170- * const { globalConfig, configs } = await readConfigs(process.argv, [process.cwd()]);
171- * const results = await runCore(globalConfig, configs);
172- * console.log(results);
173- */
174- export const runCore = async (
175- globalConfig : Config . GlobalConfig ,
176- configs : Array < Config . ProjectConfig > ,
177- warnForDeprecations = false ,
178- outputStream : NodeJS . WriteStream = process . stdout ,
179- ) : Promise < AggregatedResult > => {
180- // Queries to hg/git can take a while, so we need to start the process
181- // as soon as possible, so by the time we need the result it's already there.
182- const changedFilesPromise = getChangedFilesPromise ( globalConfig , configs ) ;
183- if ( changedFilesPromise ) {
184- performance . mark ( 'jest/getChangedFiles:start' ) ;
185- changedFilesPromise . finally ( ( ) => {
186- performance . mark ( 'jest/getChangedFiles:end' ) ;
187- } ) ;
188- }
189-
190- // Filter may need to do an HTTP call or something similar to setup.
191- // We will wait on an async response from this before using the filter.
192- let filter : Filter | undefined ;
193- if ( globalConfig . filter && ! globalConfig . skipFilter ) {
194- const rawFilter = require ( globalConfig . filter ) ;
195- let filterSetupPromise : Promise < unknown | undefined > | undefined ;
196- if ( rawFilter . setup ) {
197- // Wrap filter setup Promise to avoid "uncaught Promise" error.
198- // If an error is returned, we surface it in the return value.
199- filterSetupPromise = ( async ( ) => {
200- try {
201- await rawFilter . setup ( ) ;
202- } catch ( err ) {
203- return err ;
204- }
205- return undefined ;
206- } ) ( ) ;
207- }
208- filter = async ( testPaths : Array < string > ) => {
209- if ( filterSetupPromise ) {
210- // Expect an undefined return value unless there was an error.
211- const err = await filterSetupPromise ;
212- if ( err ) {
213- throw err ;
214- }
215- }
216- return rawFilter ( testPaths ) ;
217- } ;
218- }
219-
220- performance . mark ( 'jest/buildContextsAndHasteMaps:start' ) ;
221- const { contexts, hasteMapInstances} = await buildContextsAndHasteMaps (
222- configs ,
223- globalConfig ,
224- outputStream ,
225- ) ;
226- performance . mark ( 'jest/buildContextsAndHasteMaps:end' ) ;
227-
228- if ( globalConfig . watch || globalConfig . watchAll ) {
229- await runWatch (
230- contexts ,
231- configs ,
232- warnForDeprecations ,
233- globalConfig ,
234- outputStream ,
235- hasteMapInstances ,
236- filter ,
237- ) ;
238- // If in watch mode, return the promise that will never resolve.
239- // If the watch mode is interrupted, watch should handle the process
240- // shutdown.
241- // eslint-disable-next-line @typescript-eslint/no-empty-function
242- return new Promise ( ( ) => { } ) ;
243- } else {
244- let result : AggregatedResult ;
245- await runWithoutWatch (
246- globalConfig ,
247- contexts ,
248- outputStream ,
249- r => {
250- result = r ;
251- } ,
252- changedFilesPromise ,
253- filter ,
254- ) ;
255- return result ! ;
256- }
257- } ;
258-
259- const runWatch = async (
260- contexts : Array < TestContext > ,
261- _configs : Array < Config . ProjectConfig > ,
262- warnForDeprecations : boolean ,
263- globalConfig : Config . GlobalConfig ,
264- outputStream : NodeJS . WriteStream ,
265- hasteMapInstances : Array < IHasteMap > ,
266- filter ?: Filter ,
267- ) => {
268- if ( warnForDeprecations ) {
269- try {
270- await handleDeprecationWarnings ( outputStream , process . stdin ) ;
271- return await watch (
272- globalConfig ,
273- contexts ,
274- outputStream ,
275- hasteMapInstances ,
276- undefined ,
277- undefined ,
278- filter ,
279- ) ;
280- } catch {
281- exit ( 0 ) ;
282- }
283- }
284-
285- return watch (
286- globalConfig ,
287- contexts ,
288- outputStream ,
289- hasteMapInstances ,
290- undefined ,
291- undefined ,
292- filter ,
293- ) ;
294- } ;
295-
296- const runWithoutWatch = async (
297- globalConfig : Config . GlobalConfig ,
298- contexts : Array < TestContext > ,
299- outputStream : NodeJS . WriteStream ,
300- onComplete : OnCompleteCallback ,
301- changedFilesPromise ?: ChangedFilesPromise ,
302- filter ?: Filter ,
303- ) => {
304- const startRun = async ( ) : Promise < void | null > => {
305- if ( ! globalConfig . listTests ) {
306- preRunMessagePrint ( outputStream ) ;
307- }
308- return runJest ( {
309- changedFilesPromise,
310- contexts,
311- failedTestsCache : undefined ,
312- filter,
313- globalConfig,
314- onComplete,
315- outputStream,
316- startRun,
317- testWatcher : new TestWatcher ( { isWatchMode : false } ) ,
318- } ) ;
319- } ;
320- return startRun ( ) ;
321- } ;
0 commit comments