Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Support Node.js fake timers

Đang mở
#1,393 3 bình luận 2 reaction 0 người được giao Xem trên GitHub

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

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Lĩnh vực
frontend, testing-qa

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện ví dụ về trình chạy kiểm thử Node.js với t.mock.timers.enable() và screen.findByText(). Theo dõi cách findByText chờ các kết quả bất đồng bộ khi các bộ hẹn giờ được mock. Hoàn tất khi lỗi phân giải promise đã được báo cáo được khắc phục hoặc hành vi được hỗ trợ và cách khắc phục được xác định rõ ràng, cùng với một bài kiểm thử hồi quy cho ví dụ này.

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

Mô tả

enhancement
  • @testing-library/react version: 16.3.0
  • Testing Framework and version: node:test, 22.14.0
  • DOM Environment: jsdom 26.0.0
Relevant code or config:
import { screen } from '@testing-library/react';
import { it } from 'node:test';

await it('example', async (t) => {
   t.mock.timers.enable();  // Mocking timers using Node.js test runner
   await screen.findByText('some text');  // Attempting to find text
});
What you did:

In the code above, the timers are mocked using t.mock, which is provided by the Node.js test runner. The findByText query is used to search for specific text in the rendered output.

What happened:

When running this test, the following error message is displayed:

'Promise resolution is still pending but the event loop has already resolved'

This occurs because findByText internally uses setTimeout or setInterval (i.e., timers) to wait for the text to appear, and these timers are being mocked using t.mock.timers.enable(). As a result, the timer-based mechanism in findByText does not work correctly, causing the test to fail prematurely with the error message mentioned above.

Reproduction:
  1. Mock timers using t.mock.timers.enable() in the Node.js test runner.
  2. Attempt to find text asynchronously using screen.findByText().
  3. The error will occur when the test tries to resolve the promise.
Problem description:

The core issue is that findByText relies on timers internally to wait for the element to appear, but when these timers are mocked (using t.mock.timers.enable()), the expected behavior of waiting for the element to render is disrupted. This leads to the error:

'Promise resolution is still pending but the event loop has already resolved'

This behavior makes it difficult to use findByText in tests where timers are mocked or disabled. This can cause confusion and make it harder to test components that rely on async behavior.

Suggested solution:

One possible solution would be to handle cases where timers are mocked explicitly within the findByText query, by either:

  1. Detecting whether timers are mocked and adapting the logic accordingly, or
  2. Providing a way to configure findByText to explicitly use a custom timer (so the user can provide an unmocked version) or use a different timing mechanism (e.g., using async/await without relying on setTimeout).

Alternatively, allowing the user to opt-out of using timers in certain test scenarios (such as when mocking them) would be helpful.

Another workaround could be to manually wait for the element to appear without relying on findByText, using custom logic that doesn’t involve setTimeout or other mocked timers.

Ngôn ngữ chính
JavaScript
Star
19.7k
Fork
1.2k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

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 testing-library/react-testing-library

Tất cả issue của testing-library/react-testing-library

Issue tương tự

Thêm issue về JavaScript

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.