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

Debugging from jest test is all out of wack with the line numbers #171

Closed
dsanders1234 opened this issue Jan 30, 2018 · 10 comments
Closed

Comments

@dsanders1234
Copy link

I don't know if this is just a configuration fix (such as to use inline source maps) or what, but it doesn't appear to work out of the box, at least when debugging in IntelliJ. It works when stepping through actual jest code, but not in the code under test.

@alexjoverm
Copy link
Owner

Hmmm, probably there could be some configuration missing for source maps on Jest. That's something we must investigate.

@alexjoverm
Copy link
Owner

Could you @dsanders1234 provide some reproduction steps/repo?

@dsanders1234
Copy link
Author

dsanders1234 commented Feb 6, 2018

Upon further investigation, it happens when jest configuration "collectCoverage" is set to true

@alexjoverm
Copy link
Owner

@dsanders1234 could you describe in detail the issue? Do you have a fix for it?

@abner
Copy link

abner commented Jun 11, 2018

Upon further investigation, it happens when jest configuration "collectCoverage" is set to true

When collecting coverage, the code is istrumented, i think. Because of that, the source map generated wouldn't match the actual code.

@jordond
Copy link

jordond commented Jul 18, 2018

I was having the same problem, and I narrowed it down to "allowSyntheticDefaultImports": true in the tsconfig.json.

So I created 2 more tsconfigs.

tsconfig.base.json

{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es5",
    "rootDir": "src",
    "sourceMap": true,
    "strict": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "suppressImplicitAnyIndexErrors": true,
    "baseUrl": ".",
    "typeRoots": ["node_modules/@types"]
  },
  "include": ["src"]
}

Then a config for running jest.
Note: inlineSourceMap and inlineSources were required for me to debug properly using VSCode.

tsconfig.test.json

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "module": "commonjs",
    "inlineSourceMap": true,
    "inlineSources": true
  }
}

And finally the config file for rollup to use when building

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "module": "es2015",
    "lib": ["es2015", "es2016", "es2017", "dom"],
    "declarationDir": "dist/types",
    "outDir": "dist/lib",
    "declaration": true,
    "allowSyntheticDefaultImports": true
  },
  "include": ["src"]
}

Now I can properly debug tests with VSCode, and linting/building still works.

@alexjoverm
Copy link
Owner

@jordond that setup looks nice! Do you mind opening a PR so we can merge the changes with the repo?

@Toilal
Copy link
Contributor

Toilal commented Nov 3, 2018

@jordond How do you load tsconfig.test.json file when running tests ? It should appear somewhere in package.json jest section ?

I have found this page, it works when running npm run test from command line and runs in Intellij IDEA too, but still no breakpoint because line numbers are still broken :(

"globals": {
  "ts-jest": {
    "tsConfig": "tsconfig.test.json"
  }
},

@Toilal
Copy link
Contributor

Toilal commented Nov 3, 2018

"lib": ["es2015", "es2016", "es2017", "dom"] should be moved to common tsconfig.base.json file.

@Toilal
Copy link
Contributor

Toilal commented Nov 3, 2018

In my case, it seems the problem is only related to collectCoverage: true, as stated by @dsanders1234 comment. @jordond configuration doesn't help, only setting collectCoverage: false in package.json fix the issue.

I would advocate for removing this setting from package.json, and adding --coverage into npm test and test:watch scripts. With this change, it'll still run with coverage inside travisCI and other scripts relying on npm run test, while having IDE debug functionnal (tested at least with IntelliJ IDEA).

Toilal added a commit to Toilal/typescript-library-starter that referenced this issue Nov 3, 2018
As code coverage seems to break sourcemaps line numbers, this change moves the coverage option from package.json Jest config to npm script --coverage flag for IDE debugging to work as expected.

Close alexjoverm#171
alexjoverm pushed a commit that referenced this issue Nov 21, 2018
…#254)

As code coverage seems to break sourcemaps line numbers, this change moves the coverage option from package.json Jest config to npm script --coverage flag for IDE debugging to work as expected.

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

No branches or pull requests

5 participants