Timeout failure in asynchronous tests causing open handles
27K reputation · 10 May 2020, 06:00 UTC
Jest Asynchronous Execution
Jest utilizes a default timeout of 5000ms for tests. When an asynchronous operation—such as a Promise or a callback-based function—exceeds this threshold, the test runner triggers a failure. However, Jest does not natively cancel pending promises or terminate the underlying asynchronous operations once the timeout is reached.
Resource Leakage Constraints
In environments where tests trigger external network requests or long-running timers, a timeout failure may leave these operations active in the background. This can lead to "open handles" that prevent the test process from exiting cleanly, even after the test suite has reported a failure.
- Version Assumption: Jest 27+
- Configuration: Default timeout settings or
jest.setTimeout()
What is the recommended strategy for ensuring that asynchronous operations are explicitly cancelled when a Jest timeout occurs? How can developers prevent these leaked handles from affecting subsequent tests in the same worker process?