Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!