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

Jest giving wrong line number for error #5446

Closed
zwhitchcox opened this issue Feb 2, 2018 · 36 comments
Closed

Jest giving wrong line number for error #5446

zwhitchcox opened this issue Feb 2, 2018 · 36 comments

Comments

@zwhitchcox
Copy link

Do you want to request a feature or report a bug?
Bug report

What is the current behavior?
It gives the line number a few lines above where the error actually is. The error should be on line 7, where Object.assign is run, but it is giving the error on line 10.

Sourcemaps appear to be working, but this particular instance, they didn't for some reason.

I made a repo https://github.com/zwhitchcox/jest-bug, you just have to git clone, yarn install and yarn test

What is the expected behavior?
To give the correct line number

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

Node: v8.4.0
Jest: 22.1.4
OS: GalliumOS (basically Ubuntu 14.04)
yarn: 1.1.0

@zwhitchcox
Copy link
Author

I also copied my code to the react official example, and it had the same problem

@rickhanlonii
Copy link
Member

@zwhitchcox that repo is empty 😬

@zwhitchcox
Copy link
Author

Sorry! Just pushed!

@rickhanlonii
Copy link
Member

Confirmed - the failure is shown on line 10 but the code is on line 7.

Code:

FWIW converting the exports to commonjs will show the correct line so this is probably babel related:

@jwbay
Copy link
Contributor

jwbay commented Feb 3, 2018

Does this repro without jest-sourcemaps? Custom transformers currently can't map stacktraces and code frames against original source code. I would think out-of-the-box babel-jest would work here.

@zwhitchcox
Copy link
Author

No, it wasn't working, and I added that thinking it might work

@rickhanlonii
Copy link
Member

@zwhitchcox it would be helpful if you could remove everything you can from the test repo

@jwbay I had the same thought, but I tested and it's not related to jest-sourcemaps

@rickhanlonii
Copy link
Member

I created the absolute minimum repo: https://github.com/rickhanlonii/jest-issue-5446

See the comment here for an interesting finding:

@zwhitchcox
Copy link
Author

Thanks, sorry, I would've done that, but I'm not home right now. And yeah, I think it's probably babel, but Facebook de facto owns anything related to the internet anyway, right??

@SimenB
Copy link
Member

SimenB commented Feb 3, 2018

Vaguely related to #5449. If I apply the same changes here as I propose in there, this is the error:

image

@nbkhope
Copy link

nbkhope commented Feb 14, 2018

Hi. I'm using Node 8.9.4 with jest@22.2.1. Given the following example

  test('should fail', () => { // Line 31
    expect(1).toBe(2); // Line 32
  });

  test('should do something else', () => {

  });

  test('should do yet something else', () => {

  }); // Line 41

The test output is:

    expect(received).toBe(expected) // Object.is equality

    Expected value to be:
      2
    Received:
      1

      39 |   test('should do yet something else', () => {
      40 |
    > 41 |   });
      42 |

So the error should be at line 32, but Jest says it is at line 41, a difference of 9.

For babel, I'm using:

    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",

@SimenB
Copy link
Member

SimenB commented Feb 14, 2018

Can you create a repro showing this?

@SimenB
Copy link
Member

SimenB commented Feb 17, 2018

@rickhanlonii your example is fixed after #5177. The OP is still wrong, though...

@SimenB SimenB closed this as completed Feb 17, 2018
@SimenB SimenB reopened this Feb 17, 2018
@thymikee
Copy link
Collaborator

@zwhitchcox you're using a transformer called jest-sourcemaps. When jest@22.4.x is out, you can safely remove it entirely from your project, along with this line on top of your test file:

- require('source-map-support').install()

I've just verified it works as expected (from master branch).

@mikebm
Copy link

mikebm commented Mar 5, 2018

@thymikee I am running 22.4.2 and it still requires
require('source-map-support').install()
for proper line number reporting...

@SimenB
Copy link
Member

SimenB commented Mar 5, 2018

That's odd. Do you have a reproduction we can pull down and take a look at?

@mikebm
Copy link

mikebm commented Mar 5, 2018

Unfortunately, I do not have one yet. However, it appears it may be isolated to typescript files, which source-map-support seems to fix from a jest reporting perspective. I am also having issues with code stepping inside the file to be tested. The source maps are way off but fine on the actual TS test file. I'll probably take this over to ts-jest repo if I cannot get this resolved. Thanks.

@SimenB
Copy link
Member

SimenB commented Mar 5, 2018

Are you using ts-jest? If so, can you try downgrading (or upgrading if you're not on latest)? Wondering if it's related to kulshekhar/ts-jest#449

@mikebm
Copy link

mikebm commented Mar 5, 2018

So, there is two issues:
Line reporting broken in version 22.0.1 of ts-jest. Looks like this PR:
kulshekhar/ts-jest@97ed727#diff-b9cfc7f2cdf78a7f4b91a753d10865a2

@SimenB Source stepping typescript breaks in jest when collectCoverage is set to true. Regardless of ts-jest version. Currently only tested with jest 22.4.2. Removing the collectCoverage line from package.json results in proper source stepping. It worked before mapCoverage was deprecated.

@SimenB
Copy link
Member

SimenB commented Mar 6, 2018

Does it help to apply this diff? #5692

@mikebm
Copy link

mikebm commented Mar 6, 2018

I cloned the latest master which includes #5692 and yarn linked it. It still has the same issue when collectCoverage=true.
@SimenB I create a separate issue for this and added a repo that reproduces the source stepping issue here: #5739

For those following along, I created an incorrect line error reporting bug for ts-jest here:
kulshekhar/ts-jest#463
The issue appears to be tied to TypeScript decorators and unmocking ts-jest in unmockedModulePathPatterns.

@AndrewSouthpaw
Copy link
Contributor

AndrewSouthpaw commented May 4, 2018

... since we're diverging with the ts-jest thread, I'd like to draw attention back to @rickhanlonii's finding in this repro. I've found the same thing in my own project, where changing export function to export const fixes the issue with lines being wrong. I was not using jest-sourcemaps as @thymikee identified.

I think this issue is not yet resolved and hope it can be reopened. 🙏

@AndrewSouthpaw
Copy link
Contributor

I worked with the OP repo as well, and have confirmed that changing to export const fixes the issue there as well.

Before:

image

After:

image

Here's the OP repo updated with export const syntax.

@SimenB
Copy link
Member

SimenB commented May 4, 2018

This was fixed in #5945, temporarily reverted in #6106 due to a memory leak.

See
image

@GantMan
Copy link
Contributor

GantMan commented Dec 19, 2018

I'm using ts-jest and my error lines are completely wrong. Is this still an issue?

@petercm
Copy link

petercm commented Feb 9, 2019

FWIW my version of this appears to have been caused by babel-plugin-source-map-support. Removing it from my plugins fixed jest error lines.

@warpdesign
Copy link

warpdesign commented Mar 4, 2019

I am having the same problem. I am using the following packages:

   "babel-core": "6.24.1",
   "babel-jest": "^24.1.0",
   "babel-loader": "6.4.1",
   "babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
   "babel-preset-env": "^1.7.0",

I am not sure what to do to fix the problem since I don't use any sourcemaps.

@hipstersmoothie
Copy link

FUTURE PEOPLE:

If you have minified: true in your .babelrc the line number can be off. trying turning it off when running your tests

@lake2
Copy link

lake2 commented Oct 22, 2019

same issue +1

@djw-sl
Copy link

djw-sl commented Aug 5, 2020

Still getting the same issue. Using jest 25.4.0 in a NodeJS 12.13.0 project.

@airhorns
Copy link
Contributor

airhorns commented Jan 4, 2021

I have this issue on my project when using both babel-jest and esbuild-jest with TypeScript sources and jest-circus. Is it possible Jest itself is adding a pre-amble or something that is causing an offset?

@breautek
Copy link

For those who are having this issue in 2020... Running jest through nyc caused the issue for me.

And I've learned that jest has coverage collection built in, so you can uninstall nyc and add collectCoverage: true to your jest config.

@hnq90
Copy link

hnq90 commented Mar 5, 2021

My project do not have nyc package but still got that issue.
I'm using jest: 26.6.3 & ts-jest: 26.5.3

@kenji4569
Copy link

In my project, I found that the problem is caused by an npm module (dynamoose) which imports source-map-support/register on the inside (https://github.com/dynamoose/dynamoose/blob/master/lib/index.ts#L1).

So I replaced it with a mock in jest.config.js:

  moduleNameMapper: {
    'source-map-support/register': 'identity-obj-proxy'
  }

then the problem is fixed.

@russcarver
Copy link

I am having this issue too in Jest 26 (since last July) with Angular 9/10/11. I've detailed the issue with a minimal repository here

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

No branches or pull requests