Jest: Jest giving wrong line number for error

Created on 2 Feb 2018  路  30Comments  路  Source: facebook/jest

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

Most helpful comment

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

All 30 comments

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

@zwhitchcox that repo is empty 馃槵

Sorry! Just pushed!

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:

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.

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

@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

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

See the comment here for an interesting finding:

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??

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

image

Hi. I'm using Node 8.9.4 with [email protected]. 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",

Can you create a repro showing this?

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

@zwhitchcox you're using a transformer called jest-sourcemaps. When [email protected] 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).

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

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

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.

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 https://github.com/kulshekhar/ts-jest/pull/449

So, there is two issues:
Line reporting broken in version 22.0.1 of ts-jest. Looks like this PR:
https://github.com/kulshekhar/ts-jest/commit/97ed727fa93d94d17931c13874a0e26ecc07092b#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.

Does it help to apply this diff? #5692

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: https://github.com/facebook/jest/issues/5739

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

... 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. 馃檹

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.

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

See
image

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

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.

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.

FUTURE PEOPLE:

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

same issue +1

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

Was this page helpful?
0 / 5 - 0 ratings