unit-test-quality-standards: a zero-delay setTimeout yield is reported as a real-timer wait

Open Beginner friendly
#548 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
testing

Research direction

The check lives in waitsOnRealTimer in src/utils/testing/async-test-analyzer/async-test-analyzer-configuration.ts (around line 146), which matches TIMER_CALL (line 136) without reading the delay argument. Start by reading how that matcher captures the call, then add a case where a missing or literal 0 delay is not counted. Done when the reproduction in src/sum.spec.ts no longer reports a violation, while setTimeout(resolve, 1200) and non-literal delays still do.

Written by the indexing model from the issue text.

Description

bug

Version: 8.0.1 (tag v8.0.1)
Law: unit-test-quality-standards (Unit Test Quality Standards), "setTimeout usage without fake timers may cause test flakiness"

What happens

AsyncTestAnalyzerConfiguration.waitsOnRealTimer (src/utils/testing/async-test-analyzer/async-test-analyzer-configuration.ts:146) counts every setTimeout( (TIMER_CALL, :136) in a file without useFakeTimers( and without clearTimeout(handle). The delay argument is not read.

Reproduction

it('adds after the pending microtasks and one macrotask have run', async () => {
  await new Promise((resolve) => setTimeout(resolve));
  expect(sum(1, 2)).toBe(3);
});

laws.enabled { "Unit Test Quality Standards": true }, typescript installed:

1. Unit Test Quality Standards [2 violations] (Score: 80/100)
   • setTimeout usage without fake timers may cause test flakiness in src/sum.spec.ts

What I ruled out

The same violation is produced for setTimeout(resolve, 0) and for setTimeout(resolve, 1200): the message is identical, so a reader cannot tell a yield from a sleep.

Why it matters

await new Promise((r) => setTimeout(r)) yields one macrotask; it does not wait on wall-clock time and is not a flakiness risk. In a large codebase such yields are common and they drown the few real waits, which are the ones the law is for.

Suggested direction

  1. Do not count a call with no delay argument or a literal 0.
  2. Keep counting non-zero literal delays and non-literal delays.
  3. Optionally put the delay in the message (setTimeout(…, 1200) without fake timers).
Dominant language
TypeScript
Stars
0
Forks
0
Avg merge
2h 36m
Merged PRs (30d)
168

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rule-Of-Code/RoC

All issues in Rule-Of-Code/RoC

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.