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

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

Open
timganter opened this issue May 4, 2024 · 9 comments

Comments

@timganter
Copy link

  • 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
@ericmorand
Copy link

I have the same issue: #498

@cenfun
Copy link
Contributor

cenfun commented May 8, 2024

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

@ericmorand
Copy link

ericmorand commented May 8, 2024

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.

@cenfun
Copy link
Contributor

cenfun commented May 9, 2024

@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 % │                 │
└────────────┴──────────┴────────────┴──────────┴───────────┴──────────┴─────────────────┘

@skullpsg
Copy link

skullpsg commented May 9, 2024

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

@ericmorand
Copy link

ericmorand commented May 9, 2024

@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?

@cenfun
Copy link
Contributor

cenfun commented May 9, 2024

@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.

@ericmorand
Copy link

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

@timganter
Copy link
Author

@skullpsg sorry, no 🎲 . You can try it out on my repo here: https://github.com/timganter/c8-sub-directory?tab=readme-ov-file#steps-to-reproduce

allowExternal

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

4 participants