{"record":{"id":"f66b621dfa1028fa","repo":"awslabs/llrt","slug":"you-must-provide-a-promise-to-expect-when-using-rejects-not","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":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"llrt_core/src/modules/js/@llrt/expect/jest-expect.ts","lineNumber":582,"sourceCode":"      });\n\n      return proxy;\n    }\n  );\n\n  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: any = 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 (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      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          return async (...args: any[]) => {\n            const promise = wrapper.then(\n              (value: any) => {\n                const _error = new AssertionError(\n                  `promise resolved \"${utils.inspect(value)}\" instead of rejecting`,\n                  {\n                    showDiff: true,\n                    expected: new Error(\"rejected promise\"),","sourceCodeStart":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/awslabs/llrt/blob/742fc00b82cbeaab1c1b76f0d706c302a5cbc306/llrt_core/src/modules/js/@llrt/expect/jest-expect.ts#L564-L600","documentation":"This TypeError is thrown by JestChaiExpect when the `.rejects` modifier is used but the (possibly invoked) value given to expect() is not a Promise. For Jest compatibility, if the object is a function it is first called (`const wrapper = typeof obj === 'function' ? obj() : obj`), then checked for a `then` method. If neither the function's return value nor the object is thenable, the error is raised.","triggerScenarios":"Calling expect(fnOrValue).rejects.toThrow(...) where fnOrValue is a plain object/value, or a function that returns a non-Promise (e.g. a sync function).","commonSituations":"Passing a synchronous function to .rejects expecting it to be wrapped like Jest's expect(fn).rejects, or the async function was changed to sync so calling it returns undefined.","solutions":["Pass a function that returns a rejected promise or the promise itself: expect(asyncFn()).rejects.toThrow(E).","Make sure the function under test is async or returns a Promise.","If the value never rejects, drop .rejects and use .toThrow on a wrapping () => { ... } function instead."],"exampleFix":"// before\nexpect(() => syncFn()).rejects.toThrow('boom'); // syncFn returns non-promise\n// after\nexpect(asyncFn()).rejects.toThrow('boom');","handlingStrategy":"type-guard","validationCode":"const wrapper = typeof fnOrValue === 'function' ? fnOrValue() : fnOrValue;\nif (!wrapper || typeof wrapper.then !== 'function') throw new TypeError('.rejects requires a Promise');","typeGuard":"function isPromiseLike(v) {\n  return v !== null && typeof v.then === 'function';\n}","tryCatchPattern":null,"preventionTips":["Pass a function that CALLS an async function, or the promise itself — not a sync function.","Verify the API under test can actually reject before using .rejects.","Prefer expect(asyncFn()).rejects.toThrow(E) over passing raw values."],"tags":["expect","promise","testing","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"742fc00b82cbeaab1c1b76f0d706c302a5cbc306","analyzedAt":"2026-09-12T11:14:07.838Z","contentChangedAt":"2026-09-12T11:14:07.838Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}