public icon indicating copy to clipboard operation
public copied to clipboard

Feature Request: Highlight mocked objects/modules

Open smcenlly opened this issue 1 year ago • 2 comments

From discord:

I spent a bunch of time tracking down why some code wasn't running only to find out that my test suite was mocking the module. I'm a little embarrassed to admit that this is the first time this has happened. I was wondering if it would be possible to detect whether a module has been mocked using jest.mock and then surface that to the user when they're looking at the source code for the file that is mocked. Maybe a banner of some sort or even giving the gutter indicators a different color in that situation. 🙏

smcenlly avatar Sep 28 '22 03:09 smcenlly

Thanks for opening this ticket. 🙂

The analysis of what's mocked and isn't mocked doesn't have to be perfect. It would still be useful if it could handle simple cases like:

jest.mock("./path/to/file.js");
jest.mock("./path/to/another-file.js", () => {
   // custom mock, but not calls to jest.requireActual()
});

If the mock includes a call to jest.requireActual() then all bets are off and we can fallback to the current behaviour.

The cases in the snippet above should be statically analyzable without having to inspect values at runtime.

kevinbarabash avatar Sep 28 '22 03:09 kevinbarabash

FTR, I meant to say this wasn't the first time I forgot that I had mocked a module.

kevinbarabash avatar Sep 28 '22 15:09 kevinbarabash