fix: Fail expectations that throw non-Errors #843
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I hit a very strange error in one of my codebases (private, unfortunately, else I'd link to it). Here's a pretend example:
In normal environments,
Buffer.from(undefined)
throws a TypeError, which then correctly passes allinstanceof Error
checks. For reasons which I haven't fully diagnosed, but appear related to something to do with my setup (Jest? JSDom? Unsure.) and this warning, the below actually logs false:Now, this is a silly example, but in my test I was trying to convert a buffer to a string and assert its string result. Something like:
For my example, what I actually wanted was
res.body.toString
(theBuffer.from
was unnecessary), but it was throwing, and silently failing because of the above-described problem.I tried a couple things, like trying to see if something "looks like" an error, but the simplest thing I could land on was to just simply covert non-errors to errors, and then the rest of the codebase is happy. Incidentally, and I would argue this is a good thing, it also now fails if you do
throw 123
, which while not a great throw, should probably fail, and previously did not.Checklist