Skip to content

jest-circus crashes with TypeError when test throws a plain object and asyncError is undefined #15996

Closed

Description

@alex-all3dp

Jest version: 30.3.0

What happens

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.

Location 1: packages/jest-circus/src/formatNodeAssertErrors.ts

} else {
  error = asyncError;      // asyncError may be undefined
  error.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.

Location 2: packages/jest-circus/src/utils.ts (_getError)

// asyncError = errors[1], which may be undefined when errors is an array
if (error && (typeof error.stack === 'string' || error.message)) {
  return error;
}
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 RpcException
  const obs = new Observable(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:

  1. formatNodeAssertErrors sees no .stack on originalError, assigns error = asyncError (undefined), then crashes on error.message = ...
  2. _getError hits the same crash on line 436

The test runner aborts with an internal error instead of reporting a clean test failure.

Fix

formatNodeAssertErrors.ts:

} else if (asyncError) {
  error = asyncError;
  error.message = originalError.message || `thrown: ${prettyFormat(originalError, {maxDepth: 3})}`;
} else {
  error = new Error(originalError.message || `thrown: ${prettyFormat(originalError, {maxDepth: 3})}`);
}

utils.ts (_getError):

if (asyncError) {
  asyncError.message = `thrown: ${prettyFormat(error, {maxDepth: 3})}`;
  return asyncError;
}
return new Error(`thrown: ${prettyFormat(error, {maxDepth: 3})}`);

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.

Activity

  1. ashoknarayan commented on Mar 17, 2026

    @ashoknarayan
    Contributor

    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.

  2. alex-all3dp commented on Mar 18, 2026

    @alex-all3dp
    Author

    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 :)

  3. ashoknarayan commented on Mar 19, 2026

    @ashoknarayan
    Contributor

    Hi @alex-all3dp, I’ve opened a PR (#16003) addressing this issue.

    I’ve added checks to handle cases where asyncError is undefined and fall back to creating a new Error, 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!

  4. added 2 commits that reference this issue on Mar 26, 2026
    c7b7080
    c95b433
  5. github-actions commented on Apr 19, 2026

    @github-actions

    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.

  6. alex-all3dp commented on Apr 20, 2026

    @alex-all3dp
    Author

    Still being worked on

  7. SimenB commented on May 7, 2026

    @SimenB
    Member
  8. github-actions commented on Jun 22, 2026

    @github-actions

    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.

  9. locked as resolved and limited conversation to collaborators on Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions