From b664d42c5c658cf05a11a7f57c07d3564c6582d8 Mon Sep 17 00:00:00 2001 From: Clemens Pflaum <32223782+ghry5@users.noreply.github.com> Date: Mon, 29 May 2023 16:04:05 +0200 Subject: [PATCH] fix(types): fix `PartialMock` with async TReturns (#3462) --- packages/spy/src/index.ts | 2 +- test/core/test/vi.spec.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/spy/src/index.ts b/packages/spy/src/index.ts index d077893df3ea..54af57cfec66 100644 --- a/packages/spy/src/index.ts +++ b/packages/spy/src/index.ts @@ -62,7 +62,7 @@ export interface Mock extends SpyInst new (...args: TArgs): TReturns (...args: TArgs): TReturns } -export interface PartialMock extends SpyInstance> { +export interface PartialMock extends SpyInstance> ? Promise>> : Partial> { new (...args: TArgs): TReturns (...args: TArgs): TReturns } diff --git a/test/core/test/vi.spec.ts b/test/core/test/vi.spec.ts index 5b038da725e6..d938c7b32f9c 100644 --- a/test/core/test/vi.spec.ts +++ b/test/core/test/vi.spec.ts @@ -65,6 +65,22 @@ describe('testing vi utils', () => { vi.mocked(mockFactory, { partial: true, deep: true }).mockReturnValue({ baz: 'baz', }) + + type FooBarAsyncFactory = () => Promise + + const mockFactoryAsync: FooBarAsyncFactory = vi.fn() + + vi.mocked(mockFactoryAsync, { partial: true }).mockResolvedValue({ + foo: vi.fn(), + }) + + vi.mocked(mockFactoryAsync, { partial: true, deep: false }).mockResolvedValue({ + bar: vi.fn(), + }) + + vi.mocked(mockFactoryAsync, { partial: true, deep: true }).mockResolvedValue({ + baz: 'baz', + }) }) test('can change config', () => {