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: update spy type inference #643

Merged
merged 7 commits into from Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions packages/vitest/src/integrations/jest-mock.ts
Expand Up @@ -117,17 +117,10 @@ export function spyOn<T, G extends Properties<Required<T>>>(
methodName: G,
accesType: 'set',
): SpyInstance<[T[G]], void>
export function spyOn<T, M extends Classes<Required<T>>>(
object: T,
method: M
): Required<T>[M] extends new (...args: infer A) => infer R
? SpyInstance<A, R>
: never
export function spyOn<T, M extends Methods<Required<T>>>(
export function spyOn<T, M extends (Methods<Required<T>> | Classes<Required<T>>)>(
obj: T,
methodName: M,
mock?: T[M]
): Required<T>[M] extends (...args: infer A) => infer R ? SpyInstance<A, R> : never
): Required<T>[M] extends (...args: infer A) => infer R | (new (...args: infer A) => infer R) ? SpyInstance<A, R> : never
export function spyOn<T, K extends keyof T>(
obj: T,
method: K,
Expand Down
12 changes: 12 additions & 0 deletions test/core/test/spy.test.ts
@@ -0,0 +1,12 @@
import { describe, expect, spyOn, test } from 'vitest'

/**
* @vitest-environment happy-dom
*/

describe('spyOn', () => {
test('correctly infers method types', async() => {
spyOn(localStorage, 'getItem').mockReturnValue('world')
expect(window.localStorage.getItem('hello')).toEqual('world')
})
})