Add support for spyOn() as a valid global
See original GitHub issueI am trying to use the spyOn()
method in my Jest tests. Although this is a valid global Jest function, it is still causing the following ESLint error:
error 'spyOn' is not defined
I have followed the instructions to setup this plugin, including installing it as a devDependency, adding “jest” to my eslintrc plugins and adding “jest/globals” to the env
object. ESLint still throws an error. I even tried extending “plugin:jest/recommended”, but that still throws the error.
What I find interesting is that there was a past commit (2984eb50c) that introduced support for spyOn
as a valid global. However, there was soon after another commit (491271aaa) that reverted the previous one, so that support for spyOn
was removed. I am curious to understand more about the intent of these commits. It seems like the former commit, which includes support for spyOn
as a valid global, would fix my issue.
In the meanwhile, I am getting around this my adding a /* global spyOn */
comment to each file that uses spyOn()
. Ideally, this project can make ESLint smart enough to know that spyOn
is a valid global method.
Thank you.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top GitHub Comments
Using
spyOn
,pending
,fail
etc is not really supported.That said, I’d be happy to have a rule saying “don’t use jasmine globals”, whilst adding this to our list of globals
I’ve also found that there are behavioral differences between
spyOn
andjest.spyOn
.In my test, I initially had:
Which worked fine.
After changing it to use the
jest
global, it stopped workingThis would yield the
JSDOM
error:To fix this, I had to explicitly mock the implementation of the spy:
Conclusion
It seems like
spyOn
automatically mocks the implementation with a No-Op function, which allowed the original version of the test to work.Hopefully with this I’ve demonstrated that there may be use cases for the global
spyOn
. However, it is also hard to tell whether this difference was actually intended by the Jest developers.