1

Im using Jest / enzyme to test react components and having some trouble getting my mock function to actually get used:

The file I am testing

export const myFunction = () => {}
const MyComponent = () => {
  return <> {myFunction()} </>
}
export default MyComponent

My test file

import MyComponent, {myFunction} from 'myFile.jsx'

jest.mock('myFile.jsx', () => ({
  __esModule: true,
  ...requireActual('myFile.jsx'),
  myFunction: jest.fn()
}))

// then a test
it('test', () => {
  myFunction.mockReturnValue('one') // this never gets mocked when I call the component
  ...mountComponent
  log( myFunction.mock.calls.length) // 0
})

So as you can see the mocks are working however the actual mock function is not getting called. Anyone have any ideas about what could cause this?

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Sid
  • 846
  • 3
  • 12
  • 25
  • Duplicated question, see https://stackoverflow.com/questions/45111198/how-to-mock-functions-in-the-same-module-using-jest – Lin Du Dec 22 '21 at 02:23

0 Answers0