question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for spyOn() as a valid global

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
SimenBcommented, May 11, 2018

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

0reactions
GeorgeTaveras1231commented, May 11, 2018

I’ve also found that there are behavioral differences between spyOn and jest.spyOn.

In my test, I initially had:

spyOn(window.location, 'assign')

/* Omitted */

expect(window.location.assign).toHaveBeenCalledWith('/some-path')

Which worked fine.

After changing it to use the jest global, it stopped working

jest.spyOn(window.location, 'assign')

/* Omitted */

expect(window.location.assign).toHaveBeenCalledWith('/some-path')

This would yield the JSDOM error:

SyntaxError: Could not resolve the given string "/some-path" relative to the base URL "about:blank"

To fix this, I had to explicitly mock the implementation of the spy:

jest.spyOn(window.location, 'assign').mockImplementation(() => {}) // < Call .mockImplementation

/* Omitted */

expect(window.location.assign).toHaveBeenCalledWith('/some-path')

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Jest spyOn with React.js and Fetch - Meticulous
Follow this step-by-step guide on Jest's SpyOn method, with a practical example using React and Fetch. This post also includes a comparison ...
Read more >
How to spyOn a value property (rather than a method) with ...
Just spy and stub. const spy = spyOnProperty(myObj, 'myGetterName', 'get').and.
Read more >
Mocking a global function - Google Groups
How do I mock (spy) a function in the global space? For example, I have in common.js: isPending() { ... } ...and I...
Read more >
The Jest Object
The methods in the jest object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by...
Read more >
spyon is not defined jest | The AI Search Engine You Control
jest-community/eslint-plugin-jestAdd support for spyOn() as a valid global#35. Created about 5 years ago. 5. I am trying to use the spyOn() method in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found