Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/rules/prefer-import-in-mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@

<!-- end auto-generated rule header -->

This rule enforces using a dynamic import() in vi.mock(), which improves type information and IntelliSense for the mocked module.
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 pattern is considered a warning:
The following patterns are considered a warning:

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

The following pattern is not considered a warning:
The following patterns are not considered a warning:

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

### Options
Expand Down
4 changes: 3 additions & 1 deletion src/rules/prefer-import-in-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { parseVitestFnCall } from '../utils/parse-vitest-fn-call'

const RULE_NAME = 'prefer-import-in-mock'

const MOCK_METHODS = new Set(['mock', 'doMock'])

type MESSAGE_ID = 'preferImport'
type Options = [
Partial<{
Expand Down Expand Up @@ -54,7 +56,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

if (
property.type != AST_NODE_TYPES.Identifier ||
property.name != 'mock'
!MOCK_METHODS.has(property.name)
) {
return
}
Expand Down
79 changes: 79 additions & 0 deletions tests/prefer-import-in-mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ describe(rule.name, () => {
'vi.mock(import("node:fs/promises"))',
'vi.mock(import("./foo.js"), () => ({ Foo: vi.fn() }))',
'vi.mock(import("./foo.js"), { spy: true });',
'vi.doMock(import("foo"))',
'vi.doMock(import("node:fs/promises"))',
'vi.doMock(import("./foo.js"), () => ({ Foo: vi.fn() }))',
],
invalid: [
{
Expand Down Expand Up @@ -53,6 +56,49 @@ describe(rule.name, () => {
`,
errors: [{ messageId: 'preferImport', column: 9, line: 3 }],
},
{
options: [
{
fixable: false,
},
],
code: `vi.doMock('foo', () => {})`,
errors: [
{
messageId: 'preferImport',
},
],
},
{
options: [
{
fixable: false,
},
],
code: 'vi.doMock("node:fs/promises")',
errors: [{ messageId: 'preferImport', column: 1, line: 1 }],
},
{
options: [
{
fixable: false,
},
],
code: 'vi.doMock("./foo.js", () => ({ Foo: vi.fn() }))',
errors: [{ messageId: 'preferImport', column: 1, line: 1 }],
},
{
options: [
{
fixable: false,
},
],
code: `
import { vi as renamedVi } from 'vitest';
renamedVi.doMock('./foo.js', () => ({ Foo: vi.fn() }))
`,
errors: [{ messageId: 'preferImport', column: 9, line: 3 }],
},
],
})

Expand All @@ -62,6 +108,9 @@ describe(rule.name, () => {
'vi.mock(import("node:fs/promises"))',
'vi.mock(import("./foo.js"), () => ({ Foo: vi.fn() }))',
'vi.mock(import("./foo.js"), { spy: true });',
'vi.doMock(import("foo"))',
'vi.doMock(import("node:fs/promises"))',
'vi.doMock(import("./foo.js"), () => ({ Foo: vi.fn() }))',
],
invalid: [
{
Expand Down Expand Up @@ -94,6 +143,36 @@ describe(rule.name, () => {
renamedVi.mock(import('./foo.js'), () => ({ Foo: vi.fn() }))
`,
},
{
code: `vi.doMock('foo', () => {})`,
errors: [
{
messageId: 'preferImport',
},
],
output: `vi.doMock(import('foo'), () => {})`,
},
{
code: 'vi.doMock("node:fs/promises")',
errors: [{ messageId: 'preferImport', column: 1, line: 1 }],
output: "vi.doMock(import('node:fs/promises'))",
},
{
code: 'vi.doMock("./foo.js", () => ({ Foo: vi.fn() }))',
errors: [{ messageId: 'preferImport', column: 1, line: 1 }],
output: "vi.doMock(import('./foo.js'), () => ({ Foo: vi.fn() }))",
},
{
code: `
import { vi as renamedVi } from 'vitest';
renamedVi.doMock('./foo.js', () => ({ Foo: vi.fn() }))
`,
errors: [{ messageId: 'preferImport', column: 9, line: 3 }],
output: `
import { vi as renamedVi } from 'vitest';
renamedVi.doMock(import('./foo.js'), () => ({ Foo: vi.fn() }))
`,
},
],
})
})