-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't control stringification of error messages #3985
Comments
I think this is probably a bug, and would encourage a PR to switch the order of the conditionals. It'd be helpful to understand what assertion library has these errors with |
the |
It was not assertions where I was seeing this, the problem I was having was with tests that were failing because they were throwing actual exceptions from buggy code, and this was preventing me from getting the information that I needed to determine why those exceptions had been thrown. |
What about
|
Another year, another crippling bug in essential tooling that seems to receive zero attention from developers... |
Prerequisites
faq
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
I've been running into issues with error stringification, due to the change that was introduced in #2101.
My application adds additional context to error messages in development by adding additional properties to the
Error
objects, and I've struggled to get Mocha to display any of this additional information.The main problem is that the code used to ensure that message is a string is this (from lib/reporters/list.js):
The problem with that is that you can (theoretically) customize how Mocha displays your error messages, but only if the error doesn't have a
message
property that includes atoString
method, which seems like an odd edge case for error messages.So in my case, I have the following chain of events making this impossible.
message
property, and it is always aString
String.prototype
has atoString
method that just returns itselftoString
method on a string.This also means that if you are using a custom
Error
subclass that includes aninspect
method, then whether that function gets called or not can be affected by what themessage
property contains. It will work if that property doesn't have atoString
method, but won't work if it does.The only way I've found to make this work is with
new Error( undefined )
, which seems to be the only way to get a value into the message property that doesn't have atoString
method, unfortunately that fixes it for Mocha but makes it useless for everything else.My recommendation would be to just swap the order of the checks, if the error has an
inspect
method it seems like that should be used regardless of what the message is.Steps to Reproduce
Expected behavior: [What you expect to happen]
Both of the test should fail with an error message that gets displayed with a prefix of
Custom Formatted Error
.Actual behavior: [What actually happens]
Only one of them gets formatted correctly.
Reproduces how often: [What percentage of the time does it reproduce?]
100% reproducible
Versions
mocha --version
andnode node_modules/.bin/mocha --version
: 6.2.0The text was updated successfully, but these errors were encountered: