{"record":{"id":"c7cb813cfb25e20a","repo":"vitest-dev/vitest","slug":"you-must-provide-a-promise-to-expect-when-using-c7cb81","errorCode":null,"errorMessage":"You must provide a Promise to expect() when using .rejects, not '${typeof wrapper}'.","messagePattern":"You must provide a Promise to expect\\(\\) when using \\.rejects, not '(.+?)'\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/jest-expect.ts","lineNumber":1164,"sourceCode":"  utils.addProperty(\n    chai.Assertion.prototype,\n    'rejects',\n    function __VITEST_REJECTS__(this: any) {\n      const error = new Error('rejects')\n      utils.flag(this, 'promise', 'rejects')\n      utils.flag(this, 'error', error)\n      const test: Test = utils.flag(this, 'vitest-test')\n      const obj = utils.flag(this, 'object')\n      const wrapper = typeof obj === 'function' ? obj() : obj // for jest compat\n\n      if (utils.flag(this, 'poll')) {\n        throw new SyntaxError(\n          `expect.poll() is not supported in combination with .rejects`,\n        )\n      }\n\n      if (typeof wrapper?.then !== 'function') {\n        throw new TypeError(\n          `You must provide a Promise to expect() when using .rejects, not '${typeof wrapper}'.`,\n        )\n      }\n\n      const proxy: any = new Proxy(this, {\n        get: (target, key, receiver) => {\n          const result = Reflect.get(target, key, receiver)\n\n          if (typeof result !== 'function') {\n            return result instanceof chai.Assertion ? proxy : result\n          }\n\n          return (...args: any[]) => {\n            utils.flag(this, '_name', key)\n            const promise = Promise.resolve(wrapper).then(\n              (value: any) => {\n                const _error = new AssertionError(\n                  `promise resolved \"${utils.inspect(","sourceCodeStart":1146,"sourceCodeEnd":1182,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/expect/src/jest-expect.ts#L1146-L1182","documentation":"`.rejects` needs a thenable (a Promise). The code wraps `obj` (calling it if it is a function, for Jest compat) and checks `wrapper.then`; if it is not a function, the assertion cannot observe a rejection and throws a `TypeError`.","triggerScenarios":"`expect(42).rejects...`, `expect(() => 42).rejects...`, or passing a non-promise to a `.rejects` assertion; the producer returned a value instead of rejecting.","commonSituations":"Function returns a value in the happy path when the test expected a throw; forgot to `throw` inside an async function so it resolved instead of rejected; passing a sync result to `.rejects`.","solutions":["Ensure the subject returns a rejected Promise (`Promise.reject(err)` or `throw` inside an async function).","If the value is not a promise, assert on it directly without `.rejects`.","Use `await` correctly so the rejection is observable."],"exampleFix":"// before\nasync function fail() { return 'oops' }\nexpect(fail()).rejects.toThrow()\n\n// after\nasync function fail() { throw new Error('oops') }\nexpect(fail()).rejects.toThrow('oops')","handlingStrategy":"type-guard","validationCode":"const wrapper = typeof obj === 'function' ? obj() : obj\nif (!(wrapper && typeof wrapper.then === 'function')) {\n  throw new Error('pass a Promise when using .rejects')\n}","typeGuard":"const isPromise = (v: unknown): v is Promise<unknown> =>\n  !!v && typeof (v as any).then === 'function'","tryCatchPattern":null,"preventionTips":["Ensure async functions throw or return Promise.reject for failure paths.","Confirm the subject is a Promise before using .rejects.","Avoid mixing sync returns with .rejects assertions."],"tags":["expect","rejects","promise","type-error","jest-expect"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}