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

Đang mở Phù hợp với người mới
#548 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
testing

Hướng nghiên cứu

Kiểm tra nằm trong waitsOnRealTimer tại src/utils/testing/async-test-analyzer/async-test-analyzer-configuration.ts (khoảng dòng 146), khớp với TIMER_CALL (dòng 136) mà không đọc đối số độ trễ. Hãy bắt đầu bằng việc đọc cách matcher đó thu nhận lệnh gọi, sau đó thêm một trường hợp trong đó độ trễ bị thiếu hoặc 0 dạng literal không được tính. Hoàn thành khi bản tái hiện trong src/sum.spec.ts không còn báo vi phạm nữa, trong khi setTimeout(resolve, 1200) và các độ trễ không phải literal vẫn được báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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).
Ngôn ngữ chính
TypeScript
Star
0
Fork
0
Merge trung bình
2 giờ 36 phút
Pull request đã merge (30 ngày)
168

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Rule-Of-Code/RoC

Tất cả issue của Rule-Of-Code/RoC

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.