Skip to content

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

Merged
sheremet-va merged 34 commits intovitest-dev:mainfrom
hi-ogawa:feat-breaking-fn-mock-typing
Jun 23, 2024
Merged

fix!(spy): simplify mock function generic types and align with jest#4784
sheremet-va merged 34 commits intovitest-dev:mainfrom
hi-ogawa:feat-breaking-fn-mock-typing

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:.

@netlify
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.

@sheremet-va sheremet-va removed this from the 2.0.0 milestone Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

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

2 participants