> ## Documentation Index
> Fetch the complete documentation index at: https://loadwise.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# test-cleanup-after-await

> A test waits before cleaning up a local resource, so a rejection can leave it running.

## What it flags

An async test acquires a local child process, HTTP listener, worker, file watcher, or interval and cleans it up after an `await`. A rejected promise can skip cleanup and leave the resource running, which can hang the test runner.

The rule runs in recognized test and end-to-end files. It accounts for cleanup in a `finally` block that protects the wait and cleanup hooks registered before the wait, such as `t.after`, `onTestFinished` (from the test context or imported) or `onTestFailed` from the test context. It also stays quiet when, before the wait, a closure that performs the cleanup is passed to a call (`t.teardown`, `stack.defer`, `cleanups.push`) or held by a `using` declaration. Event listeners, timers and `afterAll` called inside a test do not count as teardown. Neither does an in-test `afterEach` that is imported from `node:test`, `@jest/globals` or `bun:test`, or called globally or on a runner such as `test` in a file that uses `jest`; other in-test `afterEach` calls, such as Mocha globals, are accepted. Cleanup that a handled rejection still reaches, after a `catch` block that neither throws nor returns or after `.catch()` on the awaited promise, is left alone. It leaves the resource alone when ownership passes to another function.

## How to review or fix

Register a cleanup hook immediately after creating the resource, or wrap the work in `try` and put cleanup in `finally`. For example, in a Node.js test:

```ts theme={null}
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import test from 'node:test';

test('starts the worker', async (t) => {
  const child = spawn('node', ['worker.js']);
  t.after(() => {
    child.kill();
  });
  await once(child.stdout, 'data');
});
```

## Configuration

Set the rule to `"off"` in `loadwise.config.json` to drop its findings:

```json theme={null}
{
  "rules": { "test-cleanup-after-await": "off" }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.