@@ -18,6 +18,7 @@ import {makeGlobalConfig, makeProjectConfig} from '@jest/test-utils';
18
18
import * as transform from '@jest/transform' ;
19
19
import { createTestScheduler } from '../TestScheduler' ;
20
20
import * as testSchedulerHelper from '../testSchedulerHelper' ;
21
+ import * as runGlobalHook from '../runGlobalHook' ;
21
22
22
23
jest
23
24
. mock ( 'ci-info' , ( ) => ( { GITHUB_ACTIONS : true } ) )
35
36
__esModule : true ,
36
37
...jest . requireActual ( '@jest/transform' ) ,
37
38
} ;
38
- } ) ;
39
+ } )
40
+ . mock ( 'exit-x' , ( ) => ( { __esModule : true , default : jest . fn ( ) } ) ) ;
39
41
const mockSerialRunner = {
40
42
isSerial : true ,
41
43
runTests : jest . fn ( ) ,
@@ -53,10 +55,13 @@ jest.mock('jest-runner-parallel', () => jest.fn(() => mockParallelRunner), {
53
55
54
56
const spyShouldRunInBand = jest . spyOn ( testSchedulerHelper , 'shouldRunInBand' ) ;
55
57
58
+ const spyRunGlobalHook = jest . spyOn ( runGlobalHook , 'default' ) ;
59
+
56
60
beforeEach ( ( ) => {
57
61
mockSerialRunner . runTests . mockClear ( ) ;
58
62
mockParallelRunner . runTests . mockClear ( ) ;
59
63
spyShouldRunInBand . mockClear ( ) ;
64
+ spyRunGlobalHook . mockClear ( ) ;
60
65
} ) ;
61
66
62
67
describe ( 'reporters' , ( ) => {
@@ -449,6 +454,42 @@ test('should bail after `n` failures', async () => {
449
454
expect ( setState ) . toHaveBeenCalledWith ( { interrupted : true } ) ;
450
455
} ) ;
451
456
457
+ test ( 'should bail after `n` failures and perform global teardown' , async ( ) => {
458
+ const scheduler = await createTestScheduler (
459
+ makeGlobalConfig ( { bail : 3 } ) ,
460
+ { } ,
461
+ { } ,
462
+ ) ;
463
+ const test = {
464
+ context : {
465
+ config : makeProjectConfig ( {
466
+ moduleFileExtensions : [ '.js' ] ,
467
+ rootDir : './' ,
468
+ runner : 'jest-runner-serial' ,
469
+ transform : [ ] ,
470
+ } ) ,
471
+ hasteFS : {
472
+ matchFiles : jest . fn ( ( ) => [ ] ) ,
473
+ } ,
474
+ } ,
475
+ path : './test/path.js' ,
476
+ } ;
477
+
478
+ const tests = [ test ] ;
479
+ const setState = jest . fn ( ) ;
480
+ await scheduler . scheduleTests ( tests , {
481
+ isInterrupted : jest . fn ( ) ,
482
+ isWatchMode : ( ) => false ,
483
+ setState,
484
+ } ) ;
485
+ await mockSerialRunner . runTests . mock . calls [ 0 ] [ 3 ] ( test , {
486
+ numFailingTests : 3 ,
487
+ snapshot : { } ,
488
+ testResults : [ { } ] ,
489
+ } ) ;
490
+ expect ( spyRunGlobalHook . mock . calls [ 0 ] [ 0 ] . moduleName ) . toBe ( 'globalTeardown' ) ;
491
+ } ) ;
492
+
452
493
test ( 'should not bail if less than `n` failures' , async ( ) => {
453
494
const scheduler = await createTestScheduler (
454
495
makeGlobalConfig ( { bail : 2 } ) ,
0 commit comments