| 102 | }; |
| 103 | |
| 104 | const readResultsAndExit = ( |
| 105 | result: AggregatedResult | null, |
| 106 | globalConfig: Config.GlobalConfig, |
| 107 | ) => { |
| 108 | const code = !result || result.success ? 0 : globalConfig.testFailureExitCode; |
| 109 | |
| 110 | // Only exit if needed |
| 111 | process.on('exit', () => { |
| 112 | if (typeof code === 'number' && code !== 0) { |
| 113 | process.exitCode = code; |
| 114 | } |
| 115 | }); |
| 116 | |
| 117 | if (globalConfig.forceExit) { |
| 118 | if (!globalConfig.detectOpenHandles) { |
| 119 | console.warn( |
| 120 | `${chalk.bold( |
| 121 | 'Force exiting Jest: ', |
| 122 | )}Have you considered using \`--detectOpenHandles\` to detect ` + |
| 123 | 'async operations that kept running after all tests finished?', |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | exit(code); |
| 128 | } else if ( |
| 129 | !globalConfig.detectOpenHandles && |
| 130 | globalConfig.openHandlesTimeout !== 0 |
| 131 | ) { |
| 132 | const timeout = globalConfig.openHandlesTimeout; |
| 133 | setTimeout(() => { |
| 134 | console.warn( |
| 135 | chalk.yellow.bold( |
| 136 | `Jest did not exit ${ |
| 137 | timeout === 1000 ? 'one second' : `${timeout / 1000} seconds` |
| 138 | } after the test run has completed.\n\n'`, |
| 139 | ) + |
| 140 | chalk.yellow( |
| 141 | 'This usually means that there are asynchronous operations that ' + |
| 142 | "weren't stopped in your tests. Consider running Jest with " + |
| 143 | '`--detectOpenHandles` to troubleshoot this issue.', |
| 144 | ), |
| 145 | ); |
| 146 | }, timeout).unref(); |
| 147 | } |
| 148 | }; |