Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!(spy): simplify mock function generic types and align with jest #4784

Merged
merged 34 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e2d6312
fix!(spy): align vi.fn and vi.Mock typings to jest
hi-ogawa Dec 20, 2023
c5781fe
fix: put back `any` for starter
hi-ogawa Dec 20, 2023
8a13a22
chore: lint
hi-ogawa Dec 20, 2023
a87d320
chore: back to "unknown"
hi-ogawa Dec 20, 2023
a0e8876
Merge branch 'main' into feat-breaking-fn-mock-typing
hi-ogawa Jan 3, 2024
f79f1c8
chore: revert lock
hi-ogawa Jan 3, 2024
5efee95
chore: more any type
hi-ogawa Jan 3, 2024
ba54044
Merge branch 'main' into feat-breaking-fn-mock-typing
hi-ogawa Feb 8, 2024
c17bac0
Merge branch 'main' into feat-breaking-fn-mock-typing
hi-ogawa Apr 23, 2024
4f50db5
Merge branch 'main' into feat-breaking-fn-mock-typing
hi-ogawa Apr 23, 2024
a3bdb7a
fix: fix types
hi-ogawa Apr 23, 2024
e034a05
test: fix and add
hi-ogawa Apr 23, 2024
eb1c442
fix: give up "ts/method-signature-style"
hi-ogawa Apr 23, 2024
d0903b6
chore: comment
hi-ogawa Apr 23, 2024
fef7e5f
chore: cleanup
hi-ogawa Apr 23, 2024
cb777f1
chore: cleanup
hi-ogawa Apr 23, 2024
2363d57
Merge branch 'main' into feat-breaking-fn-mock-typing
hi-ogawa Apr 23, 2024
7b59e10
feat: replace `any` with `unknown` for default types
hi-ogawa Apr 23, 2024
74984d7
Merge branch 'main' into feat-breaking-fn-mock-typing
hi-ogawa Jun 20, 2024
67b1887
fix: fix types
hi-ogawa Jun 20, 2024
8ff1526
chore: fix format 1
hi-ogawa Jun 20, 2024
3527342
chore: fix format 2
hi-ogawa Jun 20, 2024
61f6ecc
chore: fix format 3
hi-ogawa Jun 20, 2024
0d47f57
docs: jest migration
hi-ogawa Jun 20, 2024
5e8b508
docs: v2 migration
hi-ogawa Jun 20, 2024
1a7a3c8
docs: tweak
hi-ogawa Jun 20, 2024
140c25d
fix: more any types
hi-ogawa Jun 20, 2024
532684b
docs: also call out `any` to `unknown`
hi-ogawa Jun 20, 2024
bfa8ebf
Merge branch 'main' into feat-breaking-fn-mock-typing
hi-ogawa Jun 20, 2024
e5c85c1
chore: revert default UknownProcedure to AnyProcedure
hi-ogawa Jun 21, 2024
b417c3e
Merge branch 'main' into feat-breaking-fn-mock-typing
hi-ogawa Jun 21, 2024
0fde92a
Merge remote-tracking branch 'origin/feat-breaking-fn-mock-typing' in…
hi-ogawa Jun 21, 2024
72adccf
chore: more any
hi-ogawa Jun 21, 2024
1946007
Merge remote-tracking branch 'origin/feat-breaking-fn-mock-typing' in…
hi-ogawa Jun 21, 2024
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
24 changes: 20 additions & 4 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ This makes `.suite` optional; if the task is defined at the top level, it will n

This change also removes the file from `expect.getState().currentTestName` and makes `expect.getState().testPath` required.

### Simplified generic types of mock functions (e.g. `vi.fn<T>`, `Mock<T>`)

Previously `vi.fn<TArgs, TReturn>` accepted two generic types separately for arguemnts and return value. This is changed to directly accept a function type `vi.fn<T>` to simplify the usage.

```ts
import { type Mock, vi } from 'vitest'

const add = (x: number, y: number): number => x + y

// using vi.fn<T>
const mockAdd = vi.fn<Parameters<typeof add>, ReturnType<typeof add>>() // [!code --]
const mockAdd = vi.fn<typeof add>() // [!code ++]

// using Mock<T>
const mockAdd: Mock<Parameters<typeof add>, ReturnType<typeof add>> = vi.fn() // [!code --]
const mockAdd: Mock<typeof add> = vi.fn() // [!code ++]
```

## Migrating to Vitest 1.0

<!-- introduction -->
Expand Down Expand Up @@ -324,13 +342,11 @@ export default defineConfig({
Vitest doesn't have an equivalent to `jest` namespace, so you will need to import types directly from `vitest`:

```ts
let fn: jest.Mock<string, [string]> // [!code --]
let fn: jest.Mock<(name: string) => number> // [!code --]
import type { Mock } from 'vitest' // [!code ++]
let fn: Mock<[string], string> // [!code ++]
let fn: Mock<(name: string) => number> // [!code ++]
```

Also, Vitest has `Args` type as a first argument instead of `Returns`, as you can see in diff.

### Timers

Vitest doesn't support Jest's legacy timers.
Expand Down
Loading
Loading