Skip to content

expect().resolves does not accept a function #10281

@IIIMADDINIII

Description

@IIIMADDINIII

Describe the bug

Mostly a the same as #6643 but now Jest supports functions with resolves: jestjs/jest#15713

If you call await expect(async () => { ... }).rejects.toThrow(), this will work as expected. However, change this to await expect(async () => { ... }).resolves.not.toThrow() and the test fails with the following error:

TypeError: You must provide a Promise to expect() when using .resolves, not 'function'.

const obj = utils.flag(this, 'object')
const wrapper = typeof obj === 'function' ? obj() : obj // for jest compat
if (utils.flag(this, 'poll')) {
throw new SyntaxError(
`expect.poll() is not supported in combination with .rejects`,
)
}
if (typeof wrapper?.then !== 'function') {
throw new TypeError(
`You must provide a Promise to expect() when using .rejects, not '${typeof wrapper}'.`,
)
}

const obj = utils.flag(this, 'object')
if (utils.flag(this, 'poll')) {
throw new SyntaxError(
`expect.poll() is not supported in combination with .resolves`,
)
}
if (typeof obj?.then !== 'function') {
throw new TypeError(
`You must provide a Promise to expect() when using .resolves, not '${typeof obj}'.`,
)
}

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-9omsvx?file=test%2Fbasic.test.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 22.22.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: 4.1.5 => 4.1.5 
    vite: 8.0.10 => 8.0.10 
    vitest: 4.1.5 => 4.1.5

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions