spyOn not working while Jest 24.1 is installed

时间:2019-02-18 00:34:34

标签: reactjs jestjs

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();
  });
}

1 个答案:

答案 0 :(得分:0)

只需删除此行:

import jest from 'jest';

Jest查找并运行您的测试,因此jest already exists in the scope of your test会在运行时进行测试。无需导入它,这样做会导致您看到的错误。