You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
jest-circus crashes with TypeError when test throws a plain object and asyncError is undefined #15996
Two code paths in jest-circus crash with TypeError: Cannot set properties of undefined (setting 'message') when a test throws a plain object (not an Error instance) and the captured asyncError is undefined.
}else{error=asyncError;// asyncError may be undefinederror.message= ...;// CRASH}
When originalError has no .stack (e.g. { status: 403, message: 'Forbidden' }), the else branch runs. If asyncError is undefined, writing error.message throws.
// asyncError = errors[1], which may be undefined when errors is an arrayif(error&&(typeoferror.stack==='string'||error.message)){returnerror;}asyncError.message=`thrown: ${prettyFormat(error,{maxDepth: 3})}`;// CRASH
Same crash when errors is a tuple and errors[1] is undefined.
How to reproduce
test('rpc error via done callback',done=>{const{ Observable }=require('rxjs');// subscriber.error() with a plain object (not new Error()) -- common with NestJS RpcExceptionconstobs=newObservable(sub=>sub.error({status: 403,message: 'Forbidden'}));// no error handler -- unhandled error propagates into jest-circus as [plainObject, undefined]obs.subscribe(()=>done());});
jest-circus records the error as [{ status: 403, message: 'Forbidden' }, undefined]. During test_done:
formatNodeAssertErrors sees no .stack on originalError, assigns error = asyncError (undefined), then crashes on error.message = ...
_getError hits the same crash on line 436
The test runner aborts with an internal error instead of reporting a clean test failure.
Both fixes fall back to constructing a new Error when asyncError is undefined.
Notes
Circus.TestError types the tuple second element as Exception (non-optional), but at runtime it can be undefined, so there is also a type/runtime mismatch to fix there.
Hi, I’d like to work on this issue. I can reproduce the crash and plan to handle cases where asyncError is undefined by falling back to a new Error, along with adding a test to cover this scenario. Please let me know if that approach sounds good.
Hi, I’d like to work on this issue. I can reproduce the crash and plan to handle cases where asyncError is undefined by falling back to a new Error, along with adding a test to cover this scenario. Please let me know if that approach sounds good.
@ashoknarayan Thank you for working on this! Your solution aligns with my proposed fix, if I didn't misread. Looking forward to the patched version :)
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
Activity
ashoknarayan commentedon Mar 17, 2026
Hi, I’d like to work on this issue. I can reproduce the crash and plan to handle cases where
asyncErroris undefined by falling back to a newError, along with adding a test to cover this scenario. Please let me know if that approach sounds good.alex-all3dp commentedon Mar 18, 2026
@ashoknarayan Thank you for working on this! Your solution aligns with my proposed fix, if I didn't misread. Looking forward to the patched version :)
ashoknarayan commentedon Mar 19, 2026
Hi @alex-all3dp, I’ve opened a PR (#16003) addressing this issue.
I’ve added checks to handle cases where
asyncErroris undefined and fall back to creating a newError, along with a small test to cover this scenario.Would really appreciate it if you could take a look when you get a chance. Thanks!
fix(jest-circus): handle undefined asyncError when test throws plain …
fix(jest-circus): handle undefined asyncError when test throws plain …
github-actions commentedon Apr 19, 2026
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
alex-all3dp commentedon Apr 20, 2026
Still being worked on
SimenB commentedon May 7, 2026
https://github.com/jestjs/jest/releases/tag/v30.4.0
github-actions commentedon Jun 22, 2026
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.