# vitest/prefer-import-in-mock

> Require `import()` instead of a string path in `vi.mock()` and `vi.doMock()`

TypeScript doesn't check a path that's written as a string. With `vi.mock(import("./api"))` it knows which module is mocked, so a wrong path is a type error and `importOriginal()` in the factory has the module's type.

`--fix` wraps the string in `import()`.

{/* 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]
"vitest/prefer-import-in-mock" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"vitest/prefer-import-in-mock" = ["error", {
  fixable = true,
}]
```

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

## Examples

Reported:

{/* bad */}
```ts
import { vi } from "vitest";

vi.mock("./api", async importOriginal => {
  const api = await importOriginal();
  return { ...api, fetchUser: vi.fn() };
});
```

Not reported:

{/* good */}
```ts
import { vi } from "vitest";

vi.mock(import("./api"), async importOriginal => {
  const api = await importOriginal();
  return { ...api, fetchUser: vi.fn() };
});
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `fixable` | `true` | Set it to `false` to keep the report but leave the code alone with `--fix`. |

{/* bad {"options": [{"fixable": false}]} */}
```ts
import { vi } from "vitest";

vi.doMock("./api");
```
