Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

59
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?

7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.