Skip to content

Make exceptions/promise rejections testable with throw() even if they aren't instances of Error #1841

Closed
@sth

Description

@sth

In #1704 support for non-Error exceptions was removed from throws(). Since promise rejections are also tested with throws(), this also removed support for promise rejections that are not instances of Error. While non-Error exceptions might be rare, rejecting promises with non-Error values doesn't seem too uncommon.

throws() is now strictly less useful than before. It can still test cases that were previously tested with throws(x, Error), but for other cases it will always fail. Code that throws non-standard exceptions or uses such promises always fails the assertion.

The fix in #1704 is based on issue #1440 and a motivating example seems to be #1439, where somebody explicitly specified that an Error instance should be thrown and then got confused that a thrown string didn't match. This test have easily been fixed by removing the Error (at least I assume so).

With the new throw() this test still fails with a rather cryptic error message (Function threw exception that is not an error: "...") and now can't be easily fixed. The situation is not really improved, the user needs to implement their own throw() or refactor their library to work with the testing framework. Usually you would want it the other way around, having the tests first to support the refactoring.

I don't think there is really a good reason to hard fail in non-Error values. To solve #1439 a clearer error message would be enough. It should be no problem to explicitly tell the user that the exception/rejection didn't have the specified type.

Additionally from the name one would expect throws() to just check for any thrown exceptions, not to fail when a wrong value is thrown.

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