Skip to content

[Bug]: Unhandled promise rejections leading to unexpected jest output (with workaround) #15887

@vovkasm

Description

@vovkasm

Version

30.2.0

Steps to reproduce

  1. Clone demo repo at https://github.com/vovkasm/jest-bug-demonstration.git
  2. cd jest-bug-demonstration
  3. npm ci
  4. npm run problem

Expected behavior

Not sure, but with workaround (npm run workaround) it can behave more or less as expected. I think most users expects to get clean assertion error.

> NODE_OPTIONS=--unhandled-rejections=warn jest demo1

(node:55520) UnhandledPromiseRejectionWarning
(Use `node --trace-warnings ...` to show where the warning was created)
(node:55520) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
 FAIL  ./demo1.spec.ts
  ✕ Async. Should fail with correct assertion error (3 != 2) (3 ms)

  ● Async. Should fail with correct assertion error (3 != 2)

    expect(received).toBe(expected) // Object.is equality

    Expected: 2
    Received: 3

      2 |   const res = Promise.reject(new Error("Task failed"));
      3 |
    > 4 |   expect(3).toBe(2);
        |             ^
      5 |
      6 |   await res.catch(() => {});
      7 | });

      at Object.toBe (demo1.spec.ts:4:13)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.252 s, estimated 1 s

Actual behavior

When running tests with Jest, unhandled promise rejections cause unexpected behavior. Currently, when an unhandled promise rejection occurs (due to Promise.reject() without proper handling), Jest's output is disrupted by the Node.js error message, making it difficult to discern the actual test failures.

> jest demo1


 RUNS  ./demo1.spec.ts
/Users/vovkasm/tmp/test1/demo1.spec.ts:4
  const res = Promise.reject(new Error("Task failed"));
                             ^

[Error: Task failed]

Node.js v24.11.0

Additional context

Minimal test code to reproduce:

it("Async. Should fail with correct assertion error (3 != 2)", async () => {
  const res = Promise.reject(new Error("Task failed"));

  expect(3).toBe(2);

  await res.catch(() => {});
});

In real life it was much more complex scenario.

Environment

System:
    OS: macOS 15.7.1
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 24.11.0 - /Users/vovkasm/.local/state/fnm_multishells/54801_1762875355874/bin/node
    npm: 11.6.1 - /Users/vovkasm/.local/state/fnm_multishells/54801_1762875355874/bin/npm
    bun: 1.3.2 - /opt/homebrew/bin/bun
    Deno: 2.5.6 - /opt/homebrew/bin/deno
  npmPackages:
    jest: ^30.2.0 => 30.2.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions