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

138
Views
jest not mocking internal function

I am having an issue with mocking functions called from within my module. when the mocked function is called from the test, it works as expected. but when that same mocked function is called from the library, it calls the actual function.

I have tried the jest.mock('./myModule', () => {}) approach, and enableAutomock as well, but with the same results.

I feel like i have not had this problem in other projects, but have looked through my jest configuration, and don't see anything that would effect it.

what am i missing here? how can i mock functions called internally within my module?

// myModule.js

export function foo() {
  return 'foo'
}

export function bar() {
  return foo()
}
// myModule.test.js
import * as myModule from './myModule';

jest.spyOn(myModule, 'foo').mockReturnValue('mock foo');

// i have also tried...
// jest.mock('./myModule', () => {
//   ...(jest.requireActual('./myModule')),
//   foo: jest.fn().mockReturnValue('mock foo')
// });

it('should', () => {
  expect(myModule.foo()).toEqual('mock foo'); // PASS: returns 'mock foo'
  expect(myModule.bar()).toEqual('mock foo'); // FAIL: returns 'foo'
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I found using ts-jest in my jest config, allowed these tests to run as i expected

transform: {
  '^.+\\.[tj]sx?$': ['ts-jest'],
},
about 4 years ago · Juan Pablo Isaza Report
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!