Skip to content

jest/no-mocks-import Style

What it does

This rule reports imports from a path containing a mocks component.

Why is this bad?

Manually importing mocks from a __mocks__ directory can lead to unexpected behavior and breaks Jest's automatic mocking system. Jest is designed to automatically resolve and use mocks from __mocks__ directories when jest.mock() is called. Directly importing from these directories bypasses Jest's module resolution system and can cause inconsistencies between test and production environments.

Examples

Examples of incorrect code for this rule:

ts
import thing from "./__mocks__/index";
require("./__mocks__/index");

Examples of correct code for this rule:

ts
import thing from "thing";
require("thing");

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-mocks-import": "error"
  }
}
bash
oxlint --deny jest/no-mocks-import --jest-plugin

References

Released under the MIT License.