0
votes

jest.spyOn does not work for an imported function.

There is an error - Expected mock function to have been called, but it was not called.

import testFn from 'test'
// test.js
// import foo from 'foo'
// export default () => foo('text')

import * as foo from 'foo'
// foo.js
// export default (value) => value

const spyFn = jest.spyOn(foo, 'default')

testFn()

expect(spyFn).toHaveBeenCalled()

I expect that the default function is mocked.

1

1 Answers

0
votes

Found the solution.

import testFn from 'test'

...

it('ola', () => {

  const foo = require('foo')
  const spyFn = jest.spyOn(foo, 'default')

  testFn()
  expect(spyFn).toHaveBeenCalled()


})

...