@@ -12,34 +12,23 @@ import type {Argv} from 'types/Argv';
12
12
import type { GlobalConfig , Path } from 'types/Config' ;
13
13
14
14
import path from 'path' ;
15
- import { Console , clearLine , createDirectory , preRunMessage } from 'jest-util' ;
15
+ import { clearLine } from 'jest-util' ;
16
16
import { validateCLIOptions } from 'jest-validate' ;
17
- import { readConfigs , deprecationEntries } from 'jest-config' ;
17
+ import { deprecationEntries } from 'jest-config' ;
18
+ import { runCLI } from '@jest/core' ;
18
19
import * as args from './args' ;
19
20
import chalk from 'chalk' ;
20
- import createContext from '../lib/create_context' ;
21
21
import exit from 'exit' ;
22
- import getChangedFilesPromise from '../getChangedFilesPromise' ;
23
- import { formatHandleErrors } from '../collectHandles' ;
24
- import handleDeprecationWarnings from '../lib/handle_deprecation_warnings' ;
25
- import runJest from '../runJest' ;
26
- import Runtime from 'jest-runtime' ;
27
- import TestWatcher from '../TestWatcher' ;
28
- import watch from '../watch' ;
29
- import pluralize from '../pluralize' ;
30
22
import yargs from 'yargs' ;
31
- import rimraf from 'rimraf' ;
32
23
import { sync as realpath } from 'realpath-native' ;
33
- import init from '../lib/init' ;
34
- import logDebugMessages from '../lib/log_debug_messages' ;
35
- import getVersion from '../version' ;
24
+ import init from '../init' ;
36
25
37
- const { print : preRunMessagePrint } = preRunMessage ;
26
+ import { version as VERSION } from '../../package.json' ;
38
27
39
28
export async function run ( maybeArgv ?: Argv , project ?: Path ) {
40
29
try {
41
30
// $FlowFixMe:`allow reduced return
42
- const argv : Argv = buildArgv ( maybeArgv , project ) ;
31
+ const argv : Argv = buildArgv ( maybeArgv ) ;
43
32
44
33
if ( argv . init ) {
45
34
await init ( ) ;
@@ -59,129 +48,9 @@ export async function run(maybeArgv?: Argv, project?: Path) {
59
48
}
60
49
}
61
50
62
- export const runCLI = async (
63
- argv : Argv ,
64
- projects : Array < Path > ,
65
- ) : Promise < { results : AggregatedResult , globalConfig : GlobalConfig } > => {
66
- const realFs = require ( 'fs' ) ;
67
- const fs = require ( 'graceful-fs' ) ;
68
- fs . gracefulify ( realFs ) ;
69
-
70
- let results ;
71
-
72
- // If we output a JSON object, we can't write anything to stdout, since
73
- // it'll break the JSON structure and it won't be valid.
74
- const outputStream =
75
- argv . json || argv . useStderr ? process . stderr : process . stdout ;
76
-
77
- const { globalConfig, configs, hasDeprecationWarnings} = readConfigs (
78
- argv ,
79
- projects ,
80
- ) ;
81
-
82
- if ( argv . debug ) {
83
- logDebugMessages ( globalConfig , configs , outputStream ) ;
84
- }
85
-
86
- if ( argv . showConfig ) {
87
- logDebugMessages ( globalConfig , configs , process . stdout ) ;
88
- exit ( 0 ) ;
89
- }
90
-
91
- if ( argv . clearCache ) {
92
- configs . forEach ( config => {
93
- rimraf . sync ( config . cacheDirectory ) ;
94
- process . stdout . write ( `Cleared ${ config . cacheDirectory } \n` ) ;
95
- } ) ;
96
-
97
- exit ( 0 ) ;
98
- }
99
-
100
- await _run (
101
- globalConfig ,
102
- configs ,
103
- hasDeprecationWarnings ,
104
- outputStream ,
105
- ( r : AggregatedResult ) => ( results = r ) ,
106
- ) ;
107
-
108
- if ( argv . watch || argv . watchAll ) {
109
- // If in watch mode, return the promise that will never resolve.
110
- // If the watch mode is interrupted, watch should handle the process
111
- // shutdown.
112
- return new Promise ( ( ) => { } ) ;
113
- }
114
-
115
- if ( ! results ) {
116
- throw new Error (
117
- 'AggregatedResult must be present after test run is complete' ,
118
- ) ;
119
- }
120
-
121
- const { openHandles } = results ;
122
-
123
- if ( openHandles && openHandles . length ) {
124
- const formatted = formatHandleErrors ( openHandles , configs [ 0 ] ) ;
125
-
126
- const openHandlesString = pluralize ( 'open handle' , formatted . length , 's' ) ;
127
-
128
- const message =
129
- chalk . red (
130
- `\nJest has detected the following ${ openHandlesString } potentially keeping Jest from exiting:\n\n` ,
131
- ) + formatted . join ( '\n\n' ) ;
132
-
133
- console . error ( message ) ;
134
- }
135
-
136
- return Promise . resolve ( { globalConfig , results} ) ;
137
- } ;
138
-
139
- const readResultsAndExit = (
140
- result : ?AggregatedResult ,
141
- globalConfig : GlobalConfig ,
142
- ) => {
143
- const code = ! result || result . success ? 0 : globalConfig . testFailureExitCode ;
144
-
145
- // Only exit if needed
146
- process . on ( 'exit' , ( ) => {
147
- if ( typeof code === 'number' && code !== 0 ) {
148
- process . exitCode = code ;
149
- }
150
- } ) ;
151
-
152
- if ( globalConfig . forceExit ) {
153
- if ( ! globalConfig . detectOpenHandles ) {
154
- console . error (
155
- chalk . red . bold ( 'Force exiting Jest\n\n' ) +
156
- chalk . red (
157
- 'Have you considered using `--detectOpenHandles` to detect ' +
158
- 'async operations that kept running after all tests finished?' ,
159
- ) ,
160
- ) ;
161
- }
162
-
163
- exit ( code ) ;
164
- } else if ( ! globalConfig . detectOpenHandles ) {
165
- setTimeout ( ( ) => {
166
- console . error (
167
- chalk . red . bold (
168
- 'Jest did not exit one second after the test run has completed.\n\n' ,
169
- ) +
170
- chalk . red (
171
- 'This usually means that there are asynchronous operations that ' +
172
- "weren't stopped in your tests. Consider running Jest with " +
173
- '`--detectOpenHandles` to troubleshoot this issue.' ,
174
- ) ,
175
- ) ;
176
- // $FlowFixMe: `unref` exists in Node
177
- } , 1000 ) . unref ( ) ;
178
- }
179
- } ;
180
-
181
- export const buildArgv = ( maybeArgv : ?Argv , project : ?Path ) = > {
51
+ export const buildArgv = ( maybeArgv : ?Argv ) => {
182
52
const version =
183
- getVersion ( ) +
184
- ( __dirname . includes ( `packages${ path . sep } jest-cli` ) ? '-dev' : '' ) ;
53
+ VERSION + ( __dirname . includes ( `packages${ path . sep } jest-cli` ) ? '-dev' : '' ) ;
185
54
186
55
const rawArgv : Argv | string [ ] = maybeArgv || process . argv . slice ( 2 ) ;
187
56
const argv : Argv = yargs ( rawArgv )
@@ -234,108 +103,44 @@ const getProjectListFromCLIArgs = (argv, project: ?Path) => {
234
103
return projects ;
235
104
} ;
236
105
237
- const buildContextsAndHasteMaps = async (
238
- configs ,
239
- globalConfig ,
240
- outputStream ,
241
- ) = > {
242
- const hasteMapInstances = Array ( configs . length ) ;
243
- const contexts = await Promise . all (
244
- configs . map ( async ( config , index ) => {
245
- createDirectory ( config . cacheDirectory ) ;
246
- const hasteMapInstance = Runtime . createHasteMap ( config , {
247
- console : new Console ( outputStream , outputStream ) ,
248
- maxWorkers : globalConfig . maxWorkers ,
249
- resetCache : ! config . cache ,
250
- watch : globalConfig . watch || globalConfig . watchAll ,
251
- watchman : globalConfig . watchman ,
252
- } ) ;
253
- hasteMapInstances [ index ] = hasteMapInstance ;
254
- return createContext ( config , await hasteMapInstance . build ( ) ) ;
255
- } ) ,
256
- ) ;
257
-
258
- return { contexts , hasteMapInstances} ;
259
- } ;
260
-
261
- const _run = async (
262
- globalConfig ,
263
- configs ,
264
- hasDeprecationWarnings ,
265
- outputStream ,
266
- onComplete ,
106
+ const readResultsAndExit = (
107
+ result : ?AggregatedResult ,
108
+ globalConfig : GlobalConfig ,
267
109
) => {
268
- // Queries to hg/git can take a while, so we need to start the process
269
- // as soon as possible, so by the time we need the result it's already there.
270
- const changedFilesPromise = getChangedFilesPromise ( globalConfig , configs ) ;
110
+ const code = ! result || result . success ? 0 : globalConfig . testFailureExitCode ;
271
111
272
- const { contexts, hasteMapInstances} = await buildContextsAndHasteMaps (
273
- configs ,
274
- globalConfig ,
275
- outputStream ,
276
- ) ;
112
+ // Only exit if needed
113
+ process . on ( 'exit' , ( ) => {
114
+ if ( typeof code === 'number' && code !== 0 ) {
115
+ process . exitCode = code ;
116
+ }
117
+ } ) ;
277
118
278
- globalConfig . watch || globalConfig . watchAll
279
- ? await runWatch (
280
- contexts ,
281
- configs ,
282
- hasDeprecationWarnings ,
283
- globalConfig ,
284
- outputStream ,
285
- hasteMapInstances ,
286
- changedFilesPromise ,
287
- )
288
- : await runWithoutWatch (
289
- globalConfig ,
290
- contexts ,
291
- outputStream ,
292
- onComplete ,
293
- changedFilesPromise ,
119
+ if ( globalConfig . forceExit ) {
120
+ if ( ! globalConfig . detectOpenHandles ) {
121
+ console . error (
122
+ chalk . red . bold ( 'Force exiting Jest\n\n' ) +
123
+ chalk . red (
124
+ 'Have you considered using `--detectOpenHandles` to detect ' +
125
+ 'async operations that kept running after all tests finished?' ,
126
+ ) ,
294
127
) ;
295
- } ;
296
-
297
- const runWatch = async (
298
- contexts ,
299
- configs ,
300
- hasDeprecationWarnings ,
301
- globalConfig ,
302
- outputStream ,
303
- hasteMapInstances ,
304
- changedFilesPromise ,
305
- ) = > {
306
- if ( hasDeprecationWarnings ) {
307
- try {
308
- await handleDeprecationWarnings ( outputStream , process . stdin ) ;
309
- return watch ( globalConfig , contexts , outputStream , hasteMapInstances ) ;
310
- } catch ( e ) {
311
- exit ( 0 ) ;
312
128
}
313
- }
314
-
315
- return watch ( globalConfig , contexts, outputStream, hasteMapInstances) ;
316
- } ;
317
129
318
- const runWithoutWatch = async (
319
- globalConfig ,
320
- contexts ,
321
- outputStream ,
322
- onComplete ,
323
- changedFilesPromise ,
324
- ) = > {
325
- const startRun = async ( ) => {
326
- if ( ! globalConfig . listTests ) {
327
- preRunMessagePrint ( outputStream ) ;
328
- }
329
- return await runJest ( {
330
- changedFilesPromise,
331
- contexts,
332
- failedTestsCache : null ,
333
- globalConfig,
334
- onComplete,
335
- outputStream,
336
- startRun,
337
- testWatcher : new TestWatcher ( { isWatchMode : false } ) ,
338
- } ) ;
339
- } ;
340
- return await startRun ( ) ;
130
+ exit ( code ) ;
131
+ } else if ( ! globalConfig . detectOpenHandles ) {
132
+ setTimeout ( ( ) => {
133
+ console . error (
134
+ chalk . red . bold (
135
+ 'Jest did not exit one second after the test run has completed.\n\n' ,
136
+ ) +
137
+ chalk . red (
138
+ 'This usually means that there are asynchronous operations that ' +
139
+ "weren't stopped in your tests. Consider running Jest with " +
140
+ '`--detectOpenHandles` to troubleshoot this issue.' ,
141
+ ) ,
142
+ ) ;
143
+ // $FlowFixMe: `unref` exists in Node
144
+ } , 1000 ) . unref ( ) ;
145
+ }
341
146
} ;
0 commit comments