close

jest/no-mocks-import

Configuration

PresetConfigured Value
✅ jestPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, jestPlugin } from '@rslint/core';

export default defineConfig([
  jestPlugin.configs.recommended,
  {
    rules: {
      'jest/no-mocks-import': 'error',
    },
  },
]);

Rule Details

When using jest.mock, tests should import from the original module path (for example ./x), not from ./__mocks__/x. Importing directly from a __mocks__ path can leave you with more than one instance of the mocked module that are not the same reference, which is easy to misread and can make assertions fail in surprising ways.

This rule reports import declarations and require() calls whose module specifier path contains a __mocks__ segment.

Examples of incorrect code for this rule:

import thing from './__mocks__/index';
require('./__mocks__/index');

Examples of correct code for this rule:

import thing from 'thing';
require('thing');

Original Documentation