Skip to content

fix(jest-circus): handle undefined asyncError when test throws plain object#16008

Open
JivinSardine wants to merge 1 commit intojestjs:mainfrom
JivinSardine:fix/handle-undefined-asyncerror
Open

fix(jest-circus): handle undefined asyncError when test throws plain object#16008
JivinSardine wants to merge 1 commit intojestjs:mainfrom
JivinSardine:fix/handle-undefined-asyncerror

Conversation

@JivinSardine
Copy link
Copy Markdown

@JivinSardine JivinSardine commented Mar 26, 2026

Summary

When a test throws a plain object (not an Error instance) and the captured asyncError is undefined, jest-circus crashes with TypeError.

Fix

This PR adds proper null checks in two locations:

  1. formatNodeAssertErrors.ts - When assigning asyncError to error, check if asyncError exists first; if not, create a new Error

  2. utils.ts (_getError) - When setting asyncError.message, check if asyncError exists first; if not, return a new Error

This allows jest-circus to properly handle cases like RxJS Observables that emit plain objects as errors (common in NestJS RpcException).

Fixes #15996

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Mar 26, 2026

CLA Not Signed

@netlify
Copy link
Copy Markdown

netlify bot commented Mar 26, 2026

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit c95b433
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/69c4ec9775a74e00094d5b2f
😎 Deploy Preview https://deploy-preview-16008--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 26, 2026

Open in StackBlitz

babel-jest

npm i https://pkg.pr.new/babel-jest@16008

babel-plugin-jest-hoist

npm i https://pkg.pr.new/babel-plugin-jest-hoist@16008

babel-preset-jest

npm i https://pkg.pr.new/babel-preset-jest@16008

create-jest

npm i https://pkg.pr.new/create-jest@16008

@jest/diff-sequences

npm i https://pkg.pr.new/@jest/diff-sequences@16008

expect

npm i https://pkg.pr.new/expect@16008

@jest/expect-utils

npm i https://pkg.pr.new/@jest/expect-utils@16008

jest

npm i https://pkg.pr.new/jest@16008

jest-changed-files

npm i https://pkg.pr.new/jest-changed-files@16008

jest-circus

npm i https://pkg.pr.new/jest-circus@16008

jest-cli

npm i https://pkg.pr.new/jest-cli@16008

jest-config

npm i https://pkg.pr.new/jest-config@16008

@jest/console

npm i https://pkg.pr.new/@jest/console@16008

@jest/core

npm i https://pkg.pr.new/@jest/core@16008

@jest/create-cache-key-function

npm i https://pkg.pr.new/@jest/create-cache-key-function@16008

jest-diff

npm i https://pkg.pr.new/jest-diff@16008

jest-docblock

npm i https://pkg.pr.new/jest-docblock@16008

jest-each

npm i https://pkg.pr.new/jest-each@16008

@jest/environment

npm i https://pkg.pr.new/@jest/environment@16008

jest-environment-jsdom

npm i https://pkg.pr.new/jest-environment-jsdom@16008

@jest/environment-jsdom-abstract

npm i https://pkg.pr.new/@jest/environment-jsdom-abstract@16008

jest-environment-node

npm i https://pkg.pr.new/jest-environment-node@16008

@jest/expect

npm i https://pkg.pr.new/@jest/expect@16008

@jest/fake-timers

npm i https://pkg.pr.new/@jest/fake-timers@16008

@jest/get-type

npm i https://pkg.pr.new/@jest/get-type@16008

@jest/globals

npm i https://pkg.pr.new/@jest/globals@16008

jest-haste-map

npm i https://pkg.pr.new/jest-haste-map@16008

jest-jasmine2

npm i https://pkg.pr.new/jest-jasmine2@16008

jest-leak-detector

npm i https://pkg.pr.new/jest-leak-detector@16008

jest-matcher-utils

npm i https://pkg.pr.new/jest-matcher-utils@16008

jest-message-util

npm i https://pkg.pr.new/jest-message-util@16008

jest-mock

npm i https://pkg.pr.new/jest-mock@16008

@jest/pattern

npm i https://pkg.pr.new/@jest/pattern@16008

jest-phabricator

npm i https://pkg.pr.new/jest-phabricator@16008

jest-regex-util

npm i https://pkg.pr.new/jest-regex-util@16008

@jest/reporters

npm i https://pkg.pr.new/@jest/reporters@16008

jest-resolve

npm i https://pkg.pr.new/jest-resolve@16008

jest-resolve-dependencies

npm i https://pkg.pr.new/jest-resolve-dependencies@16008

jest-runner

npm i https://pkg.pr.new/jest-runner@16008

jest-runtime

npm i https://pkg.pr.new/jest-runtime@16008

@jest/schemas

npm i https://pkg.pr.new/@jest/schemas@16008

jest-snapshot

npm i https://pkg.pr.new/jest-snapshot@16008

@jest/snapshot-utils

npm i https://pkg.pr.new/@jest/snapshot-utils@16008

@jest/source-map

npm i https://pkg.pr.new/@jest/source-map@16008

@jest/test-result

npm i https://pkg.pr.new/@jest/test-result@16008

@jest/test-sequencer

npm i https://pkg.pr.new/@jest/test-sequencer@16008

@jest/transform

npm i https://pkg.pr.new/@jest/transform@16008

@jest/types

npm i https://pkg.pr.new/@jest/types@16008

jest-util

npm i https://pkg.pr.new/jest-util@16008

jest-validate

npm i https://pkg.pr.new/jest-validate@16008

jest-watcher

npm i https://pkg.pr.new/jest-watcher@16008

jest-worker

npm i https://pkg.pr.new/jest-worker@16008

pretty-format

npm i https://pkg.pr.new/pretty-format@16008

commit: c95b433

…object

When a test throws a plain object (not an Error instance) and the
captured asyncError is undefined, jest-circus crashes with:
  'TypeError: Cannot set properties of undefined (setting "message")'

This fix adds proper null checks in two locations:
1. formatNodeAssertErrors.ts - When assigning asyncError to error, check
   if asyncError exists first; if not, create a new Error
2. utils.ts (_getError) - When setting asyncError.message, check if
   asyncError exists first; if not, return a new Error

This allows jest-circus to properly handle cases like RxJS Observables
that emit plain objects as errors (common in NestJS RpcException).

Fixes jestjs#15996
@JivinSardine JivinSardine force-pushed the fix/handle-undefined-asyncerror branch from c7b7080 to c95b433 Compare March 26, 2026 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant