c8 icon indicating copy to clipboard operation
c8 copied to clipboard

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

Open timganter opened this issue 1 year ago • 9 comments

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

  • I have created this repo that reproduces the issue. https://github.com/timganter/c8-sub-directory
  • Am I just configuring c8 incorrectly 🤞 ? Here's my config.
  • Or is this a bug?
  • Here is a video showing the issue that can be reproduced using this repo.

https://github.com/bcoe/c8/assets/1911000/e871d7cd-9e92-492e-b178-a81ffe2d4b18

timganter avatar May 04 '24 00:05 timganter

I have the same issue: https://github.com/bcoe/c8/issues/498

ericmorand avatar May 07 '24 07:05 ericmorand

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

cenfun avatar May 08 '24 14:05 cenfun

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.

ericmorand avatar May 08 '24 17:05 ericmorand

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

cenfun avatar May 09 '24 01:05 cenfun

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

skullpsg avatar May 09 '24 05:05 skullpsg

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

ericmorand avatar May 09 '24 09:05 ericmorand

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

cenfun avatar May 09 '24 09:05 cenfun

I opened an issue in your repository to move the conversation there: https://github.com/cenfun/monocart-coverage-reports/issues/22

ericmorand avatar May 09 '24 10:05 ericmorand

@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

timganter avatar May 13 '24 17:05 timganter