Support Node.js fake timers
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ệ
- Công nghệ
- javascript, node.js, react
- 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ả
@testing-library/reactversion: 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:
- Mock timers using
t.mock.timers.enable()in the Node.js test runner. - Attempt to find text asynchronously using
screen.findByText(). - 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:
- Detecting whether timers are mocked and adapting the logic accordingly, or
- Providing a way to configure
findByTextto explicitly use a custom timer (so the user can provide an unmocked version) or use a different timing mechanism (e.g., usingasync/awaitwithout relying onsetTimeout).
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
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của testing-library/react-testing-library
-
fireEvent.select does not wrap its automatic native focus in actCó thể đã có người làm @sergioperezcheco đã nhận 2 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict modeCó thể làm lại được @suhailopensource đã nhận 72 ngày trước và không có pull request nào đang mở. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 35/100
testing-library/react-testing-library#1466 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 30/100
testing-library/react-testing-library#1459 · 2 bình luận ·
-
perf: optimize container lookup with early exitCó thể làm lại được @Ch-Abhinav-Chowdary đã nhận 298 ngày trước và không có pull request nào đang mở. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 35/100
testing-library/react-testing-library#1430 · 1 bình luận ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)Có thể đã có người làm @swarnim02 đã nhận 315 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
Tất cả issue của testing-library/react-testing-library
Issue tương tự
-
Remove: Fox Deportes SDĐang mởcheck:passed feeds:remove
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 65/100
iptv-org/database#37176 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 9 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
hawk-digital-environments/HAWKI#443 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
modelcontextprotocol/python-sdk#3662 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
feedback simulation workshop
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
githubnext/gh-aw-workshop#4370 ·
Maintainer thường phản hồi trong vòng 1 ngày