导入`spyOn`函数以摆脱'未定义'的eslint错误

ala*_*yor 2 javascript unit-testing eslint jestjs

我有一个使用spyOn方法的单元测试jest.

import React from 'react';
import expect from 'jest-matchers';
import PointsAwardingPage from '../PointsAwardingPage';
import PointsAwardingForm from '../children/PointsAwardingForm';
import { shallow } from 'enzyme';

it("should call change method in form", () => {
  // given
  spyOn(PointsAwardingPage.prototype, 'change').and.callThrough();
  const form = shallow(<PointsAwardingPage />).find('PointsAwardingForm');

  // when
  form.props().onChange();

  // then
  expect(PointsAwardingPage.prototype.change).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)

一切都很好.但是,我看到eslint有关spyOn函数调用的以下错误消息.

spyOn is not defined (no-undef).

import我可以使用哪种语句来摆脱这个错误?

Or *_*r B 5

虽然global评论是有效的解决方案,但我相信您可以简单地使用jest.spyOn().

别忘了jest在你的定义.eslintrc:

"env": {
    "jest": true
}
Run Code Online (Sandbox Code Playgroud)