2020-08-26 01:57:08 +02:00
|
|
|
# Disallow manually importing from `__mocks__` (`no-mocks-import`)
|
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
💼 This rule is enabled in the ✅ `recommended`
|
|
|
|
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
|
|
|
|
|
|
|
|
<!-- end auto-generated rule header -->
|
|
|
|
|
2020-08-26 01:57:08 +02:00
|
|
|
When using `jest.mock`, your tests (just like the code being tested) should
|
|
|
|
import from `./x`, not `./__mocks__/x`. Not following this rule can lead to
|
|
|
|
confusion, because you will have multiple instances of the mocked module:
|
|
|
|
|
|
|
|
```js
|
|
|
|
jest.mock('./x');
|
|
|
|
const x1 = require('./x');
|
|
|
|
const x2 = require('./__mocks__/x');
|
|
|
|
|
|
|
|
test('x', () => {
|
|
|
|
expect(x1).toBe(x2); // fails! They are both instances of `./__mocks__/x`, but not referentially equal
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
## Rule details
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
This rule reports imports from a path containing a `__mocks__` component.
|
|
|
|
|
|
|
|
Example violations:
|
|
|
|
|
|
|
|
```js
|
|
|
|
import thing from './__mocks__/index';
|
|
|
|
require('./__mocks__/index');
|
|
|
|
require('__mocks__');
|
|
|
|
```
|