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

Stack trace line numbers are misaligned when using source-map-support #2372

Closed
silasdavis opened this issue Feb 17, 2021 · 9 comments
Closed
Labels
Not An Issue Not ts-jest issue

Comments

@silasdavis
Copy link

silasdavis commented Feb 17, 2021

## 🐛 Bug Report

When testing files that use (https://github.com/evanw/node-source-map-support) with:

import 'source-map-support/register';

The line numbers in stack traces displayed via tests run with ts-jest are wrong. In particular they point to the source line in the compiled .js files not the .ts files despite referencing the .ts files.

I want to use source-map-support in my production files to have nice stack traces.

To Reproduce

Test a file with ts-jest that has import 'source-map-support/register'; and throw an error to get a stack trace. See the line numbers are misaligned. Remove the import and see the line numbers are correct.

Expected behavior

Line numbers should be correct from ts-jest regardless of whether source-map-support is imported.

Link to repo

Minimal repro here:

https://github.com/silasdavis/ts-jest-repro

Just run yarn test (see the README.md for more details).

Annotated output:

/usr/bin/node /usr/lib/node_modules/yarn/bin/yarn.js test
yarn run v1.22.10
$ tsc --build && jest
 FAIL  src/source-map.test.ts
  ● tests › foo

    bah

       8 |   return {a: 1}
       9 | }
    > 10 |
         | ^
      11 | export function blah() {
      12 |   throw new Error('bah')
      13 | }

!!! line below is incorrect (should be line 12) !!!
      at Object.blah (src/source-map.ts:10:11)
      at src/source-map.test.ts:8:26
      at Object.<anonymous> (src/source-map.test.ts:6:46)

 FAIL  src/no-source-map.test.ts
  ● tests › foo

    bah

      12 |
      13 | export function blah() {
    > 14 |   throw new Error('bah')
         |         ^
      15 | }
      16 |

!!! line below is correct !!!
      at Object.blah (src/no-source-map.ts:14:9)
      at src/no-source-map.test.ts:5:5
      at Object.<anonymous> (src/no-source-map.test.ts:4:15)

Test Suites: 2 failed, 2 total
Tests:       2 failed, 2 total
Snapshots:   0 total
Time:        0.877 s, estimated 2 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Process finished with exit code 1

Debug log:

Find ts-jest log here: https://github.com/silasdavis/ts-jest-repro/blob/main/ts-jest.log

envinfo

System:
    OS: Linux cora 5.10.12-arch1-1 #1 SMP PREEMPT Sun, 31 Jan 2021 00:41:06 +0000 x86_64 GNU/Linux
    Node version: v14.15.5

Npm packages:
    jest: 26.6.3
    ts-jest: 26.5.1
    typescript: 4.1.5
    babel(optional): N/A
@silasdavis
Copy link
Author

See links in issues above in jest and source-map-support.

Let me know if I should definitely move this issue to one of those repros. It might be nice to keep it open to track the issue in combination for others experiencing it.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 17, 2021

  • ts-jest cannot control what you import. I'd recommend not using source-map-support in this case
  • ts-jest only transpile ts to js and provide source map to Jest. The rest is handled by Jest itself

In general it is not in control of ts-jest about this.

@silasdavis
Copy link
Author

silasdavis commented Feb 17, 2021

Thanks for the quick reply.

I'd recommend not using source-map-support in this case

Many (most?) typescript users will want to have typescript line numbers in the stack traces. I am not aware of another option for doing that. Are you amenable to the argument that this an issue worth addressing in order to participate in the broader Typescript ecosystem? It's not clear to me why an import would have this effect given what you say, are you able to shed any light on it?

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 17, 2021

When we transform ts to js, we also provide source map which is produced by TypeScript Compiler API. Therefore I think it's not needed to add source-map-support in. By default, stack trace should work without the need of using source-map-support library.

The problem of using source-map-support is: this will be loaded by Jest so ts-jest cannot do anything about it sadly.

@silasdavis
Copy link
Author

silasdavis commented Feb 17, 2021

I will not be using ts-jest in production. I import source-map-support to give me meaningful error traces in production. I could attempt to work around this by avoiding importing source-map-support in NODE_ENV === 'test', but this has issues, in particular the project is strict typescript and I cannot use require so I would need to use dynamic import which is not ideal.

To be clear: the problem arises because I have this import in files under test from my main application.

@silasdavis
Copy link
Author

The problem of using source-map-support is: this will be loaded by Jest so ts-jest cannot do anything about it sadly.

Ah so you think it is somehow overriding the metadata you provide? The odd thing is it works fine (line numbers correct) in vanilla node runtime so must be some interaction with jest.

I have imported this ticket to this comment: jestjs/jest#10330 (comment)

I'll close this here for now.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 17, 2021

What we can try is, allow to disable TypeScript source map via tsconfig. I can investigate about that thing.

@silasdavis
Copy link
Author

silasdavis commented Feb 17, 2021

Okay, just for any fellow travellers it would appear than since 12.12.0 (nodejs/node#29564) source-map-support is not required since there is native node support. So unless you transitively depend on it you can safely remove it and still get nice stack traces. That seems to resolve issues with jest/ts-jest.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 17, 2021

Oh I see, so no need any extra investigation then. Thanks for the information !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not An Issue Not ts-jest issue
Projects
None yet
Development

No branches or pull requests

2 participants