diff --git a/packages/vitest/src/integrations/jest-mock.ts b/packages/vitest/src/integrations/jest-mock.ts index 549116294eb8..87dda587f1ad 100644 --- a/packages/vitest/src/integrations/jest-mock.ts +++ b/packages/vitest/src/integrations/jest-mock.ts @@ -117,17 +117,10 @@ export function spyOn>>( methodName: G, accesType: 'set', ): SpyInstance<[T[G]], void> -export function spyOn>>( - object: T, - method: M -): Required[M] extends new (...args: infer A) => infer R - ? SpyInstance - : never -export function spyOn>>( +export function spyOn> | Classes>)>( obj: T, methodName: M, - mock?: T[M] -): Required[M] extends (...args: infer A) => infer R ? SpyInstance : never +): Required[M] extends (...args: infer A) => infer R | (new (...args: infer A) => infer R) ? SpyInstance : never export function spyOn( obj: T, method: K, diff --git a/test/core/test/spy.test.ts b/test/core/test/spy.test.ts new file mode 100644 index 000000000000..d191ae27d0cd --- /dev/null +++ b/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') + }) +})