-
Notifications
You must be signed in to change notification settings - Fork 654
Properly quote/escape strings in assertIsError #3577
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
Conversation
assert/assert_is_error.ts
Outdated
typeof error === "object" ? error?.constructor?.name : "[not an object]" | ||
}"${msgSuffix}`; | ||
msg = `Expected error to be instance of ${JSON.stringify(ErrorClass.name)}, but was ${ | ||
typeof error === "object" ? JSON.stringify(error?.constructor?.name) : "[not an object]" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know Debatebly the error.name shouldn't need JSON.stringify, but runtime performance is not an issue and this will handle really janky cases (e.g. I can make ErrorClass.name contain quotes cause this is JavaScript we're talking about)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The below script throws with TypeError: Cannot assign to read only property 'name' of function 'class ErrorClass {}'
class ErrorClass {}
ErrorClass.name = 'foo"bar'
I don't think it's possible to make Class.name contain quotes.
assert/assert_is_error.ts
Outdated
error instanceof Error ? error.message : "[not an Error]" | ||
}"${msgSuffix}`; | ||
msg = `Expected error message to include ${JSON.stringify(msgIncludes)}, but got ${ | ||
error instanceof Error ? JSON.stringify(error.message) : "[not an Error]" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, intentionally, only the error.message
is given quotes. I think "[not an Error]"
is misleading since its not a string. Arguably I think [not an Error]
should be replaced with something more descriptive about what has happened.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
Currently messages will look like
expected X but got "["howdy", 2]"
instead ofexpected X but got "[\"howdy\", 2]"
Its misleading, but also inconvenient since often its nice to copy out the
instead got
and use it to fix the test case.