George Eracleous
George Eracleous

Reputation: 4412

Property 'mockImplementation' does not exist on type when using mock with ts-jest

I have the following code and I am trying to run a Jest test using ts-jest.

jest.mock('../foo');
import configure from '../configure';
import foo from '../foo';

test('test name', () => {
  foo.mockImplementation(() => { methods: {}});   
  configure();
});

The function configure calls foo internally and I want to mock the foo function. However, when I run the test I get Property 'mockImplementation' does not exist on type.

I have checked the other answers on SO and I found out that there is a ts-jest helper called mocked which can be used to assert to TS that this is a mock (here and here).

However, when I try to do import { mocked } from 'ts-jest/utils' as described there I get the error:

Cannot find module 'ts-jest/utils' or its corresponding type declarations.

I looked at the docs for ts-jest but couldn't find anything. I found some docs for mocked on Github but they look like they are for older versions.

I also found this that mentions that I can use as jest.Mocked<typeof foo>; but since I'm using ts-jest is that the best way?

Can someone please help me understand how to do this?

Upvotes: 0

Views: 1806

Answers (1)

George Eracleous
George Eracleous

Reputation: 4412

For anyone still struggling with this I just found out that jest has a mocked utility (see here) and apparently ts-jest has deprecated its own mocked in favor of that (see here).

Upvotes: 1

Related Questions