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): align mocking related typings with jest #4784
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for fastidious-cascaron-4ded94 canceled.
|
I don't think we should change base types yet. Instead, I propose defining a namespace with new mock types like vite does with Rollup namespace (naming can be discussed): // mock-types.ts
export type MockContext<T extends Procedure> = {}
// spy.ts
import type * as Mocks from './mock-types.ts'
export type { Mocks }
// test.ts
import type { Mocks } from 'vitest'
const fn: Mocks.Spy<() => void> = vi.fn() |
vitest/packages/spy/src/index.ts Lines 111 to 114 in c17bac0
|
Sure, let's remove it. |
Typescript assignability is different between | ||
{ foo: (f: T) => U } (this is "method-signature-style") | ||
and | ||
{ foo(f: T): U } | ||
|
||
Jest uses the latter for `MockInstance.mockImplementation` etc... and it allows assignment such as: | ||
const boolFn: Jest.Mock<() => boolean> = jest.fn<() => true>(() => true) | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the latter one to follow Jest for now, but I think going with the former should be totally fine too.
Jest one is technically type unsafe and the assignment example I wrote here can be easily rewritten by:
jest.fn<() => boolean>(() => true)
jest.fn(() => true as boolean)
Or in a more realistic scenario, it would probably look like:
// setup
type MyFn = () => boolean;
let myMockFn: Mock<MyFn>
// test
myMockFn = vi.fn<MyFn>(() => true)
Description
Closes #4723
I started with a mostly straight-forward typing update by following jestjs/jest#12489. I will consider the implication of this change in details later.
todo
SpyInstance
?vi.fn
default to(...args: any[]) => any
or(...args: unknown[]) => unknown
?references
jest.fn
generic type arguments jestjs/jest#12489Mocked*
utility types jestjs/jest#13123jest.mocked
helper’s behaviour to deep mocked jestjs/jest#13125jest.Spied*
utility types jestjs/jest#13440Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yaml
unless you introduce a new test example.Tests
pnpm test:ci
.Documentation
pnpm run docs
command.Changesets
feat:
,fix:
,perf:
,docs:
, orchore:
.