@@ -12,7 +12,6 @@ import type {EnvironmentClass} from 'types/Environment';
1212import type { GlobalConfig , Path , ProjectConfig } from 'types/Config' ;
1313import type { Resolver } from 'types/Resolve' ;
1414import type { TestFramework } from 'types/TestRunner' ;
15- import type { TestResult } from 'types/TestResult' ;
1615import type RuntimeClass from 'jest-runtime' ;
1716
1817import fs from 'fs' ;
@@ -100,8 +99,15 @@ export default function runTest(
10099 mapCoverage : globalConfig . mapCoverage ,
101100 } ) ;
102101 const start = Date . now ( ) ;
103- return testFramework ( globalConfig , config , environment , runtime , path )
104- . then ( ( result : TestResult ) => {
102+ return environment . setup ( ) . then ( async ( ) => {
103+ try {
104+ const result = await testFramework (
105+ globalConfig ,
106+ config ,
107+ environment ,
108+ runtime ,
109+ path ,
110+ ) ;
105111 const testCount =
106112 result . numPassingTests +
107113 result . numFailingTests +
@@ -113,26 +119,24 @@ export default function runTest(
113119 result . console = testConsole . getBuffer ( ) ;
114120 result . skipped = testCount === result . numPendingTests ;
115121 result . displayName = config . displayName ;
116- return result ;
117- } )
118- . then (
119- result =>
120- Promise . resolve ( ) . then ( ( ) => {
121- environment . dispose ( ) ;
122- if ( globalConfig . logHeapUsage ) {
123- if ( global . gc ) {
124- global . gc ( ) ;
125- }
126- result . memoryUsage = process . memoryUsage ( ) . heapUsed ;
127- }
128-
129- // Delay the resolution to allow log messages to be output.
130- return new Promise ( resolve => setImmediate ( ( ) => resolve ( result ) ) ) ;
131- } ) ,
132- err =>
133- Promise . resolve ( ) . then ( ( ) => {
134- environment . dispose ( ) ;
135- throw err ;
136- } ) ,
137- ) ;
122+ if ( environment . teardown ) {
123+ await environment . teardown ( ) ;
124+ }
125+ environment . dispose ( ) ;
126+ if ( globalConfig . logHeapUsage ) {
127+ if ( global . gc ) {
128+ global . gc ( ) ;
129+ }
130+ result . memoryUsage = process . memoryUsage ( ) . heapUsed ;
131+ }
132+ // Delay the resolution to allow log messages to be output.
133+ return new Promise ( resolve => setImmediate ( ( ) => resolve ( result ) ) ) ;
134+ } catch ( err ) {
135+ if ( environment . teardown ) {
136+ await environment . teardown ( ) ;
137+ }
138+ environment . dispose ( ) ;
139+ throw err ;
140+ }
141+ } ) ;
138142}
0 commit comments