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

188
Views
Jest - Making imported functions from source file use mocked instances of function in same file

I'm writing some unit tests for my Node backend using jest, and keep running into the same problem. I have a test file, let's say logic.test.js, that tests functionality from logic.js. logic.js consists of two functions - dataRequestFunction and logicFunction, where logicFunction makes use of dataRequestFunction to retrieve some data with an HTTP request and then performs some sort of data handling afterwards.

I want to mock the functionality of dataRequestFunction so that I can control what is returned, so that I know logicFunction operates on the correct data. What I've done so far is using jest.requireActual, like the following

jest.mock('./logic', () => ({
  ...jest.requireActual('./logic'),
  dataRequestFunction: jest
    .fn()
    .mockImplementation(() => ({ data: 'someData' }))
}))

I then import logicFunction as usual. I want to use logicFunction in the test file, for example expect(logicFunction()).toEqual('somedata') (let's say the logic function's job is capitalizing the result from (await dataRequestFunction()).data). How do I accomplish this? With this implementation, logicFunction calls the non-mocked version of the function - however, if I call dataRequestFunction explicitly in the test file, ({ data: 'someData' }) is returned.

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!