Skip to content

Invalid coverage with --experimental-monocart when using ts-node #561

Open

Description

@AriPerkkio

All metrics of coverage report are incorrectly set to 100% when using --experimental-monocart with ts-node.

Repro, make sure you download the project locally as WebContainers do not support V8 coverage/CDP: https://stackblitz.com/~/edit/c8-ts-node-invalid-monocart

$ pnpm test

------------|---------|----------|---------|---------|-------------------
File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------|---------|----------|---------|---------|-------------------
All files   |   76.47 |       25 |     100 |   76.47 |                   
 example.ts |   76.47 |       25 |     100 |   76.47 | 3-4,13-14         
------------|---------|----------|---------|---------|-------------------

$ pnpm test:monocart

------------|---------|----------|---------|---------|-------------------
File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------|---------|----------|---------|---------|-------------------
All files   |     100 |      100 |     100 |     100 |                   
 example.ts |     100 |      100 |     100 |     100 |                   
------------|---------|----------|---------|---------|-------------------

Activity

  1. AriPerkkio commented on May 21, 2025

    @AriPerkkio
    ContributorAuthor

    I guess this is because runtime JS files are transformed on the fly, and Monocart does not have access to those? c8 would require a loader or require hook in order to capture all files that user executed? cc. @cenfun

  2. cenfun commented on May 21, 2025

    @cenfun
    Contributor

    I thought it should be the issue with the sourcemap.
    I used your example and it works with mcr CLI, see here (branch is example)

    npm run test
    
    > mcr-ts-node@1.0.0 test
    > cross-env NODE_OPTIONS="--loader ts-node/esm --no-warnings" npx mcr ts-node ./test/example.test.ts -o coverage-reports/ts
    
    [MCR] Execute: ts-node ./test/example.test.ts
    [MCR] Loaded: mcr.config.ts
    hello
    [MCR] Coverage Report
    ┌──────────────┬─────────┬────────────┬──────────┬───────────┬─────────┬─────────────────┐
    │ Name         │   Bytes │ Statements │ Branches │ Functions │   Lines │ Uncovered Lines │
    ├──────────────┼─────────┼────────────┼──────────┼───────────┼─────────┼─────────────────┤
    │ src          │         │            │          │           │         │                 │
    │ └ example.ts │ 79.28 % │    75.00 % │  62.50 % │  100.00 % │ 53.85 % │ 2-4,12-14       │
    ├──────────────┼─────────┼────────────┼──────────┼───────────┼─────────┼─────────────────┤
    │ Summary      │ 79.28 % │    75.00 % │  62.50 % │  100.00 % │ 53.85 % │                 │
    └──────────────┴─────────┴────────────┴──────────┴───────────┴─────────┴─────────────────┘

    But I haven't figured out why c8 isn't working. The differences:
    1, tsconfig.json: https://github.com/cenfun/mcr-ts-node/blob/example/tsconfig.json
    2, cross-env NODE_OPTIONS="--loader ts-node/esm --no-warnings"
    3, package.json { "type": "module" }
    4, CLI: mcr

  3. AriPerkkio commented on May 21, 2025

    @AriPerkkio
    ContributorAuthor

    That example is not using c8 at all?

  4. cenfun commented on May 21, 2025

    @cenfun
    Contributor

    yes, I haven't figured out why c8 isn't working

  5. cenfun commented on May 22, 2025

    @cenfun
    Contributor

    I have found that the issue is related to the generated source code.

    when using c8 + ts-node, we can not get the generated source code, but there is only a fake code with lineLengths of source-map-cache, see https://nodejs.org/docs/latest/api/cli.html#source-map-cache and nodejs/node#48460
    It said, the sourcemap contains the original code, but it does not have the generated code, instead, it has lineLengths only.
    So Monocart cannot get the precise location by parsing the AST of the generated code.

    Image

    In order to get the generated code, mcr CLI uses Node.js customization-hooks to save the generated code in some cases. (It doesn't always succeed, for example, it fails when using tsx)
    see https://github.com/cenfun/monocart-coverage-reports/blob/main/lib/register/hooks.js
    Image

  6. AriPerkkio commented on May 22, 2025

    @AriPerkkio
    ContributorAuthor

    Yep, that's exactly what I meant by #561 (comment).

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