Skip to content

--experimental-test-coverage falsely reports missing coverage where TS source is import type #54753

Description

@jaydenseric

Version

v22.8.0

Platform

Darwin TRI-N93DLJDY6Y 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:30 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

With this TypeScript source code in src/a.mts (where the import is from doesn't matter):

import type {} from "node:assert";

console.log("Hi");

Compiling that to dist/a.mjs:

console.log("Hi");
export {};
//# sourceMappingURL=a.mjs.map

With source map dist/a.mjs.map:

{"version":3,"file":"a.mjs","sourceRoot":"","sources":["../src/a.mts"],"names":[],"mappings":"AAEA,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC"}

Then in test.mjs:

import "./dist/a.mjs";

Run:

node --experimental-test-coverage --test test.mjs

How often does it reproduce? Is there a required condition?

Every time.

What is the expected behavior? Why is that the expected behavior?

100% code coverage reported for the module src/a.mts.

What do you see instead?

Notice the false missing line coverage reported in the terminal output:

Hi
✔ test.mjs (49.142ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 53.724042
ℹ start of coverage report
ℹ ----------------------------------------------------------
ℹ file      | line % | branch % | funcs % | uncovered lines
ℹ ----------------------------------------------------------
ℹ src/a.mts |  66.67 |   100.00 |  100.00 | 1
ℹ test.mjs  | 100.00 |   100.00 |  100.00 |
ℹ ----------------------------------------------------------
ℹ all files |  75.00 |   100.00 |  100.00 |
ℹ ----------------------------------------------------------
ℹ end of coverage report

Additional information

If you comment out the import type {} from "node:assert"; and rebuild, a second run for functionally the same runtime code now correctly reports no missing coverage:

Hi
✔ test.mjs (47.03ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 51.619166
ℹ start of coverage report
ℹ ----------------------------------------------------------
ℹ file      | line % | branch % | funcs % | uncovered lines
ℹ ----------------------------------------------------------
ℹ src/a.mts | 100.00 |   100.00 |  100.00 |
ℹ test.mjs  | 100.00 |   100.00 |  100.00 |
ℹ ----------------------------------------------------------
ℹ all files | 100.00 |   100.00 |  100.00 |
ℹ ----------------------------------------------------------
ℹ end of coverage report

If you don't create source maps when compiling TypeScript modules containing import type, the Node.js test runner correctly reports no missing coverage. So the problem is around how Node.js is interpreting the source maps. Only runtime code should determine code coverage; not source code like TypeScript import type that is eliminated in the build.

Something else that is strange, is that the Node.js CLI flag --enable-source-maps doesn't seem to have an effect on how the Node.js test runner reports coverage; even without the flag it will always take into account the source maps information. Why is coverage exempt from respecting how --enable-source-maps works for other Node.js features?

Activity

  1. added
    coverageIssues and PRs related to Node.js code coverage support.
    source mapsIssues and PRs related to source map support.
    test_runnerIssues and PRs related to the test runner subsystem.
    strip-typesIssues and PRs related to TypeScript type stripping.
    on Sep 4, 2024
  2. jaydenseric commented on Sep 4, 2024

    @jaydenseric
    ContributorAuthor

    I'm not sure, but maybe inspiration can be taken from istanbuljs/v8-to-istanbul#244 .

  3. removed
    strip-typesIssues and PRs related to TypeScript type stripping.
    on Sep 5, 2024
  4. cjihrig commented on Sep 14, 2024

    @cjihrig
    Contributor

    Why is coverage exempt from respecting how --enable-source-maps works for other Node.js features?

    That seems like a bug to me. @MoLow do we need to check the flag here?

  5. MoLow commented on Sep 15, 2024

    @MoLow
    Member

    Seems like a bug to me as well. not sure where is the correct place to fix

  6. avivkeller commented on Sep 21, 2024

    @avivkeller
    Member

    (This issue isn't fixed by the linked PR #55037, I've fixed it~sorry for the confusion)

  7. avivkeller commented on Oct 9, 2024

    @avivkeller
    Member

    Hey, I'm thinking this over, and technically the import type ... is uncovered, as it's not used anywhere at Runtime. If you look at:

    {
      "version": 3,
      "sources": ["index.mts"],
      "sourcesContent": ["invalidJSSyntax;\n\nconsole.log(\"Hi\");"],
      "mappings": "AAEA,QAAQ,IAAI,IAAI",
      "names": []
    }
    
    

    invalidJSSyntax should return that it is uncovered, right? Well... import type is similar, no?

  8. 1 remaining item

  9. avivkeller commented on Oct 9, 2024

    @avivkeller
    Member

    Reopening due to #55339. I've removed confirmed-bug Issues and PRs for confirmed bugs. as the "fix" for this issue, that is, not checking coverage on un-transpiled lines, doesn't seem to be viable.

    IMHO this is expected behavior, as, while annoying, this line is technically not covered.

  10. github-actions commented on Apr 28, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  11. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 28, 2026
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

    coverageIssues and PRs related to Node.js code coverage support.source mapsIssues and PRs related to source map support.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions