Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda