Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
jest.mock doesn't work when called inside a function

The setup:

I have module that I want to mock in my tests, so I write this:

jest.mock('path/to/module, () => mocked_value);

in the top of my test file and it works fine.

The problem:

I want to use the same mock (mocked_value is a somewhat large object, and there are multiple modules being mocked) in multiple tests, but I cannot put the jest.mock calls into a separate function in a separate file, because then it doesn't work.

I have tried doMock instead, but is also doesn't work. The test is in TypeScript if it is relevant.

Edit:

doMock should work for this purpose, but it doesn't work for me at all. Even when I put doMock directly into the test file (where mock was working just fine), it doesn't work. Putting it directly into the test function doesn't work either.

Environment:

  • OS: Windows 10
  • Editor: VS Code
  • Terminal: Git Bash
  • Typescript: 4.4.4
  • Node: v17.0.1
  • (jest config) testEnvironment: jsdom
  • (jest config) resolver: jest-pnp-resolver
  • (jest config) transform: babel-jest:
about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda