testing-library / testing-library/react-testing-library

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

Open
#1,291 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
19.7k
Forks
1.2k
Avg merge
3d 16h
Merged PRs (30d)
1

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked error-component.spec.tsx and error-component.tsx reproduction, then run the test with the stated React, Jest, and @testing-library/react versions. Trace the render path to determine why the throwing component is invoked repeatedly; the issue is resolved when the observed render count is explained and the expected behavior is established with appropriate test coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react, typescript
Domain
frontend, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.