Skip to content

[ng test] Can't debug tests when -code-coverage #22010

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

Closed
1 of 15 tasks
sroucheray opened this issue Oct 25, 2021 · 3 comments · Fixed by #22016
Closed
1 of 15 tasks

[ng test] Can't debug tests when -code-coverage #22010

sroucheray opened this issue Oct 25, 2021 · 3 comments · Fixed by #22016

Comments

@sroucheray
Copy link

🐞 Bug report

Command

  • new
  • build
  • serve
  • test (--code-coverage)
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc

Is this a regression?

I have never really seen it worked despite a previously closed issue #7117

Description

When ng test runs with --code-coverage, sourcemaps for *.ts source files do not match (while sourcemaps for *.spec.ts files are fine). Thus it is not possible to breakpoint into source files nor step in the code. So either you get the sourcemaps or if you need to debug your test, you need to stop the tests and relaunch it without --code-coverage.

🔬 Minimal Reproduction

Any angular project with latest version and source with spec files can demonstrate de problem.

  1. launch test on the project ng test --code-coverage, Karma launches the browser
  2. click on the debug button, Karma debug window opens
  3. open the devtools and in the source panel open one of the source files, you'll notice that breakpoints can be added to non code lines (comments) but cannot be added to some code lines. Actually the source displayed in the devtools are not in sync with the real sources. It seems there is a problem interpreting the sourcemaps when code coverage is activated.

NB: there is no problem adding breakpoints or step in spec files, the problem only happens in source files.
NB: there is no problem without --code-coverage

🌍 Your Environment

Angular CLI: 12.2.9
Node: 14.18.0
Package Manager: yarn 1.22.15
OS: linux x64

Angular: 12.2.9
... animations, cdk, cli, common, compiler, compiler-cli, core
... elements, forms, language-service, localize
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1202.11
@angular-devkit/build-angular   12.2.9
@angular-devkit/core            12.2.9
@angular-devkit/schematics      12.2.9
@schematics/angular             12.2.9
ng-packagr                      12.2.4
rxjs                            6.5.5
typescript                      4.3.5
webpack                         5.59.1

@sroucheray sroucheray changed the title [ng test] Can't debug tests when -code-coverage [ng test] Can't debug tests when -code-coverage Oct 25, 2021
@alan-agius4 alan-agius4 added devkit/build-angular:karma freq1: low Only reported by a handful of users who observe it rarely severity3: broken type: bug/fix labels Oct 25, 2021
@alan-agius4
Copy link
Collaborator

This seems to be an issue upstream with sourcemaps merging. However, since now the CLI uses Babel I think there are avenues here to replace the code instrumentation webpack plugin and leverage istanbul JS official plugin babel-plugin-istanbul which it seems that it doesn't have the same problem.

@alan-agius4 alan-agius4 self-assigned this Oct 26, 2021
@ngbot ngbot bot added this to the Backlog milestone Oct 26, 2021
filipesilva pushed a commit that referenced this issue Oct 27, 2021
…code coverage is enabled

With this change we replace `@jsdevtools/coverage-istanbul-loader` webpack loader with [`babel-plugin-istanbul`](https://github.com/istanbuljs/babel-plugin-istanbul) which is an official Babel plugin by the istanbuljs team.

Previously, when code coverage was enabled we had multiple Babel runs on the same file. This is because istanbuljs' `instrumentSync` and `instrument` APIs which are used by the Webpack plugin invokes Babel directly https://github.com/istanbuljs/istanbuljs/blob/66bc39b3c7b301a4b4456101a9996f90b1638dc0/packages/istanbul-lib-instrument/src/instrumenter.js#L98

By using the babel plugin directly, we avoid this which also improves the sourcemaps correctness and test performance.

Closes #22010
filipesilva pushed a commit that referenced this issue Oct 27, 2021
…code coverage is enabled

With this change we replace `@jsdevtools/coverage-istanbul-loader` webpack loader with [`babel-plugin-istanbul`](https://github.com/istanbuljs/babel-plugin-istanbul) which is an official Babel plugin by the istanbuljs team.

Previously, when code coverage was enabled we had multiple Babel runs on the same file. This is because istanbuljs' `instrumentSync` and `instrument` APIs which are used by the Webpack plugin invokes Babel directly https://github.com/istanbuljs/istanbuljs/blob/66bc39b3c7b301a4b4456101a9996f90b1638dc0/packages/istanbul-lib-instrument/src/instrumenter.js#L98

By using the babel plugin directly, we avoid this which also improves the sourcemaps correctness and test performance.

Closes #22010

(cherry picked from commit cf77b73)
@sroucheray
Copy link
Author

Thanks a lot for your work @alan-agius4 and @filipesilva ! That's going to be very helpful !

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants