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

Can't use with --experimental-test-coverage --test #437

Closed
4 of 6 tasks
gjuchault opened this issue Dec 13, 2023 · 12 comments
Closed
4 of 6 tasks

Can't use with --experimental-test-coverage --test #437

gjuchault opened this issue Dec 13, 2023 · 12 comments
Labels

Comments

@gjuchault
Copy link

gjuchault commented Dec 13, 2023

Acknowledgements

  • I searched existing issues before opening this one to avoid duplicates
  • I understand this is not a place for seek help, but to report a bug
  • I understand that the bug must be proven first with a minimal reproduction
  • I will be polite, respectful, and considerate of people's time and effort

Minimal reproduction URL

https://codesandbox.io/p/devbox/tsx-import-coverage-dkkgs3

I had to use codesandbox to have recent node version, you need local reproduction:

package.json
{
  "name": "nodejs-sandbox",
  "scripts": {
    "start": "npm run test-a || npm run test-b || npm run test-c || npm run test-d",
    "test-a": "tsx --experimental-test-coverage --test index.test.ts",
    "test-b": "NODE_OPTIONS='--import tsx' node --experimental-test-coverage --test index.test.ts",
    "test-c": "NODE_V8_COVERAGE=./coverage c8 -r html tsx --experimental-test-coverage --test index.test.ts",
    "test-d": "NODE_OPTIONS='--import tsx' NODE_V8_COVERAGE=./coverage c8 -r html node --experimental-test-coverage --test index.test.ts"
  },
  "devDependencies": {
    "c8": "^8.0.1",
    "tsx": "^4.6.2"
  }
}
index.test.ts
import { describe, it } from "node:test";
import * as assert from "node:assert/strict";

describe("case", () => {
  it("works", () => {
    assert.equal(1, 1);
  });
});

Version

4.6.2

Node.js version

v20+

Package manager

npm

Operating system

Linux

Problem & Expected behavior

I'm trying to get tsx to work with the native test runner, coverage included
It seems to be failing with the following warning:

ℹ Warning: Could not report code coverage. TypeError: Cannot read properties of undefined (reading 'line')

Also, c8 is not able to generate any report:

Unknown%

Contributions

  • I plan to open a pull request for this issue
  • I plan to make a financial contribution to this project
@gjuchault gjuchault added bug Something isn't working pending triage labels Dec 13, 2023
@privatenumber
Copy link
Owner

Is this a duplicate of #433?

@gjuchault
Copy link
Author

gjuchault commented Dec 14, 2023

Sorry for not spotting this issue earlier, I might have added a comment on it.
However, I tried with tsx@4.2.1 (pinned) as suggested by the author of #433 and I still get the same ℹ Warning: Could not report code coverage. TypeError: Cannot read properties of undefined (reading 'line') and no coverage, so I'm not sure it's exactly the same error

@jbergstroem

This comment has been minimized.

@gjuchault
Copy link
Author

gjuchault commented Jan 31, 2024

@privatenumber I tried the solution you suggested in #433 locally and ran npm link to try on my reproduction case. It still didn't run

Edit: also I've been running the code with Node v21.x where the condition is supposed to be true (process.setSourceMapsEnabled is defined and Error.prepareStackTrace is undefined)

@gjuchault
Copy link
Author

Should be fixed by Node 22 since they merged this

@SuperchupuDev
Copy link
Contributor

SuperchupuDev commented Apr 24, 2024

updating to node 22 doesn't solve the issue for me
image

@gjuchault

This comment was marked as off-topic.

@khaosdoctor
Copy link

Node team has addressed that in nodejs/node#52775 to remove the error, the coverage though is still useless.

One thing that we saw is that it probably has to do with the source map generation, but I haven't yet tested it

@privatenumber
Copy link
Owner

I left a comment in nodejs/node#52775 (comment), but tsx has source maps with V8 coverage support (released 5 days ago in v4.10.4).

I tested the minimal reproduction, and using c8 without --experimental-test-coverage works:

$ npx c8 -r html tsx --test index.test.ts
▶ case
  ✔ works (0.338ms)
▶ case (0.748625ms)
ℹ tests 1
ℹ suites 1
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 203.282667

Verified coverage is working with:

$ open coverage/index.html

But this doesn't with --experimental-test-coverage:

$ npx c8 -r html tsx --experimental-test-coverage --test index.test.ts
▶ case
  ✔ works (0.342167ms)
▶ case (0.768417ms)
ℹ Warning: Could not report code coverage. TypeError: Cannot read properties of undefined (reading 'line')
ℹ tests 1
ℹ suites 1
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 138.662125

IMO this is an issue blocked by Node.js

@khaosdoctor

This comment has been minimized.

@SuperchupuDev
Copy link
Contributor

works for me now using node 22.3.0, looks like it was a node issue fixed by nodejs/node#53315 which was just shipped

@privatenumber
Copy link
Owner

Thanks.

Confirmed working with Node v22.3.0:

➜  sandbox (1) npx c8 -r html tsx --experimental-test-coverage --test index.test.ts
▶ case
  ✔ works (0.363709ms)
▶ case (0.820125ms)
ℹ tests 1
ℹ suites 1
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 296.576208
ℹ start of coverage report
ℹ --------------------------------------------------------------
ℹ file          | line % | branch % | funcs % | uncovered lines
ℹ --------------------------------------------------------------
ℹ index.test.ts | 100.00 |   100.00 |  100.00 |
ℹ index.ts      |  33.33 |   100.00 |  100.00 | 2-3
ℹ --------------------------------------------------------------
ℹ all files     |  83.33 |   100.00 |  100.00 |
ℹ --------------------------------------------------------------
ℹ end of coverage report

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants