Skip to content

Still required to Promise.resolve in vi.when().thenResolve #11017

Description

@JetUni

Describe the bug

I am using the new vi.when syntax in the v5 rc.2 vitest package and noticed that when using the .thenResolves function it requires me to include Promise.resolve(...) wrapping my return value. I would expect that this function would not require that vs the .thenReturn which would. The reproduction below gives a build error, but I would only expect it to when using .thenReturn as a direct replacement in the repro code

Reproduction

import { Mock, expect } from 'vitest';

async function foo(input: string) {
return 'bar';
}

describe('vi.when.thenResolves reproduction', () => {
let fooStub: Mock;

beforeAll(() => {
fooStub = vi.mocked(foo);
});
it('should resolve a string without Promise.resolve wrapping it', async () => {
vi.when(fooStub).calledWith('easter-egg').thenResolve('bar');

const result = await foo('easter-egg');

expect(result).toBe('bar');

});
});

System Info

System:
    OS: macOS 26.6
    CPU: (11) arm64 Apple M3 Pro
    Memory: 69.45 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.15.0 - /Users/sorenja/.nvm/versions/node/v24.15.0/bin/node
    Yarn: 1.22.22 - /Users/sorenja/.nvm/versions/node/v24.15.0/bin/yarn
    npm: 11.12.1 - /Users/sorenja/.nvm/versions/node/v24.15.0/bin/npm
    pnpm: 11.9.0 - /Users/sorenja/Library/pnpm/bin/pnpm
  Browsers:
    Safari: 26.6
  npmPackages:
    @vitest/coverage-v8: 5.0.0-rc.2 => 5.0.0-rc.2 
    vitest: 5.0.0-rc.2 => 5.0.0-rc.2

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

Labels

p3-minor-bugAn edge case that only affects very specific usage (priority)

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions