@@ -5,6 +5,7 @@ import path from 'path';
5
5
import { createDirectory } from 'jest-util' ;
6
6
import rimraf from 'rimraf' ;
7
7
import execa from 'execa' ;
8
+ import chalk from 'chalk' ;
8
9
import { Writable } from 'readable-stream' ;
9
10
10
11
const CLI_PATH = path . resolve ( __dirname , '../packages/cli/build/bin.js' ) ;
@@ -13,12 +14,15 @@ type RunOptions = {
13
14
nodeOptions ?: string ,
14
15
nodePath ?: string ,
15
16
timeout ?: number , // kill the process after X milliseconds
17
+ expectedFailure ?: boolean ,
16
18
} ;
17
19
18
20
export function run (
19
21
dir : string ,
20
22
args ?: Array < string > ,
21
- options : RunOptions = { } ,
23
+ options : RunOptions = {
24
+ expectedFailure : false ,
25
+ } ,
22
26
) {
23
27
return spawnCli ( dir , args , options ) ;
24
28
}
@@ -28,7 +32,9 @@ export async function runUntil(
28
32
dir : string ,
29
33
args : Array < string > | void ,
30
34
text : string ,
31
- options : RunOptions = { } ,
35
+ options : RunOptions = {
36
+ expectedFailure : false ,
37
+ } ,
32
38
) {
33
39
const spawnPromise = spawnCliAsync ( dir , args , { timeout : 30000 , ...options } ) ;
34
40
@@ -109,7 +115,11 @@ export const getTempDirectory = (name: string) =>
109
115
function spawnCli ( dir : string , args ?: Array < string > , options : RunOptions = { } ) {
110
116
const { spawnArgs, spawnOptions} = getCliArguments ( { dir, args, options} ) ;
111
117
112
- return execa . sync ( process . execPath , spawnArgs , spawnOptions ) ;
118
+ const result = execa . sync ( process . execPath , spawnArgs , spawnOptions ) ;
119
+
120
+ handleTestCliFailure ( options , result , dir , args ) ;
121
+
122
+ return result ;
113
123
}
114
124
115
125
function spawnCliAsync (
@@ -119,7 +129,12 @@ function spawnCliAsync(
119
129
) {
120
130
const { spawnArgs, spawnOptions} = getCliArguments ( { dir, args, options} ) ;
121
131
122
- return execa ( process . execPath , spawnArgs , spawnOptions ) ;
132
+ try {
133
+ return execa ( process . execPath , spawnArgs , spawnOptions ) ;
134
+ } catch ( result ) {
135
+ handleTestCliFailure ( options , result , dir , args ) ;
136
+ return result ;
137
+ }
123
138
}
124
139
125
140
function getCliArguments ( { dir, args, options} ) {
@@ -147,3 +162,15 @@ function getCliArguments({dir, args, options}) {
147
162
} ;
148
163
return { spawnArgs, spawnOptions} ;
149
164
}
165
+
166
+ function handleTestCliFailure ( options , result , dir , args ) {
167
+ if ( ! options . expectedFailure && result . code !== 0 ) {
168
+ console . log ( `Running CLI failed for unexpected reason. Here's more info:
169
+
170
+ ${ chalk . bold ( 'dir:' ) } ${ dir }
171
+ ${ chalk . bold ( 'args:' ) } ${ ( args || [ ] ) . join ( ' ' ) }
172
+ ${ chalk . bold ( 'stderr:' ) } ${ result . stderr }
173
+ ${ chalk . bold ( 'stdout:' ) } ${ result . stdout }
174
+ ${ chalk . bold ( 'code:' ) } ${ result . code } `) ;
175
+ }
176
+ }
0 commit comments