Files
orca/config/ast-grep/rules/flaky-tests/test-no-polling-loop.yml
Neil 6bb2e13c35 refactor(tooling): organize remaining scripts by domain owners (#38407)
* refactor(tooling): group remaining scripts by concrete domain owners

* fix(tooling): preserve relocated launcher resource roots
2026-11-05 00:29:16 -08:00

140 lines
5.3 KiB
YAML

# Ratcheted by config/scripts/static-analysis/ratchets/test-quality/check-flaky-test-pattern-ratchet.mjs; info severity keeps
# `pnpm lint:ast-grep` quiet about the grandfathered baseline.
# The `utils` block matches test-no-real-time-wait.yml; keep them in sync.
# The TSX document below mirrors the TypeScript one; keep them in sync.
id: test-no-polling-loop
language: typescript
severity: info
message: A hand-rolled polling loop guesses at timing. Use expect.poll, vi.waitFor, or a locator assertion, which report what never became true.
note: 'For a deliberate loop, add `// ast-grep-ignore: test-no-polling-loop -- <reason>` above it.'
files:
- '**/*.test.ts'
- '**/*.test.tsx'
- '**/tests/e2e/**'
utils:
resolves-promise-executor:
inside:
stopBy: end
kind: new_expression
all:
- has: { field: constructor, regex: '^Promise$' }
- has:
field: arguments
has:
nthChild: 1
any:
- has: { field: parameter, pattern: $R }
- has:
{
field: parameters,
has: { nthChild: 1, has: { field: pattern, pattern: $R } }
}
nonzero-delay:
has: { field: arguments, has: { nthChild: 2, not: { regex: '^0$' } } }
# Inline checks rather than `constraints`: those run after matching, so a rejected candidate
# would end a `has` search instead of letting it try the next node.
real-time-sleep:
any:
- kind: call_expression
has: { field: function, has: { field: property, regex: '^waitForTimeout$' } }
- kind: await_expression
has:
kind: call_expression
all:
- has: { field: function, regex: '^setTimeout$' }
- has: { field: arguments, has: { nthChild: 1, kind: number } }
- kind: await_expression
has:
kind: call_expression
all:
- has: { field: function, regex: '^(sleep|delay|wait|pause)$' }
- has: { field: arguments, has: { nthChild: 1, not: { regex: '^0$' } } }
# Callback forms first: `any` keeps the first match, and `setTimeout($R, …)` would otherwise
# bind $R to the callback and fail the executor check.
- all:
- pattern: setTimeout(() => $R($$$), $MS)
- matches: nonzero-delay
- matches: resolves-promise-executor
- all:
- pattern: setTimeout(() => { $R($$$) }, $MS)
- matches: nonzero-delay
- matches: resolves-promise-executor
- all:
- pattern: setTimeout($R, $MS)
- matches: nonzero-delay
- matches: resolves-promise-executor
rule:
any: [{ kind: while_statement }, { kind: do_statement }, { kind: for_statement }]
has:
field: body
stopBy: end
matches: real-time-sleep
---
id: test-no-polling-loop-tsx
language: tsx
severity: info
message: A hand-rolled polling loop guesses at timing. Use expect.poll, vi.waitFor, or a locator assertion, which report what never became true.
note: 'For a deliberate loop, add `// ast-grep-ignore: test-no-polling-loop -- <reason>` above it.'
files:
- '**/*.test.ts'
- '**/*.test.tsx'
- '**/tests/e2e/**'
utils:
resolves-promise-executor:
inside:
stopBy: end
kind: new_expression
all:
- has: { field: constructor, regex: '^Promise$' }
- has:
field: arguments
has:
nthChild: 1
any:
- has: { field: parameter, pattern: $R }
- has:
{
field: parameters,
has: { nthChild: 1, has: { field: pattern, pattern: $R } }
}
nonzero-delay:
has: { field: arguments, has: { nthChild: 2, not: { regex: '^0$' } } }
# Inline checks rather than `constraints`: those run after matching, so a rejected candidate
# would end a `has` search instead of letting it try the next node.
real-time-sleep:
any:
- kind: call_expression
has: { field: function, has: { field: property, regex: '^waitForTimeout$' } }
- kind: await_expression
has:
kind: call_expression
all:
- has: { field: function, regex: '^setTimeout$' }
- has: { field: arguments, has: { nthChild: 1, kind: number } }
- kind: await_expression
has:
kind: call_expression
all:
- has: { field: function, regex: '^(sleep|delay|wait|pause)$' }
- has: { field: arguments, has: { nthChild: 1, not: { regex: '^0$' } } }
# Callback forms first: `any` keeps the first match, and `setTimeout($R, …)` would otherwise
# bind $R to the callback and fail the executor check.
- all:
- pattern: setTimeout(() => $R($$$), $MS)
- matches: nonzero-delay
- matches: resolves-promise-executor
- all:
- pattern: setTimeout(() => { $R($$$) }, $MS)
- matches: nonzero-delay
- matches: resolves-promise-executor
- all:
- pattern: setTimeout($R, $MS)
- matches: nonzero-delay
- matches: resolves-promise-executor
rule:
any: [{ kind: while_statement }, { kind: do_statement }, { kind: for_statement }]
has:
field: body
stopBy: end
matches: real-time-sleep