Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to disable built-in enhanced formatting of Node AssertionError #8728

Closed
Zalathar opened this issue Jul 21, 2019 · 10 comments
Closed

Option to disable built-in enhanced formatting of Node AssertionError #8728

Zalathar opened this issue Jul 21, 2019 · 10 comments

Comments

@Zalathar
Copy link
Contributor

馃殌 Feature Proposal

It should be possible to disable Jest's built-in handling of Node's AssertionError, to allow better integration with other tools that also enhance assertion error messages.

Motivation

I've been trying to configure Jest and power-assert to work well together, so that I can use assert in my code and tests, and see power-assert's enhanced output when running tests under Jest.

The biggest obstacle is that Jest already has special enhanced handling of AssertionError failures. Normally that's good, but when combined with power-assert it means that Jest's (understandably) less-useful enhanced output takes priority, and the more-useful power-assert output gets pushed down and is harder to see.

It would therefore be nice to have the option to disable the Jest-side enhancement, and have it treat AssertionError in the same way as all other errors.

Example

When the user enables power-assert support in their Jest and Babel configuration, they would also choose to disable Jest's special treatment of AssertionError, to give more helpful test failure output.

Pitch

The enhanced output is built into jest-circus (and the Jasmine runner). Custom environments can't cleanly undo the enhancements, and there's currently no reasonable way to opt out of them.

@Zalathar
Copy link
Contributor Author

Side note: It's almost possible to get rid of the enhanced formatting, by using a custom environment to get rid of every part of the formatted error message prior to \nMessage:\n .

In practice this doesn't quite work cleanly, because the actual error message is full of chalk formatting codes around every line, so the necessary string manipulation becomes a lot harder.

@jeysal
Copy link
Contributor

jeysal commented Jul 21, 2019

Also stumbled over this at some point and had to make https://github.com/spockjs/spockjs/blob/master/packages/assertion-post-processor-regular-errors/src/index.ts
Maybe we could move adding the handler into the environment so that a custom one can just not do it.

@Zalathar
Copy link
Contributor Author

Another possibility would be to add the original error as a property of the custom message object.

https://github.com/facebook/jest/blob/33e2d5ce454554363198c8d8a673793222955dbc/packages/jest-circus/src/formatNodeAssertErrors.ts#L64

This would make it easier for a custom environment to cleanly undo the enhanced formatting.

@SimenB
Copy link
Member

SimenB commented Jul 21, 2019

Yeah, I don't want the complexity of allowing an environment to influence this. Exposing the original error is something we want to do anyways (#4547, #7594), so I think that's a better approach. Would you need this in a reporter or somewhere else? How did you integrate power-assert?

@Zalathar
Copy link
Contributor Author

Zalathar commented Jul 21, 2019

How did you integrate power-assert?

I added babel-preset-power-assert to my Babel config, which takes care of (a) rewriting 'assert' imports to 'power-assert' imports, and (b) rewriting assert calls to capture intermediate values and pass them along.

Internally, power-assert takes care of calling the underlying assert module, intercepting the thrown AssertionError, and then replacing its message with an enhanced message and re-throwing.

@SimenB
Copy link
Member

SimenB commented Jul 21, 2019

Right, so you really just need it in the reporter. At that point, we might consider supporting it in the built-in reporter as well

@Zalathar
Copy link
Contributor Author

If I could keep using the default reporter, and just set a reporter option to tell it to use the original message instead of the one enhanced by formatNodeAssertErrors.ts, that would be convenient.

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

github-actions bot commented May 3, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants