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): align mocking related typings with jest #4784

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Dec 20, 2023

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

  • user-land workaround 4723#issuecomment-1863740082
  • remove deprecated SpyInstance?
  • should vi.fn default to (...args: any[]) => any or (...args: unknown[]) => unknown?
  • more type tests
  • doc / migration guide

references

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented Dec 20, 2023

Deploy Preview for fastidious-cascaron-4ded94 canceled.

Name Link
🔨 Latest commit 7b59e10
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/66275fc6f2c2e800085d1d6c

@hi-ogawa hi-ogawa changed the title fix!(spy): align vi.fn and vi.Mock typings to jest fix!(spy): align mocking related typings to jest Dec 20, 2023
@hi-ogawa hi-ogawa changed the title fix!(spy): align mocking related typings to jest fix!(spy): align mocking related typings with jest Dec 20, 2023
@sheremet-va
Copy link
Member

sheremet-va commented Dec 28, 2023

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()

@sheremet-va sheremet-va added this to the 2.0.0 milestone Feb 15, 2024
@hi-ogawa
Copy link
Contributor Author

SpyInstance is deprecated, so can we remove it in v2 with this change?

/**
* @deprecated Use MockInstance<A, R> instead
*/
export interface SpyInstance<TArgs extends any[] = any[], TReturns = any> extends MockInstance<(...args: TArgs) => TReturns> {}

@hi-ogawa hi-ogawa marked this pull request as ready for review April 23, 2024 07:24
@sheremet-va
Copy link
Member

SpyInstance is deprecated, so can we remove it in v2 with this change?

Sure, let's remove it.

Comment on lines +111 to +118
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)
*/
Copy link
Contributor Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Property 'mockClear' does not exist on type 'Mocked<...>'.
2 participants