@@ -157,89 +157,92 @@ function checkIsError(error: any): error is Error {
157
157
return ! ! ( error && ( error as Error ) . message && ( error as Error ) . stack ) ;
158
158
}
159
159
160
- export const callAsyncCircusFn = async (
160
+ export const callAsyncCircusFn = (
161
161
fn : AsyncFn ,
162
162
testContext : TestContext | undefined ,
163
163
{ isHook, timeout} : { isHook ?: boolean | null ; timeout : number } ,
164
164
) : Promise < any > => {
165
- let timeoutID : NodeJS . Timeout | undefined ;
165
+ let timeoutID : NodeJS . Timeout ;
166
166
let completed = false ;
167
167
168
- try {
169
- return await new Promise ( ( resolve , reject ) => {
170
- timeoutID = setTimeout (
171
- ( ) => reject ( _makeTimeoutMessage ( timeout , ! ! isHook ) ) ,
172
- timeout ,
173
- ) ;
174
-
175
- // If this fn accepts `done` callback we return a promise that fulfills as
176
- // soon as `done` called.
177
- if ( fn . length ) {
178
- const done = ( reason ?: Error | string ) : void => {
179
- const errorAsErrorObject = checkIsError ( reason )
180
- ? reason
181
- : new Error ( `Failed: ${ prettyFormat ( reason , { maxDepth : 3 } ) } ` ) ;
182
-
183
- // Consider always throwing, regardless if `reason` is set or not
184
- if ( completed && reason ) {
185
- errorAsErrorObject . message =
186
- 'Caught error after test environment was torn down\n\n' +
187
- errorAsErrorObject . message ;
188
-
189
- throw errorAsErrorObject ;
190
- }
168
+ return new Promise ( ( resolve , reject ) => {
169
+ timeoutID = setTimeout (
170
+ ( ) => reject ( _makeTimeoutMessage ( timeout , ! ! isHook ) ) ,
171
+ timeout ,
172
+ ) ;
173
+
174
+ // If this fn accepts `done` callback we return a promise that fulfills as
175
+ // soon as `done` called.
176
+ if ( fn . length ) {
177
+ const done = ( reason ?: Error | string ) : void => {
178
+ const errorAsErrorObject = checkIsError ( reason )
179
+ ? reason
180
+ : new Error ( `Failed: ${ prettyFormat ( reason , { maxDepth : 3 } ) } ` ) ;
181
+
182
+ // Consider always throwing, regardless if `reason` is set or not
183
+ if ( completed && reason ) {
184
+ errorAsErrorObject . message =
185
+ 'Caught error after test environment was torn down\n\n' +
186
+ errorAsErrorObject . message ;
187
+
188
+ throw errorAsErrorObject ;
189
+ }
191
190
192
- return reason ? reject ( errorAsErrorObject ) : resolve ( ) ;
193
- } ;
191
+ return reason ? reject ( errorAsErrorObject ) : resolve ( ) ;
192
+ } ;
194
193
195
- return fn . call ( testContext , done ) ;
196
- }
194
+ return fn . call ( testContext , done ) ;
195
+ }
197
196
198
- let returnedValue ;
199
- if ( isGeneratorFn ( fn ) ) {
200
- returnedValue = co . wrap ( fn ) . call ( { } ) ;
201
- } else {
202
- try {
203
- returnedValue = fn . call ( testContext ) ;
204
- } catch ( error ) {
205
- return reject ( error ) ;
206
- }
197
+ let returnedValue ;
198
+ if ( isGeneratorFn ( fn ) ) {
199
+ returnedValue = co . wrap ( fn ) . call ( { } ) ;
200
+ } else {
201
+ try {
202
+ returnedValue = fn . call ( testContext ) ;
203
+ } catch ( error ) {
204
+ return reject ( error ) ;
207
205
}
206
+ }
208
207
209
- // If it's a Promise, return it. Test for an object with a `then` function
210
- // to support custom Promise implementations.
211
- if (
212
- typeof returnedValue === 'object' &&
213
- returnedValue !== null &&
214
- typeof returnedValue . then === 'function'
215
- ) {
216
- return returnedValue . then ( resolve , reject ) ;
217
- }
208
+ // If it's a Promise, return it. Test for an object with a `then` function
209
+ // to support custom Promise implementations.
210
+ if (
211
+ typeof returnedValue === 'object' &&
212
+ returnedValue !== null &&
213
+ typeof returnedValue . then === 'function'
214
+ ) {
215
+ return returnedValue . then ( resolve , reject ) ;
216
+ }
218
217
219
- if ( ! isHook && returnedValue !== void 0 ) {
220
- return reject (
221
- new Error (
222
- `
218
+ if ( ! isHook && returnedValue !== void 0 ) {
219
+ return reject (
220
+ new Error (
221
+ `
223
222
test functions can only return Promise or undefined.
224
223
Returned value: ${ String ( returnedValue ) }
225
224
` ,
226
- ) ,
227
- ) ;
228
- }
225
+ ) ,
226
+ ) ;
227
+ }
229
228
230
- // Otherwise this test is synchronous, and if it didn't throw it means
231
- // it passed.
232
- return resolve ( ) ;
233
- } ) ;
234
- } finally {
235
- completed = true ;
236
- // If timeout is not cleared/unrefed the node process won't exit until
237
- // it's resolved.
238
- if ( timeoutID ) {
229
+ // Otherwise this test is synchronous, and if it didn't throw it means
230
+ // it passed.
231
+ return resolve ( ) ;
232
+ } )
233
+ . then ( ( ) => {
234
+ completed = true ;
235
+ // If timeout is not cleared/unrefed the node process won't exit until
236
+ // it's resolved.
239
237
timeoutID . unref && timeoutID . unref ( ) ;
240
238
clearTimeout ( timeoutID ) ;
241
- }
242
- }
239
+ } )
240
+ . catch ( error => {
241
+ completed = true ;
242
+ timeoutID . unref && timeoutID . unref ( ) ;
243
+ clearTimeout ( timeoutID ) ;
244
+ throw error ;
245
+ } ) ;
243
246
} ;
244
247
245
248
export const getTestDuration = ( test : TestEntry ) : number | null => {
0 commit comments