-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added the
require-mock-type-parameters
rule (#651)
* 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
1 parent
d9be59d
commit 5674c25
Showing
7 changed files
with
361 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.