• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

94
Vistas
Should you mock modules within a beforeEach?

For example should you do something like:

(1)

beforeEach(() => {
  jest.mock('react-router-dom', () => ({
    ...(jest.requireActual('react-router-dom')),
    useLocation: () => ({
      pathName: '/',
      search: '?something=everything'
    })
  }));
})

or is importing react-router-dom like that for every test wasteful (it seems so... if you have hundreds of tests this must cause test slowdown right)?

For example instead you could do this:

(2)

let mockUseLocation;
jest.mock('react-router-dom', () => ({
  ...(jest.requireActual('react-router-dom')),
  useLocation: () => mockUseLocation
}));

beforeEach(() => {
  mockUseLocation = {
    pathName: '/',
    search: '?something=everything'
  }
})

Is there a best practice about something like this? Is my intuition correct that the second option is better than the first?

about 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda