diff --git a/packages/vitest/src/integrations/spy.ts b/packages/vitest/src/integrations/spy.ts index 076d70e649bb..99191f66c230 100644 --- a/packages/vitest/src/integrations/spy.ts +++ b/packages/vitest/src/integrations/spy.ts @@ -67,10 +67,10 @@ export type MaybeMockedConstructor = T extends new ( ) => infer R ? SpyInstanceFn, R> : T -export type MockedFunction = MockWithArgs & { +export type MockedFunction = SpyInstanceFn, ReturnType> & { [K in keyof T]: T[K]; } -export type MockedFunctionDeep = MockWithArgs & MockedObjectDeep +export type MockedFunctionDeep = SpyInstanceFn, ReturnType> & MockedObjectDeep export type MockedObject = MaybeMockedConstructor & { [K in Methods]: T[K] extends Procedure ? MockedFunction @@ -96,12 +96,6 @@ export type MaybeMocked = T extends Procedure export type EnhancedSpy = SpyInstance & SpyImpl -export interface MockWithArgs - extends SpyInstanceFn, ReturnType> { - new (...args: T extends new (...args: any) => any ? ConstructorParameters : never): T - (...args: Parameters): ReturnType -} - export const spies = new Set() export function isMockFunction(fn: any): fn is EnhancedSpy { diff --git a/test/core/test/vi.spec.ts b/test/core/test/vi.spec.ts index ddd84545b166..63e03e540aa1 100644 --- a/test/core/test/vi.spec.ts +++ b/test/core/test/vi.spec.ts @@ -2,8 +2,11 @@ * @vitest-environment jsdom */ +import type { MockedFunction, MockedObject } from 'vitest' import { describe, expect, test, vi } from 'vitest' +const expectType = (obj: T) => obj + describe('testing vi utils', () => { test('global scope has variable', () => { const IntersectionObserverMock = vi.fn() @@ -29,6 +32,14 @@ describe('testing vi utils', () => { expect(v1).toBe(v2) }) + test('vi mocked', () => { + expectType boolean }>>({ + bar: vi.fn(() => true), + }) + expectType boolean>>(vi.fn(() => true)) + expectType boolean>>(vi.fn()) + }) + // TODO: it's unstable in CI, skip until resolved test.skip('loads unloaded module', async () => { let mod: any