Skip to content

[Bug]: assert.throws errors always formatted as not thrown #13264

@theScottyJam

Description

@theScottyJam

Version

29.0.3

Steps to reproduce

git clone https://github.com/theScottyJam/jest-assert-throws-bug-report
cd jest-assert-throws-bug-report/
npm i
npm test

Expected behavior

When Node's assert.throws() assertion error is caught and reported by Jest, it should report the original error message, or at least report an accurate error message.

Actual behavior

Jest will always report an assert.throws() error with the following message:

Expected the function to throw an error.
But it didn't throw anything.

Additional context

The reproducible example contains two test cases. The first will cause assert.throws() to fail because the message property contains the incorrect content. The second will cause assert.throws() to fail because the wrong error class was thrown. In both cases, you'll find that Jest will report the error as "Expected the function to throw an error. But it didn't throw anything". To proove that Jest is incorrectly reporting the error, I've also caught and logged out these assertion errors. You can find the logged errors above the Jest output. There, you'll find accurate output such as:

    The actual error: AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
    + actual - expected
    
      Comparison {
    +   message: 'message 1'
    -   message: 'message 2'
      }

and

    The actual error: AssertionError [ERR_ASSERTION]: The "TypeError" validation function is expected to return "true". Received TypeError: SyntaxError: message 1

Environment

System:
    OS: Linux 5.19 Fedora Linux 36 (Workstation Edition)
    CPU: (4) x64 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
  Binaries:
    Node: 16.14.0 - /usr/bin/node
    npm: 8.3.1 - /usr/bin/npm
  npmPackages:
    jest: ^29.0.3 => 29.0.3

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