I have a unit test that uses spyOn
method from 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();
});
Everything works well. However, I see the following eslint
error message regarding the spyOn
function call.
spyOn is not defined (no-undef)
.
Which import
statement can I use in order to get rid of this error?
Although a global
comment is a valid solution, I believe you could simply use jest.spyOn()
instead.
Don't forget to define jest
in your .eslintrc
:
"env": {
"jest": true
}
That's because spyOn
is provided by the test environment - in your case Jest - thus it's not defined by you.
ESLint looks for definitions in your code only.
An easy and safe way to get rid of it is to place a comment /*global spyOn*/
at the top of your test file, which tells ESLint that you have defined it, without actually doing so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With