Skip to content

jest enzyme TypeError: Cannot read property ‘fn’ of undefined

I am writing test and below is my test case.

import { mount, jest } from 'enzyme';

it('should submit job post form on save button click', () => {
        const onPostJobSubmit = jest.fn(); // Here is the error.
        const instance = wrapper.instance();
        wrapper.find('button').simulate('click');
        expect(onPostJobSubmit).toHaveBeenCalled();
    });

When I run test I get below error.

TypeError: Cannot read property 'fn' of undefined

      61 |
      62 |  it('should submit job post form on save button click', () => {
    > 63 |      const onPostJobSubmit = jest.fn();

What is wrong with that and how can I fix this?

Answer

From the doc:

The jest object is automatically in scope within every test file. The methods in the jest object help create mocks and let you control Jest’s overall behavior. It can also be imported explicitly by via import {jest} from '@jest/globals'.