1

I've got a typescript class EntityConfigurationMapper, with a map method. I'm mocking it in my Jest tests like this:

const mock = jest.fn<EntityConfigurationMapper>()
      .mockImplementation(() => ({
        map: (conf): Entity => {
          return {...};
        }
      }));
const instance = new entityConfigurationMapperMock();

Then it gets used etc.

I want to check the calls, which should be like mock.calls[0][0] etc. However, the mock.calls is an empty array.

I've put a console in the mocked map method, and it outputs as expected, so the mock is calling the mocked implementation, just not recording it the calls array.

Any idea why calls would be empty?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Joe
  • 6,773
  • 2
  • 47
  • 81

1 Answers1

0

Ah, worked it out with the help of this. You have to use jest.fn to mock the function calls as well as the class:

const map = jest.fn((conf, ids): Entity  => {
  return {...};
})

const mock = jest.fn<EntityConfigurationMapper>()
    .mockImplementation(() => ({
      map
    }));
const instance = new entityConfigurationMapperMock();

You can then investigate the calls on map:

map.mock.calls
Joe
  • 6,773
  • 2
  • 47
  • 81