【问题标题】:spyOn not working while Jest 24.1 is installed安装 Jest 24.1 时 spyOn 无法正常工作
【发布时间】:2019-07-11 08:24:16
【问题描述】:

在我的 package.json 我有 jest 24.1.0 但我的测试告诉我

“TypeError: _jest.default.spyOn 不是函数”

Jest 文档说 spyOn 是我可以使用的一种方法,但不知何故我无法使用它。我做错了什么?

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

这是我的测试...

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

【问题讨论】:

    标签: reactjs jestjs


    【解决方案1】:

    只需删除这一行:

    import jest from 'jest';
    

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

    【讨论】:

      猜你喜欢
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2012-08-19
      相关资源
      最近更新 更多