Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/vitest/src/integrations/mock/when.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type CalledWithInstance<ReturnType, Fn extends Procedure> = When<Fn> & {
* @param options - Optional behavior configuration.
* @returns The same {@linkcode when|vi.when} instance for chaining.
*/
thenResolve: (value: ReturnType, options?: BehaviorOptions | undefined) => CalledWithInstance<ReturnType, Fn>
thenResolve: (value: Awaited<ReturnType>, options?: BehaviorOptions | undefined) => CalledWithInstance<ReturnType, Fn>

/**
* Schedules a synchronous return value for a single call with the registered arguments, then removes the behavior.
Expand All @@ -103,7 +103,7 @@ type CalledWithInstance<ReturnType, Fn extends Procedure> = When<Fn> & {
* @param options - Optional behavior configuration.
* @returns The same {@linkcode when|vi.when} instance for chaining.
*/
thenResolveOnce: (value: ReturnType, options?: OnceBehaviorOptions | undefined) => CalledWithInstance<ReturnType, Fn>
thenResolveOnce: (value: Awaited<ReturnType>, options?: OnceBehaviorOptions | undefined) => CalledWithInstance<ReturnType, Fn>

/**
* Schedules a thrown error for when the spy is called with the registered arguments.
Expand Down
19 changes: 19 additions & 0 deletions test/unit/test/mocking/vi-when.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>>()

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<Fn>()

Expand Down
Loading