Skip to content

Check_assertion_quality

FieldValue
TypeSkill Resource
Source~/.copilot/skills/quality/scripts/check_assertion_quality.py
DescriptionNot specified

Source Content

#!/usr/bin/env python3
"""Flag tests whose only assertion can never actually fail.
The biggest LLM test-writing failure this skill's other checks miss:
`.skip`/`.only` catch a committed shortcut, but nothing catches a test that
runs and "passes" while asserting nothing meaningful — `expect(true).toBe(true)`
or an empty test body. (An unawaited-async-assertion check was considered
and dropped: a reliable regex for "this expect() call's promise is never
awaited or chained" would need real AST scope tracking, not text matching —
a `ts-morph`/`ast-grep` upgrade, not a grep heuristic. Flagging it here
would just be a false-confidence check.)
Usage: check_assertion_quality.py FILE [FILE ...]
Exit 0 = no flagged pattern in any file. Exit 1 = at least one flagged
pattern found. Exit 2 = usage/file error.
"""
import re
import sys
TAUTOLOGY_RE = re.compile(
r"expect\(\s*(true|1|'a'|\"a\")\s*\)\.(toBe|toEqual)\(\s*(true|1|'a'|\"a\")\s*\)"
)
EMPTY_BODY_RE = re.compile(
r"\b(it|test)\(\s*['\"][^'\"]*['\"]\s*,\s*(?:async\s*)?\(\s*\)\s*=>\s*\{\s*\}\s*\)"
)
def check_file(path: str) -> list[str]:
text = open(path, encoding="utf-8").read()
findings = []
for m in TAUTOLOGY_RE.finditer(text):
line = text[: m.start()].count("\n") + 1
findings.append(f"{path}:{line}: tautological assertion (asserts a literal against itself, can never fail)")
for m in EMPTY_BODY_RE.finditer(text):
line = text[: m.start()].count("\n") + 1
findings.append(f"{path}:{line}: empty test body — asserts nothing")
return findings
def main() -> int:
if len(sys.argv) < 2:
print("Usage: check_assertion_quality.py FILE [FILE ...]", file=sys.stderr)
return 2
fail = 0
for path in sys.argv[1:]:
try:
findings = check_file(path)
except OSError as e:
print(f"ERROR: cannot read {path}: {e}", file=sys.stderr)
return 2
if findings:
fail = 1
for f in findings:
print(f"❌ {f}")
else:
print(f"✅ {path}: no tautological or empty-body assertions found")
return fail
if __name__ == "__main__":
raise SystemExit(main())