Calling render on a component that throws, results in 4 renders
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 25/100
- Issue-Typ
- Bug
- Klarheit
- Muss geklärt werden
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- javascript, react, typescript
Rechercherichtung
Beginne mit der verlinkten Reproduktion in error-component.spec.tsx und error-component.tsx und führe anschließend den Test mit den angegebenen Versionen von React, Jest und @testing-library/react aus. Verfolge den Renderpfad, um festzustellen, warum die Komponente, die den Fehler auslöst, wiederholt aufgerufen wird; das Problem ist gelöst, wenn die beobachtete Anzahl der Render-Vorgänge erklärt und das erwartete Verhalten durch eine geeignete Testabdeckung festgelegt wurde.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
- @testing-library/react version: 14.2.2
- Testing Framework and version: jest 29.4.1
- DOM Environment: jest-environment-jsdom 29.4.1
- node: 20.11.1
- react: 18.2.0
Relevant code or config:
https://github.com/R-J-dev/example-error-render-issue/blob/main/src/test-error/error-component.tsx
error-component.tsx (just as an example, I know the return is unreachable):
export const ErrorComponent = () => {
// This logs four times, but it should log only once?
console.log('test error in component');
// When debugging this during a test, the debugger wil stop here five times. But we expect that it stops only once?
throw new Error('test error in component');
return <p>This is an error test component, which will throw an error</p>;
};
error-component.spec.tsx:
import { render } from '@testing-library/react';
import { ErrorComponent } from './error-component';
it('should throw', () => {
expect(() => render(<ErrorComponent />)).toThrow('test error in component');
});
What you did:
I was trying to test if a specific error was thrown in a component and used the render function from @testing-library/react.
What happened:
When debugging the component that is called in the render function, I found out that it seems to load 4/5 times.
Without throwing an error in the component that is being rendered, the component only loads once.
When calling the component that throws an error without the render function, the component only loads once.
Reproduction:
Reproduction repo can be found here: https://github.com/R-J-dev/example-error-render-issue/tree/main
It's a new setup created with: npx create-nx-workspace@latest (https://nx.dev/getting-started/intro)
The test: https://github.com/R-J-dev/example-error-render-issue/blob/main/src/test-error/error-component.spec.tsx
The component: https://github.com/R-J-dev/example-error-render-issue/blob/main/src/test-error/error-component.tsx
Problem description:
Not sure if this is normal behavior, but for me it's unexpected behavior and couldn't find why this is happening.
Also:
- Probably not good for test performance
- Mocking things once isn't possible, when testing if an error has thrown
Suggested solution:
It looks like that the problem is in the render function from @testing-library/react, but besides from that I have no clue yet.
- Vorherrschende Sprache
- JavaScript
- Sterne
- 19.7k
- Forks
- 1.2k
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus testing-library/react-testing-library
-
fireEvent.select does not wrap its automatic native focus in actEvtl. vergeben @sergioperezcheco hat das vor 19 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict modeEvtl. wieder frei @suhailopensource hat das vor 89 Tagen übernommen, und es ist kein Pull Request offen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 35/100
testing-library/react-testing-library#1466 · 1 Kommentar ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 30/100
testing-library/react-testing-library#1459 · 2 Kommentare ·
-
perf: optimize container lookup with early exitEvtl. wieder frei @Ch-Abhinav-Chowdary hat das vor 315 Tagen übernommen, und es ist kein Pull Request offen. Offen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 35/100
testing-library/react-testing-library#1430 · 1 Kommentar ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)Evtl. vergeben @swarnim02 hat das vor 332 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 55/100
Alle Issues in testing-library/react-testing-library
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
glossyio/traffic-monitor#255 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
juice-shop/juice-shop#3706 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Main workflow runs twice for newly created refsEvtl. vergeben @rustyconover hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
duckdb/duckdb-wasm#2293 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 85/100
521xueweihan/HelloGitHub#4115 ·
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
outline/outline#14348 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag