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

228
Visualizações
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 à 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