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.

[new rule] Disallow overwriting methods with jest.fn()

See original GitHub issue

I have seen a lot of my colleagues write code like this (based on many examples I’ve seen in the jest community):

Date.now = jest.fn().mockReturnValue(new Date('2016'));

The problem with overwriting methods on singleton/global objects like this is that jest has no way to restore the mock after the test completes. This can lead to test interactions and nasty heisenbugs due to how jest runs tests in parallel.

A better way is via spyOn, ex:

jest.spyOn(Date, 'now').mockReturnValue(new Date('2016'));

This allows jest to track mocks and un-mock them later via the restoreMocks config flag or an explicit call to jest.restoreAllMocks().

Given that I don’t think the linter can reasonably determine whether or not a given object is a singleton, how about a rule that disallows any overwriting of object properties with jest.fn()? So something like this should still be valid:

const mockObject = { doSomething: jest.fn() };

but not the first example.

I’ve never written an eslint rule but if I can find time I might take a crack at this. Anyone else is welcome to also.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
SimenBcommented, Nov 3, 2018

🎉 This issue has been resolved in version 21.27.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

2reactions
hanneslundcommented, Oct 27, 2018

I’ll give this a try.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
Mock specific function override with jest - typescript
I'm trying to mock a function from the commander library. The actual code is something like this, ... mockProgram.description = jest.fn().
Read more >
How to automatically reset mocks and restore spies in Jest
When using Jest it seemed to be a common approach to manually invoke jest.resetAllMocks() ... How to prevent method overrides with jest.fn().
Read more >
Mock Functions or Spies Demystified - How Does jest.fn() Work?
The jest. fn method allows us to create a new mock function directly. If you are mocking an object method, you can use...
Read more >
How to mock imported functions with Jest - DEV Community ‍ ‍
We will assume that we're testing a couple of validation rules: ... To mock a function with Jest we use the jest.fn() function....
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