jest-community / eslint-plugin-jest

ESLint plugin for Jest
MIT License
1.12k stars 228 forks source link

New rule to validate path given to `jest.mock` #1584

Open weary-pilgrim opened 2 months ago

weary-pilgrim commented 2 months ago

It would be really useful to have a lint rule that tells you when the path supplied to jest.mock does not exist. I would envision the rule working like this:

Disallow mocking of module paths that do not exist

When using jest.mock and jest.doMock, the path supplied must be an module or local file that exists.

Rule details

This rule triggers an error if the path supplied to jest.mock or jest.doMock does not exist.

The following patterns are considered errors:

// Module that cannot be found
jest.mock('some-module-not-in-package-json');

// Local file that cannot be found
jest.mock('../../this/path/does/not/exist');

The following patterns not considered errors:

// Module that can be found
jest.mock('some-module-in-package-json');

// Local file that can be found
jest.mock('../../this/path/really/does/exist');
G-Rath commented 2 months ago

I think this is technically a duplicate of #1346 but your writeup is a lot clearer so will close the other one 😄