Skip to content

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

Merged
merged 3 commits into from
Aug 29, 2023
Merged

Conversation

jeff-hykin
Copy link
Contributor

@jeff-hykin jeff-hykin commented Aug 25, 2023

Currently messages will look like

  • expected X but got "["howdy", 2]" instead of
  • expected 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.

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]"
Copy link
Contributor Author

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)

Copy link
Member

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.

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]"
Copy link
Contributor Author

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.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@kt3k kt3k merged commit ff19248 into denoland:main Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants