Skip to content

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

Description

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

Activity

  1. skullpsg commented on May 9, 2024

    @skullpsg

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

  2. ericmorand commented on May 9, 2024

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

  3. ericmorand commented on Jul 25, 2024

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

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