Title: node:test coverage attempts to cover mocked modules · Issue #61709 · nodejs/node · GitHub
Open Graph Title: node:test coverage attempts to cover mocked modules · Issue #61709 · nodejs/node
X Title: node:test coverage attempts to cover mocked modules · Issue #61709 · nodejs/node
Description: Version v22.22.0, v24.13.0, v25.6.0 Platform Darwin 25.2.0 Darwin Kernel Version 25.2.0: Subsystem No response What steps will reproduce the bug? I've been seeing some odd behaviour in the test coverage report when mocking modules. This ...
Open Graph Description: Version v22.22.0, v24.13.0, v25.6.0 Platform Darwin 25.2.0 Darwin Kernel Version 25.2.0: Subsystem No response What steps will reproduce the bug? I've been seeing some odd behaviour in the test cov...
X Description: Version v22.22.0, v24.13.0, v25.6.0 Platform Darwin 25.2.0 Darwin Kernel Version 25.2.0: Subsystem No response What steps will reproduce the bug? I've been seeing some odd behaviour in the test...
Opengraph URL: https://github.com/nodejs/node/issues/61709
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"node:test coverage attempts to cover mocked modules","articleBody":"### Version\n\nv22.22.0, v24.13.0, v25.6.0\n\n### Platform\n\n```text\nDarwin 25.2.0 Darwin Kernel Version 25.2.0:\n```\n\n### Subsystem\n\n_No response_\n\n### What steps will reproduce the bug?\n\nI've been seeing some odd behaviour in the test coverage report when mocking modules. This issue is very similar to #59112 however there's an edge case that was possibly missed in the fix.\n\nIf you mock a CommonJS module that's being imported into an ESM module, the coverage report includes the code from the mocked module. This is easier to demonstrate with code, [I have a reduced test case here](https://github.com/rowanmanning/node-test-coverage-issue-repro) but will talk through it.\n\nGiven these files:\n\n**`dependency.cjs`:**\n```js\nthrow new Error('This should never be called');\n\nexports.dependency = function dependency() {\n\treturn 'foo';\n}\n\n// Just here to provide some uncovered code\nexports.unused = function unused() {\n\treturn 'bar';\n}\n```\n\n**`subject.mjs`:**\n```js\nimport { dependency } from './dependency.cjs';\n\nexport function subject() {\n\treturn dependency();\n}\n```\n\nand this test file:\n\n**`test.mjs`:**\n```js\nimport assert from 'node:assert/strict';\nimport { mock, test } from 'node:test';\n\nconst dependency = mock.fn(() =\u003e 'mock-return-value');\nmock.module('./dependency.cjs', { namedExports: { dependency } });\n\nconst { subject } = await import('./subject.mjs');\n\ntest('subject calls dependency', () =\u003e {\n\tassert.equal(subject(), 'mock-return-value');\n\tassert.equal(dependency.mock.callCount(), 1);\n});\n```\n\nRunning the following test command fails because coverage does not meet the 100% target:\n\n```sh\nnode --test --experimental-test-module-mocks --experimental-test-coverage \\\n --test-coverage-branches=100 --test-coverage-functions=100 --test-coverage-lines=100 \\\n ./test.mjs\n```\n\n```\n✔ subject calls dependency (0.512ms)\nℹ tests 1\nℹ suites 0\nℹ pass 1\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 54.595125\nℹ Error: 92.86% line coverage does not meet threshold of 100%.\nℹ Error: 80.00% branch coverage does not meet threshold of 100%.\nℹ start of coverage report\nℹ -----------------------------------------------------------------\nℹ file | line % | branch % | funcs % | uncovered lines\nℹ -----------------------------------------------------------------\nℹ dependency.cjs | 88.89 | 66.67 | 100.00 | 9\nℹ subject.mjs | 100.00 | 100.00 | 100.00 |\nℹ -----------------------------------------------------------------\nℹ all files | 92.86 | 80.00 | 100.00 |\nℹ -----------------------------------------------------------------\nℹ end of coverage report\n```\n\nThe `dependency.cjs` module is definitely not imported because the error at the top of that file is never thrown as part of the test.\n\n### How often does it reproduce? Is there a required condition?\n\nSystematically\n\n### What is the expected behavior? Why is that the expected behavior?\n\nI expect to only see a module covered when it's imported directly. I expect mocked modules to not be considered part of the covered code.\n\n### What do you see instead?\n\nUnexpected lines are reported as uncovered and the coverage report includes files that were not imported as part of the test.\n\n### Additional information\n\n_No response_","author":{"url":"https://github.com/rowanmanning","@type":"Person","name":"rowanmanning"},"datePublished":"2026-02-06T15:39:10.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/61709/node/issues/61709"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:099f6809-d4d2-0b6b-07c4-0a1b1217c041 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 90C8:1FB069:E052D9:12F0C12:6A4C5E6B |
| html-safe-nonce | e1cc780919dc822069cc681131b1638bc721774853aea02af6624af79e499564 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MEM4OjFGQjA2OTpFMDUyRDk6MTJGMEMxMjo2QTRDNUU2QiIsInZpc2l0b3JfaWQiOiI5NTQ4MTAzNDU5NTEzNTQ0NzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6b42f50cbb0df356ef4a4ed59a16231bd4b17c23923f61fb3eaa7c52cc471886 |
| hovercard-subject-tag | issue:3907309896 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/61709/issue_layout |
| twitter:image | https://opengraph.githubassets.com/23e7b40429c650a4834e1699bf51d9fb06bb84aad8e8062777a4092f38faa36d/nodejs/node/issues/61709 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/23e7b40429c650a4834e1699bf51d9fb06bb84aad8e8062777a4092f38faa36d/nodejs/node/issues/61709 |
| og:image:alt | Version v22.22.0, v24.13.0, v25.6.0 Platform Darwin 25.2.0 Darwin Kernel Version 25.2.0: Subsystem No response What steps will reproduce the bug? I've been seeing some odd behaviour in the test cov... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rowanmanning |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3d11bb817438277de2a940854450e83a7d32b6aeb5014e9e6b00a6423900251c |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 14099438da5379150f15a2892474c7c7e6c0e55e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width