valid-mock-module-path
Added in v0.9.4Configuration
Rule Details
Disallow mocking of non-existing module paths.
This rule reports jest.mock() and jest.doMock() calls whose first argument names a module that cannot be found. Only a string literal is checked; a variable, template literal or other expression is ignored.
A path starting with . is resolved against the directory of the linted file. It exists when a file or directory is found at that path, or at that path followed by one of the moduleFileExtensions. Any other specifier is resolved the way Node's require.resolve resolves it: Node built-in modules are always accepted, packages are looked up in node_modules directories, and a package's exports field is honored with the require condition, so a path the package does not export is reported even when the file exists.
A call passing a third argument that is an object literal with a virtual property set to a truthy literal, such as { virtual: true }, is not checked, because Jest does not require a virtual module to exist.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Options
moduleFileExtensions
The file extensions tried after a local path that does not exist as written. The default extensions are:
".js"".ts"".jsx"".tsx"".json"
Each extension is appended to the path as written, so a custom extension must include its leading dot. This option does not affect package specifiers.
Differences from ESLint
- Package specifiers are looked up in
node_modulesdirectories starting from the linted file's directory, the way Jest looks them up when running the test. eslint-plugin-jest looks them up from its own install location instead. The results differ only when a package is installed where one location can see it and the other cannot, for example in a nested package of a monorepo. - For the same reason, a
#subpath import such asjest.mock('#utils')is resolved through theimportsfield of thepackage.jsonthat owns the linted file, and is accepted when that field maps it to an existing file. eslint-plugin-jest reports every#import.
When Not To Use It
Don't use this rule on non-jest test files.