Support Node.js fake timers
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- javascript, node.js, react
- Ambito
- frontend, testing-qa
Direzione di ricerca
Inizia riproducendo l’esempio del test runner di Node.js con t.mock.timers.enable() e screen.findByText(). Traccia il modo in cui findByText attende i risultati asincroni quando i timer sono sottoposti a mock. Il lavoro è completato quando il problema segnalato nella risoluzione della promise è stato risolto oppure il comportamento supportato e la soluzione alternativa sono chiaramente definiti, con un test di regressione per l’esempio.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
@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.
- Lingua principale
- JavaScript
- Stelle
- 19.7k
- Fork
- 1.2k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di testing-library/react-testing-library
-
fireEvent.select does not wrap its automatic native focus in actForse già presa @sergioperezcheco l’ha presa 2 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict modeForse di nuovo libera @suhailopensource l’ha presa 72 giorni fa e non c’è nessuna pull request aperta. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 35/100
testing-library/react-testing-library#1466 · 1 commento ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 30/100
testing-library/react-testing-library#1459 · 2 commenti ·
-
perf: optimize container lookup with early exitForse di nuovo libera @Ch-Abhinav-Chowdary l’ha presa 298 giorni fa e non c’è nessuna pull request aperta. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 35/100
testing-library/react-testing-library#1430 · 1 commento ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)Forse già presa @swarnim02 l’ha presa 315 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
Tutte le issue di testing-library/react-testing-library
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
NaturalIntelligence/fast-xml-parser#888 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
bug callouts regression revealjs
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
quarto-dev/quarto-cli#15014 ·
I maintainer di solito rispondono entro 1 giorno
-
Remove: Fox Deportes SDApertacheck:passed feeds:remove
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 65/100
iptv-org/database#37176 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 9 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
hawk-digital-environments/HAWKI#443 ·
I maintainer di solito rispondono entro 1 giorno
-
documentation v2
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
modelcontextprotocol/python-sdk#3662 ·
I maintainer di solito rispondono entro 1 giorno