Jessi
Jessi

Reputation: 821

spyOn not working while Jest 24.1 is installed

In my package.json I have jest 24.1.0 yet my test tell me

"TypeError: _jest.default.spyOn is not a function"

The Jest docs say spyOn is a method I can use but somehow it is not available to me. What am I doing wrong?

https://jestjs.io/docs/en/jest-object#jestspyonobject-methodname

here is my test...

import React from 'react';
import jest from 'jest';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({adapter: new Adapter()});
import  NavLink  from '../Tabs/NavLink';

describe('NavLink', () => {
  it('handles onClick prop', () => {
    const onClick = jest.fn();
    const e = jest.spyOn('e', ['preventDefault']);
    const wrapper = shallow(
      <NavLink onClick={onClick} />
    );

    wrapper.find('a').simulate('click', e);
    expect(onClick).toHaveBeenCalled();
    expect(e.preventDefault).not.toHaveBeenCalled();
  });
}

Upvotes: 0

Views: 545

Answers (1)

Brian Adams
Brian Adams

Reputation: 45840

Just remove this line:

import jest from 'jest';

Jest finds and runs your tests, so jest already exists in the scope of your test by the time it is running. There is no need to import it and doing so is causing the error you are seeing.

Upvotes: 1

Related Questions