Skip to content
Advertisement

Getting `TypeError: jest.fn is not a function`

I’m trying to create the following unit test using Jest.

JavaScript

But I’m getting the following error after running npm test.

TypeError: jest.fn is not a function

This is some section of my package.json:

JavaScript

What could be the reason I’m getting that error?

Advertisement

Answer

The jest object is automatically in scope within every test file, so there’s no need to import it explicitly. If you do want to import the jest object directly, you want to import the jest-mock module, not the jest-cli module, via:

JavaScript
Advertisement