# jest/no-mocks-import

> Disallow importing from a `__mocks__` directory

Jest picks up the files in `__mocks__` automatically when you call `jest.mock()`. Importing one by its path doesn't mock anything. The code under test still gets the real module, and your assertions look at a mock that nobody calls. Even with `jest.mock()`, the import by path is a second copy of the mock, not the one the code under test calls.

Import the real path and mock it. The test and the code under test then share the same mock.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"jest/no-mocks-import" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```ts
import { expect, test } from "@jest/globals";
import { sendEmail } from "./__mocks__/mailer";
import { register } from "./register";

test("sends a welcome email", () => {
  register("ada@example.com");
  expect(sendEmail).toHaveBeenCalled();
});
```

`require()` is checked too:

{/* bad */}
```js
const { sendEmail } = require("../__mocks__/mailer");
```

Not reported:

{/* good */}
```ts
import { expect, jest, test } from "@jest/globals";
import { sendEmail } from "./mailer";
import { register } from "./register";

jest.mock("./mailer");

test("sends a welcome email", () => {
  register("ada@example.com");
  expect(sendEmail).toHaveBeenCalled();
});
```
