8000 Invalid coverage with `--experimental-monocart` when using `ts-node` · Issue #561 · bcoe/c8 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

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

Open
AriPerkkio opened this issue May 21, 2025 · 6 comments
Open

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

AriPerkkio opened this issue May 21, 2025 · 6 comments

Comments

@AriPerkkio
Copy link
Contributor

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 |                   
------------|---------|----------|---------|---------|-------------------
@AriPerkkio
Copy link
Contributor Author

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

@cenfun
Copy link
Contributor
cenfun commented May 21, 2025

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

@AriPerkkio
Copy link
Contributor Author

That example is not using c8 at all?

@cenfun
Copy link
Contributor
cenfun commented May 21, 2025

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

@cenfun
Copy link
Contributor
cenfun commented May 22, 2025

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 8C5E 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

@AriPerkkio
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0