GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Option for Istanbul coverage to include all source files (as opposed to only files loaded during test) · Issue #1735 · angular/angular-cli

Option for Istanbul coverage to include all source files (as opposed to only files loaded during test) #1735

New issue
Copy link
New issue
Copy link
Open
Enhancement
Open
Option for Istanbul coverage to include all source files (as opposed to only files loaded during test)#1735
Enhancement
Copy link
Labels
area: @angular-devkit/build-angulardevkit/build-angular:karmafeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationFeature request for which voting has completed and the request is now under consideration
Milestone
needsTriage

Description

@westonpace
westonpace
opened on Aug 17, 2016
Issue body actions

Please provide us with the following information:

  1. OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?)
    Any
  2. Versions. Please run ng --version. If there's nothing outputted, please run
    in a Terminal: node --version and paste the result here:

angular-cli: 1.0.0-beta.11-webpack.2
node: 6.2.2
os: linux x64

  1. Repro steps. Was this an app that wasn't created using the CLI? What change did you
    do on your code? etc.

Create an application. Test some, but not all of the source files. Generated coverage will not include files that were not loaded by the tests.

  1. The log given by the failure. Normally this include a stack trace and some
    more information.

N/A

  1. Mention any other details that might be useful.

Thanks! We'll be in touch soon.

Similar to gotwarlost/istanbul#275 and karma-runner/karma-coverage#125

Reacted by Jason Tesser, Antony Budianto, Alexander Rusakov, Edmundo Santos, Dominik, Victor Passapera, Almar Schouten, bjakubiak, Ben Elliott, raghles and 15 more

Activity

Next
  1. filipesilva
    added
    type: enhancement
    P5The team acknowledges the request but does not plan to address it, it remains open for discussionThe team acknowledges the request but does not plan to address it, it remains open for discussion
    on Aug 23, 2016
  2. jtesser commented on Oct 20, 2016

    @jtesser
    jtesser
    on Oct 20, 2016
    More actions

    +1
    karma-coverage has an option includeAllSources which could be used.

  3. filipesilva commented on Oct 24, 2016

    @filipesilva
    filipesilva
    on Oct 24, 2016
    Contributor
    More actions

    If anyone has is willing to do a PR, or point me in the right direction to do it with webpack/karma-remap-istanbul, I'd be happy to look at it.

  4. jtesser commented on Oct 26, 2016

    @jtesser
    jtesser
    on Oct 26, 2016
    More actions

    @filipesilva what exactly are you asking for I am confused.

  5. westonpace commented on Oct 27, 2016

    @westonpace
    westonpace
    on Oct 27, 2016
    Author
    More actions

    Given that I understand webpack a little better now the includeAllSources option in karma would not work. Under the hood that is including all files that were instrumented. Webpack won't instrument code that isn't loaded so includeAllSources would not see it.

    Instead I ended up using a simpler workaround. I added a single file:

    src/app.module.spec.ts

    which is simply (e.g. no test suites or cases):

    import './app.module';

    This file will cause webpack to load the entire application. The istanbul coverage will only be generated for local code (since node_modules is excluded). As far as I can tell this is a pretty ideal solution with 2 caveats:

    • It is still possible for code to be missed in coverage if that code is not included in the application at all. One could potentially mitigate this with a webpack plugin that checked for unused code in ./src and reported it.
    • It will cause an increase in test times depending on the size of the application.
    Reacted by Tamás Érdfalvi, Mikhail Khalizev and Gábor Beckl
  6. westonpace commented on Oct 27, 2016

    @westonpace
    westonpace
    on Oct 27, 2016
    Author
    More actions

    Let me know if you want me to make a pull request with this particular solution (or go ahead and implement it as it is simple enough). I wasn't sure creating a file named app.module.spec.ts would be universally accepted (some might be confused by its presence) and was hoping that someone might have a better, more invisible, idea on how to accomplish the same thing.

  7. filipesilva
    added
    type: discussion
    on Nov 2, 2016
  8. filipesilva commented on Nov 2, 2016

    @filipesilva
    filipesilva
    on Nov 2, 2016
    Contributor
    More actions

    @westonpace I see how your solution works. It's something that's worth discussing before being implemented though...

    A more direct use of your solution would be to add import './app.module'; to src/test.ts instead.

    I think the biggest drawback is now that the whole app is indeed loaded on every test, whereas we only realistically want it for code coverage tests.

    Not sure of what the best approach is for now, but at least we have a workaround. Let's let this issue sit open for a while, see what people say.

  9. seeruk commented on Nov 4, 2016

    @seeruk
    seeruk
    on Nov 4, 2016
    More actions

    Another option may be to modify the following line in test.ts to require everything:

    .then(() => require.context('./', true, /\.spec\.ts/))
    // Becomes
    .then(() => require.context('./app', true, /\.ts/))
    

    If you use lazy loaded modules importing AppModule won't include everything.

    It seems like the main downside with showing coverage for everything is that you may have files that can't be covered, or don't make sense to be covered that will be included in the report, skewing your numbers a little.

    Reacted by Yaroslav Admin
  10. blaugold commented on Nov 7, 2016

    @blaugold
    blaugold
    on Nov 7, 2016
    More actions

    I'm successfully using

    .then(() => require.context('./', true, /\/app\/.*\.ts$/))

    in src/test.ts.
    @seeruk's solution seems to trigger a weback bug in the context loader where files are loaded as directories. Putting the 'app' path part in the regex works.

    Reacted by Mark Lagendijk, Victor Passapera and David Barreto
  11. seeruk commented on Nov 7, 2016

    @seeruk
    seeruk
    on Nov 7, 2016
    More actions

    Aah, perhaps it needed a slash on the end of the path. I'd not actually tried that solution with angular-cli, I'm just doing something similar in the boilerplate I've been making.

  12. rashmi-dey commented on Mar 2, 2017

    @rashmi-dey
    rashmi-dey
    on Mar 2, 2017
    More actions

    I am not using angular-cli. Is there an option to include all source files in coverage while using karma-test-shim.js to load systemjs configuration and all test files.

  13. arusakov commented on Mar 25, 2017

    @arusakov
    arusakov
    on Mar 25, 2017
    Contributor
    More actions

    @filipesilva

    I want to suggest separated file coverage.ts near test.ts and coverage option in .angular-cli.json.
    In coverage.ts we can do all stuff related to full code coverage and developers can configure what file use for coverage via coverage option (use "coverage": "coverage.ts" - for full coverage, and "coverage": "test.ts" keeps current behaviour).

    Reacted by Florian Neumann
  14. kpaxton commented on May 16, 2017

    @kpaxton
    kpaxton
    on May 16, 2017
    More actions

    @arusakov I have implemented something similar to what you are suggesting in my own application. The downfall to this is that I then have two test start points that need to stay in sync when things might be changed.
    I implemented a coverage.ts in the same folder as test.ts and updated the karma-conf.js to actually change the files array and preprocessors object to reflect the new start point if config.angularCli.codeCoverage is true.
    It works pretty well, but having a flag available to test.ts to allow for switching the context based on that flag I think would be slightly more ideal than having two separate files.

  15. 18 remaining items

  16. ngbot
    modified the milestone: Backlog on Oct 1, 2019
  17. CraigComeOnThisNameCantBeTaken commented on Oct 27, 2019

    @CraigComeOnThisNameCantBeTaken
    CraigComeOnThisNameCantBeTaken
    on Oct 27, 2019
    More actions

    Since this is still open, I intend to solve this issue in my project by creating a schematic that adds tests where theyre missing, I found that even if the spec files are empty as long as they import the file it seems to generate correct coverage.

  18. johnthagen commented on Feb 23, 2020

    @johnthagen
    johnthagen
    on Feb 23, 2020
    More actions

    I've had good luck using the karma-sabarivka-reporter plugin to pick up missed source files into coverage.

    Reacted by Igor and Jeroen van Warmerdam
  19. clydin
    added
    devkit/build-angular:karma
    triage #1
    on May 27, 2020
  20. PiDelport
    added 4 commits that reference this issue on Jul 27, 2021

    test(web-client): fix coverage reporting

    f6dd3d2

    test(web-client): fix coverage reporting

    a9cb7c4

    test(web-client): fix coverage reporting

    b972d5a

    test(web-client): fix coverage reporting (#37)

    b94f080
  21. angular-robot
    added
    feature: under considerationFeature request for which voting has completed and the request is now under considerationFeature request for which voting has completed and the request is now under consideration
    on Feb 1, 2022
  22. ngbot
    modified the milestones: Backlog, needsTriage on Feb 1, 2022
  23. alan-agius4
    removed
    triage #1
    on Jun 24, 2022
  24. chucknelson commented on Aug 16, 2024

    @chucknelson
    chucknelson
    on Aug 16, 2024
    More actions

    Has there been any progress or changes in this issue over the past few Angular versions?

    Seems like the two primary options to force coverage reporting on all sources, regardless of if they have a .spec file, are:

    1. Add an app.module.spec.ts file that just imports app.module.ts.
    2. Use https://github.com/kopach/karma-sabarivka-reporter

    Is that still the situation today?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: @angular-devkit/build-angulardevkit/build-angular:karmafeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationFeature request for which voting has completed and the request is now under consideration

    Type

    Enhancement

    Projects

    No projects

      Milestone

      • needsTriageNo due date

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.