【发布时间】: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();
});
}
【问题讨论】: