close

detectAsyncLeaks

  • Type: boolean
  • Default: false
  • CLI: --detectAsyncLeaks

Detect async resources that are still active after a test file finishes, such as timers that were not cleaned up.

This option uses Node.js async_hooks, so it may slow down tests. It is intended for debugging leaks and is not recommended for regular test runs.

CLI
rstest.config.ts
npx rstest --detectAsyncLeaks

Example

The following test leaks an interval:

import { expect, it } from '@rstest/core';

it('leaks a timer', () => {
  setInterval(() => {}, 1000);
  expect(1).toBe(1);
});

When detectAsyncLeaks is enabled, Rstest fails the test file and reports the resource type and creation stack:

AsyncLeakError: Detected async leak: Timeout was still active after leaks a timer finished.

Clean up async resources before the test finishes to avoid the error:

import { expect, it } from '@rstest/core';

it('cleans up a timer', () => {
  const timer = setInterval(() => {}, 1000);
  clearInterval(timer);
  expect(1).toBe(1);
});