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

c8 CLI doesn't work properly when executed in a sub-directory of the project #498

Open
ericmorand opened this issue Oct 31, 2023 · 3 comments

Comments

@ericmorand
Copy link

ericmorand commented Oct 31, 2023

  • Version: 8.0.1
  • Platform: Ubuntu 20.04 + Node.js 16.20.2

Consider the following project:

src/index.ts

export const foo = (bar: number) => {
    return bar + 1;
};

test/index.ts

import assert from "assert";
import {foo} from "../src";

assert(foo(5) === 6);

Depending on where c8 is executed, the result is quite different:

Executed from the root of the project

$ c8 ts-node test/index.ts 

=============================== Coverage summary ===============================
Statements   : 100% ( 3/3 )
Branches     : 100% ( 1/1 )
Functions    : 100% ( 1/1 )
Lines        : 100% ( 3/3 )
================================================================================

Executed from the place where the test file is (i.e. test/)

$ cd test/
$ c8 ts-node index.ts 

=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================

For information - and maybe it can help - here is the .nycrc.json that I use:

{
  "all": true,
  "lines": 100,
  "branches": 100,
  "functions": 100,
  "extension": [
    ".ts"
  ],
  "include": [
    "src"
  ],
  "exclude": [],
  "reporter": [
    "text-summary",
    "html"
  ]
}

Let me also add that nyc works perfectly regardless of where it is executed - i.e. replace c8 with nyc in the showcased shell commands and the report is perfect in both case. We educated all our developers too execute coverage as soon as possible and on local tests, and unfortunately c8 not supporting this pattern means that we can't migrate to it from nyc.

Hopefully this is solvable.

@skullpsg
Copy link

skullpsg commented May 9, 2024

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

@ericmorand
Copy link
Author

I'll try but I think it won't: c8 resolves the src reference from the working directory and will then expect that the files exist in test/src.

ericmorand added a commit to ericmorand/c8 that referenced this issue Jun 23, 2024
ericmorand added a commit to ericmorand/c8 that referenced this issue Jun 23, 2024
@ericmorand
Copy link
Author

For information, after working on PR #539 and seeing how fixing the current issue would break c8, I created a code coverage tool from scratch that is not affected by the issue : https://www.npmjs.com/package/one-double-zero. Please give it a try if you are affected by the current issue.

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

2 participants