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: return types for vi.mocked are now equal to MaybeMocked #1511

Merged
merged 2 commits into from Jun 21, 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
10 changes: 2 additions & 8 deletions packages/vitest/src/integrations/spy.ts
Expand Up @@ -67,10 +67,10 @@ export type MaybeMockedConstructor<T> = T extends new (
) => infer R
? SpyInstanceFn<ConstructorParameters<T>, R>
: T
export type MockedFunction<T extends Procedure> = MockWithArgs<T> & {
export type MockedFunction<T extends Procedure> = SpyInstanceFn<Parameters<T>, ReturnType<T>> & {
[K in keyof T]: T[K];
}
export type MockedFunctionDeep<T extends Procedure> = MockWithArgs<T> & MockedObjectDeep<T>
export type MockedFunctionDeep<T extends Procedure> = SpyInstanceFn<Parameters<T>, ReturnType<T>> & MockedObjectDeep<T>
export type MockedObject<T> = MaybeMockedConstructor<T> & {
[K in Methods<T>]: T[K] extends Procedure
? MockedFunction<T[K]>
Expand All @@ -96,12 +96,6 @@ export type MaybeMocked<T> = T extends Procedure

export type EnhancedSpy<TArgs extends any[] = any[], TReturns = any> = SpyInstance<TArgs, TReturns> & SpyImpl<TArgs, TReturns>

export interface MockWithArgs<T extends Procedure>
extends SpyInstanceFn<Parameters<T>, ReturnType<T>> {
new (...args: T extends new (...args: any) => any ? ConstructorParameters<T> : never): T
(...args: Parameters<T>): ReturnType<T>
}

export const spies = new Set<SpyInstance>()

export function isMockFunction(fn: any): fn is EnhancedSpy {
Expand Down
11 changes: 11 additions & 0 deletions test/core/test/vi.spec.ts
Expand Up @@ -2,8 +2,11 @@
* @vitest-environment jsdom
*/

import type { MockedFunction, MockedObject } from 'vitest'
import { describe, expect, test, vi } from 'vitest'

const expectType = <T>(obj: T) => obj

describe('testing vi utils', () => {
test('global scope has variable', () => {
const IntersectionObserverMock = vi.fn()
Expand All @@ -29,6 +32,14 @@ describe('testing vi utils', () => {
expect(v1).toBe(v2)
})

test('vi mocked', () => {
expectType<MockedObject<{ bar: () => boolean }>>({
bar: vi.fn(() => true),
})
expectType<MockedFunction<() => boolean>>(vi.fn(() => true))
expectType<MockedFunction<() => boolean>>(vi.fn())
})

// TODO: it's unstable in CI, skip until resolved
test.skip('loads unloaded module', async () => {
let mod: any
Expand Down