{"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":1159,"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":1141,"sourceCodeEnd":1177,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/expect/src/jest-expect.ts#L1141-L1177","documentation":"TypeError thrown by the 'rejects' property getter when the value passed to expect() (or the function's return value, for jest-compat function wrapping) is not thenable. .rejects requires a real Promise-like target; the wrapper is computed at line 1150 as typeof obj === 'function' ? obj() : obj.","triggerScenarios":"expect(42).rejects; expect('foo').rejects.toThrow(); expect(() => 5).rejects; expect(undefined).rejects. Triggered when typeof wrapper?.then !== 'function' at line 1158.","commonSituations":"Forgetting to call the function that returns a promise (expect(getPromise).rejects instead of expect(getPromise()).rejects), or asserting .rejects on a synchronous value.","solutions":["Ensure expect() receives a thenable: expect(promise).rejects or expect(asyncFn()).rejects.","If the value is sync, drop .rejects and assert directly.","If you passed a function reference, call it: expect(fn()).rejects."],"exampleFix":"// before\nexpect(fetchUser).rejects.toThrow();\n// after\nexpect(fetchUser()).rejects.toThrow();","handlingStrategy":"type-guard","validationCode":"const wrapper = typeof obj === 'function' ? obj() : obj;\nif (typeof wrapper?.then !== 'function') throw new TypeError(`expect() target is ${typeof wrapper}, not a Promise — drop .rejects`);","typeGuard":"const isThenable = (v: unknown): v is PromiseLike<unknown> =>\n  v != null && typeof (v as any).then === 'function';","tryCatchPattern":null,"preventionTips":["Call the function so it returns its promise: expect(fn()).rejects.","For sync values, assert directly without .rejects."],"tags":["typeerror","rejects","promise","type-mismatch","async"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}