Skip to content

Cannot get coverage report if c8 is running in a sub-directory & src is one level up. #527

Open

Description

@timganter
  • 9.1.0:
  • MacOS:

Hello! When I have c8 running from a sub-directory, I can't seem to get it to report coverage for src code that is one level up from where c8 is running.

video.mov

Activity

  1. ericmorand commented on May 7, 2024

    @ericmorand

    I have the same issue: #498

  2. cenfun commented on May 8, 2024

    @cenfun
    Contributor

    For sub-directory, the source file index.js is outside of the current working directory, so it was excluded. see https://github.com/istanbuljs/test-exclude/blob/master/index.js#L90-L92

  3. ericmorand commented on May 8, 2024

    @ericmorand

    Yes. Actually, c8 doesn't consider the location of the configuration file as the base path to resolve the sources to cover. This makes declaring the sources there useless except when executing c8 from the same directory as the one where the configuration file is located.

    There are some other issues too that prevent c8 from working properly outside of the "root" directory: include and exclude are also not relative to the configuration file location, and they basically contradict the src configuration and the source map of the executed module. Basically c8 is plagued by the same design decisions as nyc but to an even greater extent.

  4. cenfun commented on May 9, 2024

    @cenfun
    Contributor

    @ericmorand You can try mcr CLI

    // mcr.config.js
    export default {
        name: 'My Coverage Report',
        reports: [
            'v8',
            'console-details'
        ],
        outputDir: './coverage-reports',
        entryFilter: {
            '**/node_modules/**': false,
            '**/src/**': true
        },
        sourceFilter: {
            '**/src/**': true
        }
    };
    mcr npx mocha
    ┌────────────┬──────────┬────────────┬──────────┬───────────┬──────────┬─────────────────┐
    │ Name       │    Bytes │ Statements │ Branches │ Functions │    Lines │ Uncovered Lines │
    ├────────────┼──────────┼────────────┼──────────┼───────────┼──────────┼─────────────────┤
    │ src        │          │            │          │           │          │                 │
    │ └ index.js │ 100.00 % │   100.00 % │          │  100.00 % │ 100.00 % │                 │
    ├────────────┼──────────┼────────────┼──────────┼───────────┼──────────┼─────────────────┤
    │ Summary    │ 100.00 % │   100.00 % │          │  100.00 % │ 100.00 % │                 │
    └────────────┴──────────┴────────────┴──────────┴───────────┴──────────┴─────────────────┘
    cd sub-directory
    mcr npx mocha -c ../mcr.config.js
    ┌────────────┬──────────┬────────────┬──────────┬───────────┬──────────┬─────────────────┐
    │ Name       │    Bytes │ Statements │ Branches │ Functions │    Lines │ Uncovered Lines │
    ├────────────┼──────────┼────────────┼──────────┼───────────┼──────────┼─────────────────┤
    │ src        │          │            │          │           │          │                 │
    │ └ index.js │ 100.00 % │   100.00 % │          │  100.00 % │ 100.00 % │                 │
    ├────────────┼──────────┼────────────┼──────────┼───────────┼──────────┼─────────────────┤
    │ Summary    │ 100.00 % │   100.00 % │          │  100.00 % │ 100.00 % │                 │
    └────────────┴──────────┴────────────┴──────────┴───────────┴──────────┴─────────────────┘
    
  5. skullpsg commented on May 9, 2024

    @skullpsg

    @timganter Can you try with --allowExternal on the cmd. It would work.

  6. ericmorand commented on May 9, 2024

    @ericmorand

    @cenfun thank you..I think you got it right by having both a source and an entry filter approach. Can you elaborate a bit on entryFilter?

    Typically:

    We can use entryFilter to filter the entry files. For example, we should remove vendor.js and something-else.js if they are not in our coverage scope.

    Since we have to execute a command (i.e. interpreting an entry point with node like doing npm t), we have an entry point. In what context would we want to filter it out?

  7. cenfun commented on May 9, 2024

    @cenfun
    Contributor

    @ericmorand Both can generate coverage reports from raw v8 coverage data.
    c8 uses the converter v8-to-istanbul, and it can only generate Istanbul reports.
    mcr uses its own converter (can analyze AST). In addition to generating Istanbul reports, it can also generate V8-style reports.

  8. ericmorand commented on May 9, 2024

    @ericmorand

    I opened an issue in your repository to move the conversation there: cenfun/monocart-coverage-reports#22

  9. timganter commented on May 13, 2024

    @timganter
    Author
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions