Skip to content

Commit

Permalink
fix: update spy type inference (#643)
Browse files Browse the repository at this point in the history
* fix: update spy type inference

* test: add test case for `spyOn` type inference

* refactor: merge overload declarations

* chore: remove unused parameter

* test: use `happy-dom`

* test: skip flaky test

Co-authored-by: Ivan Demchuk <ivan.demchuk@gmail.com>
  • Loading branch information
DerYeger and Demivan committed Feb 1, 2022
1 parent d55736d commit 691bede
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
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')
})
})

0 comments on commit 691bede

Please sign in to comment.