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

The original function name gets lost in the Jest error stack #10633

Open
gaearon opened this issue Oct 13, 2020 · 18 comments
Open

The original function name gets lost in the Jest error stack #10633

gaearon opened this issue Oct 13, 2020 · 18 comments

Comments

@gaearon
Copy link
Contributor

gaearon commented Oct 13, 2020

test('hello world', () => {
 function BadCode() {
    throw new Error('noo')
  }

  function run(fn) {
    fn()
  }

  run(BadCode)
});

I expect to see BadCode in the stack because it's a named function. But it shows up as fn instead:

      at fn (sum.test.js:3:11)
      at run (sum.test.js:7:5)
      at Object.<anonymous> (sum.test.js:10:3)

Same with error.stack property.

Isn't this just a Node issue?

Nope, try it in Node REPL and see BadCode:

Uncaught Error: noo
    at BadCode (repl:2:11)
    at run (repl:2:5)

Also works correctly in Chrome.

I've done both locally so I used the same Node version (12.18.3). So it's Jest-specific.

I can also reproduce this on Repl.it: https://repl.it/repls/SecondaryItchyGui

I've tried vm.runInContext locally but that did not trigger the issue. I also verified I'm not transpiling anything. So I have no idea what is the cause of this. The underlying bug is probably in Node or V8 but we need to figure out what is special about Jest that is triggering it.

Why fix this?

This affects the new React component stack printing strategy so it would be unfortunate for this to stay unfixed very long.

@flozender
Copy link
Contributor

Would love to give it a shot!

@SimenB
Copy link
Member

SimenB commented Oct 14, 2020

Go for it 🙂

@SimenB
Copy link
Member

SimenB commented Oct 14, 2020

Hmm, maybe not. @gaearon did you test with latest version of Jest? Repl.it is on 24 by default This is what I'm seeing running your example in jest@26.5.2 (node 12.19.0)

  ✕ hello world (3 ms)

  ● hello world

    noo

      1 | test('hello world', () => {
      2 |   function BadCode() {
    > 3 |     throw new Error('noo');
        |           ^
      4 |   }
      5 |
      6 |   function run(fn) {

      at BadCode (test.js:3:11)
      at fn (test.js:7:5)
      at Object.run (test.js:10:3)

@gaearon
Copy link
Contributor Author

gaearon commented Oct 14, 2020

No, I didn't test with a different Jest version. This looks better! Although I'm still confused about why fn even exists in the stack..

@flozender
Copy link
Contributor

@SimenB I tried the same and got fn:

  ✕ hello world (2 ms)

  ● hello world

    noo

      1 | test("hello world", () => {
      2 |   function BadCode() {
    > 3 |     throw new Error("noo");
        |           ^
      4 |   }
      5 | 
      6 |   function run(fn) {

      at fn (__tests__/index.test.js:3:11)
      at run (__tests__/index.test.js:7:5)
      at Object.<anonymous> (__tests__/index.test.js:10:3)

Node: v12.19.0
Jest: v26.5.3

@SimenB
Copy link
Member

SimenB commented Oct 14, 2020

Interesting! It doesn't reproduce in the Jest repo or the project at work.

@flozender
Copy link
Contributor

Oh, yeah! Jest repo gives me BadCode:

  ● hello world

    noo

      1 | test('hello world', () => {
      2 |   function BadCode() {
    > 3 |     throw new Error('noo');
        |           ^
      4 |   }
      5 | 
      6 |   function run(fn) {

      at BadCode (index.test.js:3:11)
      at fn (index.test.js:7:5)
      at Object.run (index.test.js:10:3)

@SimenB
Copy link
Member

SimenB commented Oct 14, 2020

do you have a repository that reproduces?

@gaearon
Copy link
Contributor Author

gaearon commented Oct 14, 2020

React repo does.

@gaearon
Copy link
Contributor Author

gaearon commented Oct 14, 2020

(but with the old version)

@flozender
Copy link
Contributor

You could also try this: https://github.com/flozender/jest-fnName/

@SimenB
Copy link
Member

SimenB commented Oct 14, 2020

React repo doesn't reproduce for me 🙈

image

@gaearon
Copy link
Contributor Author

gaearon commented Oct 14, 2020

Hmm. Actually sorry I created some confusion here — it reproed in a separate setup unrelated to our main Jest config. My mind blanked.

@gaearon
Copy link
Contributor Author

gaearon commented Oct 14, 2020

This is where I could repro it. https://github.com/facebook/react/tree/master/fixtures/legacy-jsx-runtimes
Run yarn && yarn test in that directory.

@SimenB
Copy link
Member

SimenB commented Oct 14, 2020

@flozender's repo works (or doesn't... It reproduces the bug, is what I'm trying to say)

@SimenB
Copy link
Member

SimenB commented Oct 14, 2020

This "fixes" the reproduction

diff --git i/yarn.lock w/yarn.lock
index 3d75ffc..d290b3c 100644
--- i/yarn.lock
+++ w/yarn.lock
@@ -3019,9 +3019,9 @@ source-map-resolve@^0.5.0:
     urix "^0.1.0"
 
 source-map-support@^0.5.6:
-  version "0.5.19"
-  resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
-  integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
+  version "0.5.13"
+  resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.13.tgz#31b24a9c2e73c2de85066c0feb7d44767ed52932"
+  integrity sha512-SHSKFHadjVA5oR4PPqhtAVdcBWwRYVd6g6cAXnIbRiIwc2EhPrTuKUBdSLvlEKyIP3GCf89fltvcZiP9MMFA1w==
   dependencies:
     buffer-from "^1.0.0"
     source-map "^0.6.0"

So related to #9147 somehow is my guess

Adding "jest": {"transform": {}} to package.json also "fixes" it, fwiw

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants