Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Calling render on a component that throws, results in 4 renders

オープン
#1,291 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
javascript, react, typescript
領域
frontend, testing

調査の方向性

リンクされている error-component.spec.tsx と error-component.tsx の再現手順から始め、指定されている React、Jest、@testing-library/react のバージョンでテストを実行します。レンダリングパスを追跡して、エラーをスローするコンポーネントが繰り返し呼び出される理由を特定します。観測されたレンダリング回数が説明され、適切なテストカバレッジによって期待される動作が確立されれば、この問題は解決です。

索引モデルが issue の本文から書いたものです。

説明

  • @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.

主要言語
JavaScript
スター
19.7k
フォーク
1.2k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

testing-library/react-testing-library のほかの issue

testing-library/react-testing-library の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。