Calling render on a component that throws, results in 4 renders
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 25/100
- Type d'issue
- Bug
- Clarté
- À clarifier
- Activité
- À l'abandon
- Stack technique
- javascript, react, typescript
Piste de recherche
Commencez par la reproduction liée dans error-component.spec.tsx et error-component.tsx, puis exécutez le test avec les versions indiquées de React, Jest et @testing-library/react. Suivez le chemin de rendu pour déterminer pourquoi le composant qui lève l’erreur est invoqué à plusieurs reprises ; le problème est résolu lorsque le nombre de rendus observé est expliqué et que le comportement attendu est établi avec une couverture de tests appropriée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
- @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.
- Langage dominant
- JavaScript
- Étoiles
- 19.7k
- Forks
- 1.2k
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de testing-library/react-testing-library
-
fireEvent.select does not wrap its automatic native focus in actPeut-être pris @sergioperezcheco l’a pris il y a 19 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict modePeut-être à nouveau libre @suhailopensource l’a pris il y a 89 jours, et aucune pull request n’est ouverte. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 35/100
testing-library/react-testing-library#1466 · 1 commentaire ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 30/100
testing-library/react-testing-library#1459 · 2 commentaires ·
-
perf: optimize container lookup with early exitPeut-être à nouveau libre @Ch-Abhinav-Chowdary l’a pris il y a 315 jours, et aucune pull request n’est ouverte. Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 35/100
testing-library/react-testing-library#1430 · 1 commentaire ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)Peut-être pris @swarnim02 l’a pris il y a 333 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 55/100
Toutes les issues de testing-library/react-testing-library
Issues similaires
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
glossyio/traffic-monitor#255 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
juice-shop/juice-shop#3706 ·
Les mainteneurs répondent en général sous 1 jour
-
Main workflow runs twice for newly created refsPeut-être pris @rustyconover l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
duckdb/duckdb-wasm#2293 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 85/100
521xueweihan/HelloGitHub#4115 ·
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
outline/outline#14348 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour