Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
How to test different types of test for a single render in React with testing-library?

I'm new in TDD for the React. I've decided to use the react-testing-library to start TDD for development. Suppose that, I should check a prop and simple text in component:

import { render, screen } from '@testing-library/react';
import Card from '../Card';

test('render without crash', () => {
  render(<Card />);
  expect(screen.getByText('Card Component')).toBeInTheDocument();
});

test('title', () => {
  render(<Card title="test" />);
  expect(screen.getByText('test')).toBeInTheDocument();
});

I used render two times two check different types of rendering for my component. But I was worry about performance... Is it best practice to separately use render in any tests? or I should create something like this common variable:

const Component = render(<Card title={...} prop2={} prop3={} ... />);

then use Component instead using render again?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I don't this this would be a bad practice. I usually use one of these approach :

  • render my component once in a beforeEach, and use it in multiple test(), if I always need the same setup for each test (props, contexts, redux store...),
  • render my component in each test(), so I can adjust props and context and test multiple configuration.

See https://stackoverflow.com/a/61838982/2295549 for a more complete explanation, and some pros/cons.

But using render multiple times is not a bad practice, in fact it is a pattern used in official docs.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!