redline
MediumTests and CIsleep-in-test

Fixed sleep in a test

Fixed sleep in a test. await sleep(5 waits a fixed time. Too short on a slow CI box, wasted time everywhere else.

This is how the comment reads inline, under the flagged line.

Why it matters

Sleeps make tests slow and flaky at once: they guess how long something takes instead of waiting for it.

What to do

Wait for the condition (waitFor, an event, a polled assertion) or use fake timers.

Examples

Flagged
await sleep(500);
expect(list).toHaveLength(3);
Better
await waitFor(() => expect(list).toHaveLength(3));

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript, Python. Scope: Test files only. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

await sleep(500);
tests/test_a.py
import time
time.sleep(1)

Left alone

await waitFor(() => done);

Research

Fixed sleeps and `setTimeout` waits, in tests and in shipped code. It is slow when the state is ready and flaky when it is not.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "sleep-in-test"
    ],
    "severity": {
      "sleep-in-test": "low"
    }
  }
}

Read the source or report a false positive.