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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Error.captureStackTrace from erasing Error stack #5136

Closed
bvaughn opened this issue Dec 19, 2017 · 4 comments · Fixed by #5162
Closed

Prevent Error.captureStackTrace from erasing Error stack #5136

bvaughn opened this issue Dec 19, 2017 · 4 comments · Fixed by #5162

Comments

@bvaughn
Copy link
Contributor

bvaughn commented Dec 19, 2017

I'm working on some custom Jest matchers for the React repo and I noticed a quirk that I believe is intentional, but doesn't seem optimal. Essentially the problem is this: I'm unable to throw an error from within a custom matcher without the stack being overridden by Jest via Error.captureStackTrace.

So a meaningful stack like this:

ReferenceError: asdf is not defined

  38 |   const foo = () => bar();
  39 |   const bar = () => baz();
> 40 |   const baz = () => asdf();
  41 |   expect(() => {
  42 |     foo();
  43 |   }).toWarnDev([

  at baz (path/to/test.js:40:28)
  at bar (path/to/test.js:39:35)
  at foo (path/to/test.js:38:35)
  at path/to/test.js:42:7
  at Object.matcher (path/to/matcher.js:47:11)
  at Object.<anonymous> (path/to/test.js:43:8)

Becomes a generic stack like this:

ReferenceError: asdf is not defined

  41 |   expect(() => {
  42 |     foo();
> 43 |   }).toWarnDev([
  44 |     'foo',
  45 |     'bar',
  46 |   ]);

  at Object.<anonymous> (path/to/matcher.js:43:8)

I'd like a way to preserve the original stack since it contains useful information. I don't object to the current default behavior, but it would be nice if there was a way to mark a specific error to be skipped. Looking at the source code though, it seems like there aren't really any good options:

  • A custom matcher can't throw an instance of JestAssertionError (since the class isn't exposed externally).
  • I could set the error.name to "PrettyFormatPluginError" but this is a pretty major hack for a few reasons (it relies on undocumented internals and it would modify the error message logged to the developer).
  • I could override Error.captureStackTrace, but this could not be done within the matcher; rather it would need to be done in a global beforeEach hook.

What would you think of an approach like the one React uses for error logging (eg a special property Jest checks for on an error like dangerousDontCaptureStack that prevents the stack from being overridden)?

@bvaughn
Copy link
Contributor Author

bvaughn commented Dec 19, 2017

What would you think of an approach like the one React uses for error logging (eg a special property Jest checks for on an error like dangerousDontCaptureStack that prevents the stack from being overridden)?

I would be happy to submit a PR that adds this functionality if there are no objections. 😄

@SimenB
Copy link
Member

SimenB commented Dec 19, 2017

We really should export JestAssertionError, would that be enough?

Sorta related, if you know how to keep a stack across frames, help with #5104 would be appreciated :D

@bvaughn
Copy link
Contributor Author

bvaughn commented Dec 19, 2017

Yeah, that would be even nicer. It just seemed (potentially) more controversial. I'd be happy to put together a PR for that instead.

@github-actions
Copy link

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 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants