diff --git a/packages/vitest/src/integrations/mock/when.ts b/packages/vitest/src/integrations/mock/when.ts index d1a505da7dc7..1c06e9720990 100644 --- a/packages/vitest/src/integrations/mock/when.ts +++ b/packages/vitest/src/integrations/mock/when.ts @@ -85,7 +85,7 @@ type CalledWithInstance = When & { * @param options - Optional behavior configuration. * @returns The same {@linkcode when|vi.when} instance for chaining. */ - thenResolve: (value: ReturnType, options?: BehaviorOptions | undefined) => CalledWithInstance + thenResolve: (value: Awaited, options?: BehaviorOptions | undefined) => CalledWithInstance /** * Schedules a synchronous return value for a single call with the registered arguments, then removes the behavior. @@ -103,7 +103,7 @@ type CalledWithInstance = When & { * @param options - Optional behavior configuration. * @returns The same {@linkcode when|vi.when} instance for chaining. */ - thenResolveOnce: (value: ReturnType, options?: OnceBehaviorOptions | undefined) => CalledWithInstance + thenResolveOnce: (value: Awaited, options?: OnceBehaviorOptions | undefined) => CalledWithInstance /** * Schedules a thrown error for when the spy is called with the registered arguments. diff --git a/test/unit/test/mocking/vi-when.test.ts b/test/unit/test/mocking/vi-when.test.ts index a7cf24a3df38..1c430810f632 100644 --- a/test/unit/test/mocking/vi-when.test.ts +++ b/test/unit/test/mocking/vi-when.test.ts @@ -136,6 +136,25 @@ describe('vi.when()', () => { expect(w).toHaveBeenExhausted() }) + test('resolves an unwrapped value for an async mock using `thenResolve`', async () => { + const spy = vi.fn<(input: string) => Promise>() + + const w = vi.when(spy) + .calledWith('easter-egg') + .thenResolve('bar') + + expect(w).not.toHaveBeenExhausted() + + await expect(spy('easter-egg')).resolves.toBe('bar') + + expect(spy).toHaveBeenLastCalledWith('easter-egg') + expect(spy).toHaveLastResolvedWith('bar') + + expect(spy).toHaveBeenCalledOnce() + + expect(w).toHaveBeenExhausted() + }) + test('rejects a promise when using `toReject`', async () => { const spy = vi.fn()