vitest/prefer-import-in-mock

March 5, 2026 ยท View on GitHub

๐Ÿ“ Prefer dynamic import in mock.

โš ๏ธ This rule warns in the ๐ŸŒ all config.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

This rule enforces using a dynamic import() in vi.mock() and vi.doMock(), which improves type information and IntelliSense for the mocked module.

Rule details

The following patterns are considered a warning:

vi.mock('./path/to/module')
vi.doMock('./path/to/module')

The following patterns are not considered a warning:

vi.mock(import('./path/to/module'))
vi.doMock(import('./path/to/module'))

Options

NameDescriptionType
fixableWhether the rule should provide an autofix.Boolean

This rule has a fixable option that tells the plugin to automatically fix the imports for you. The option is enabled by default. You can disable it in your eslint.config.js file using the following configuration.

import vitest from 'eslint-plugin-vitest'

export default [
  {
    files: ['**/*.ts', '**/*.js'], // or any other pattern
    plugins: {
      vitest,
    },
    rules: {
      ...vitest.configs.recommended.all,
      'vitest/prefer-import-in-mock': ['error', { fixable: false }],
    },
  },
]