Incorrect line numbers in Jest output if sourceMaps are not enabled

See original GitHub issue

Example

I put throw new Error('TEST ERROR') in one of my test files.

Source maps not enabled in .swcrc

Jest output shows the failure occurring at the wrong line:

 FAIL  src/__tests__/Skeleton.test.tsx
  ● disables the animation if and only if enableAnimation is false

    TEST ERROR

      68 |     expect(skeleton).toHaveClass('react-loading-skeleton')
      69 |     expect(skeleton).toHaveClass('test-class')
    > 70 | })
         |   ^
      71 |
      72 | it('applies the containerClassName and containerTestId', () => {
      73 |     render(<Skeleton containerClassName="test-class" containerTestId="myTestId" />)

      at Object.<anonymous> (src/__tests__/Skeleton.test.tsx:70:11)

Source maps enabled in .swcrc

Jest output shows the failure occurring at the correct line:

 FAIL  src/__tests__/Skeleton.test.tsx     
  ● disables the animation if and only if enableAnimation is false

    TEST ERROR

      58 |     expect(skeleton.style.animation).toBe('none')
      59 |
    > 60 |     throw new Error('TEST ERROR')
         |           ^
      61 | })
      62 |
      63 | it('uses a custom className', () => {

      at Object.<anonymous> (src/__tests__/Skeleton.test.tsx:60:11)

Possible Solutions

  • If this behavior is unintended, fix the bug. (I can provide a minimal reproducible example.)
  • Update https://swc.rs/docs/usage-swc-jest to recommend enabling source maps in .swcrc.
  • (?) Automatically enable source maps in the @swc/jest code.

Additional Context

This occurred in a React .tsx file.

.swcrc:

{
    "jsc": {
        "parser": {
            "syntax": "typescript",
            "tsx": true,
            "dynamicImport": true
        },
        "target": "es2021"
    },
    "module": {
        "type": "commonjs"
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:6

github_iconTop GitHub Comments

3reactions
Thisencommented, Dec 3, 2021

Any status on this?

2reactions
aaronncfcacommented, Jun 28, 2022

I’m facing this issue even with sourceMaps:true, even after clearing the cache! Tests run but line numbers a good bit off. I don’t want to spam a closed issue, but I’m not sure what else to try. Let me know if any suggestions!

Here’s an exerpt of my .swcrc:

{
    "jsc": {
      "target": "es2017",
      "parser": {
        "syntax": "ecmascript",
        "jsx": true, [...]
      },
        "react": {
          "pragma": "React.createElement", [...]
        }
        "hidden": {
          "jest": true
        }
      }
    },
    "sourceMaps": true,
    "module": {
      "type": "commonjs", [...]
    }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest test of Typescript shows wrong error lines - Stack Overflow
I have sourceMap: true, and changed to sourceMaps:true, still with no change. I added the "sourceMaps":true to the "compilerOptions" section. I ...
Read more >
Debug Node.js Apps using Visual Studio Code
The JavaScript debugger of VS Code supports source maps that help debugging of transpiled languages, for example, TypeScript or minified/uglified JavaScript.
Read more >
Why does the stack trace for my jest tests point to the wrong ...
It has to do with source maps. If those are not correctly generated or processed, the debugger will show the wrong line. I...
Read more >
Jest + Babel: Debugger stops on a wrong line : WEB-27680
This seems to be a systematic problem no matter what the source code is. ... Is Webstorm using the transpiled line numbers instead...
Read more >
Configuring Jest
When using the --config option, the JSON file must not contain a "jest" key: ... number it represents the maximum number of uncovered...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found