Skip to content

Commit

Permalink
feat: added the require-mock-type-parameters rule (#651)
Browse files Browse the repository at this point in the history
* test(require-mock-type-parameters): added tests

* feat(require-mock-type-parameters): implemented the rule

* feat(require-mock-type-parameters): added support for linting importActual and importMock

* docs(require-mock-type-parameters): added rule docs

* feat: export rule

* fix: apply linter

---------

Co-authored-by: Verite Mugabo <mugaboverite@gmail.com>
  • Loading branch information
marekdedic and veritem authored Feb 8, 2025
1 parent d9be59d commit 5674c25
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 174 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export default [
| [prefer-vi-mocked](docs/rules/prefer-vi-mocked.md) | Prefer `vi.mocked()` over `fn as Mock` | | 🌐 | 🔧 | | |
| [require-hook](docs/rules/require-hook.md) | require setup and teardown to be within a hook | | 🌐 | | | |
| [require-local-test-context-for-concurrent-snapshots](docs/rules/require-local-test-context-for-concurrent-snapshots.md) | require local Test Context for concurrent snapshot tests || 🌐 | | | |
| [require-mock-type-parameters](docs/rules/require-mock-type-parameters.md) | enforce using type parameters with vitest mock functions | | 🌐 | | | |
| [require-to-throw-message](docs/rules/require-to-throw-message.md) | require toThrow() to be called with an error message | | 🌐 | | | |
| [require-top-level-describe](docs/rules/require-top-level-describe.md) | enforce that all tests are in a top-level describe | | 🌐 | | | |
| [valid-describe-callback](docs/rules/valid-describe-callback.md) | enforce valid describe callback || 🌐 | | | |
Expand Down
57 changes: 57 additions & 0 deletions docs/rules/require-mock-type-parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Enforce using type parameters with vitest mock functions (`vitest/require-mock-type-parameters`)

⚠️ This rule _warns_ in the 🌐 `all` config.

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

When using `vi.fn()` to mock functions, by default, the mocked function has the type of `(...args: any[]) => any`. To add more specific types to the mocked function, a type parameter needs to be added to the call, e.g. `vi.fn<(arg1: string, arg2: boolean) => number>()`.

Additionally, there are two more mock functions with type parameters that cannot be automatically inferred by the TypeScript compiler, `vi.importActual` and `vi.importMock`. This rule doesn't by default report these function, however, the check can be enabled by setting the `checkImportFunctions` rule option.

The following patterns are considered bad:

```ts
import { vi } from 'vitest'

test('foo', () => {
const myMockedFn = vi.fn()
})
```

The following patterns are considered OK:

```ts
import { vi } from 'vitest'

test('foo', () => {
const myMockedFnOne = vi.fn<(arg1: string, arg2: boolean) => number>()
const myMockedFnTwo = vi.fn<() => void>()
const myMockedFnThree = vi.fn<any>()
})
```

## Options

```json
{
"vitest/require-hook": ["error", {
"checkImportFunctions": false
}]
}
```

The following patterns are considered bad with `checkImportFunctions: true`:

```ts
import { vi } from 'vitest'

vi.mock('./example.js', async () => {
const originalModule = await vi.importActual('./example.js')

return { ...originalModule }
})
```

```ts
const fs = await vi.importMock('fs')
```
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@
"@stylistic/eslint-plugin": "^2.13.0",
"@types/eslint": "^9.6.1",
"@types/mocha": "^10.0.10",
"@types/node": "^22.13.0",
"@typescript-eslint/eslint-plugin": "8.22.0",
"@typescript-eslint/parser": "8.22.0",
"@typescript-eslint/rule-tester": "8.22.0",
"@vitest/eslint-plugin": "^1.1.25",
"@types/node": "^22.13.1",
"@typescript-eslint/eslint-plugin": "8.23.0",
"@typescript-eslint/parser": "8.23.0",
"@typescript-eslint/rule-tester": "8.23.0",
"@vitest/eslint-plugin": "^1.1.26",
"bumpp": "^9.11.1",
"concurrently": "^9.1.2",
"eslint": "^9.19.0",
"eslint": "^9.20.0",
"eslint-doc-generator": "^2.0.2",
"eslint-plugin-eslint-plugin": "^6.4.0",
"eslint-remote-tester": "^4.0.1",
"eslint-remote-tester-repositories": "^2.0.0",
"tsx": "^4.19.2",
"typescript": "^5.7.3",
"unbuild": "^3.3.1",
"vitest": "^3.0.4"
"vitest": "^3.0.5"
},
"peerDependencies": {
"@typescript-eslint/utils": ">= 8.0",
Expand Down
Loading

0 comments on commit 5674c25

Please # to comment.