# prefer-import-in-mock

Rule `vitest/prefer-import-in-mock`. Prefer dynamic import in mock

- Source: vitest
- Technologies: Testing
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #1797 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `vitest/prefer-import-in-mock` | no | safe | package `@vitest/eslint-plugin` | [docs](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/prefer-import-in-mock.md) |
| Oxlint | `vitest/prefer-import-in-mock` | no | safe | plugin `vitest`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/vitest/prefer-import-in-mock.html) |

## Code examples

### Incorrect: the rule reports this

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

### Correct: the rule accepts this

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

Examples from the Oxlint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import vitest from "@vitest/eslint-plugin";

export default [
  {
    plugins: { vitest },
    rules: {
      "vitest/prefer-import-in-mock": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "plugins": [
    "vitest"
  ],
  "rules": {
    "vitest/prefer-import-in-mock": "error"
  }
}
```

## Related rules

- [vitest/prefer-expect-type-of](https://lintradar.dev/rules/vitest/prefer-expect-type-of.md): Enforce using `expect(...).toBeTypeOf(...)` instead of `expect(typeof ...).toBe(...)`
- [vitest/prefer-mock-promise-shorthand](https://lintradar.dev/rules/vitest/prefer-mock-promise-shorthand.md): Enforce mock resolved/rejected shorthands for promises
- [vitest/prefer-expect-resolves](https://lintradar.dev/rules/vitest/prefer-expect-resolves.md): Enforce using `expect().resolves` over `expect(await ...)` syntax
- [vitest/prefer-mock-return-shorthand](https://lintradar.dev/rules/vitest/prefer-mock-return-shorthand.md): Prefer mock return shorthands
- [vitest/prefer-expect-assertions](https://lintradar.dev/rules/vitest/prefer-expect-assertions.md): Enforce using expect assertions instead of callbacks
- [vitest/prefer-snapshot-hint](https://lintradar.dev/rules/vitest/prefer-snapshot-hint.md): Enforce including a hint with external snapshots

---

Web page: https://lintradar.dev/rules/vitest/prefer-import-in-mock. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
