Title: test-runner: `require`ing a mocked dual-package gets the original instead of the mock · Issue #58231 · nodejs/node · GitHub
Open Graph Title: test-runner: `require`ing a mocked dual-package gets the original instead of the mock · Issue #58231 · nodejs/node
X Title: test-runner: `require`ing a mocked dual-package gets the original instead of the mock · Issue #58231 · nodejs/node
Description: Version 22.15 Platform Darwin MacBook-Pro.local 24.4.0 Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:47 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6000 arm64 Subsystem node:mock What steps will reproduce the bug? The issue is fo...
Open Graph Description: Version 22.15 Platform Darwin MacBook-Pro.local 24.4.0 Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:47 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6000 arm64 Subsystem node:mock What steps...
X Description: Version 22.15 Platform Darwin MacBook-Pro.local 24.4.0 Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:47 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6000 arm64 Subsystem node:mock What steps...
Opengraph URL: https://github.com/nodejs/node/issues/58231
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"test-runner: `require`ing a mocked dual-package gets the original instead of the mock","articleBody":"### Version\n\n22.15\n\n### Platform\n\n```text\nDarwin MacBook-Pro.local 24.4.0 Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:47 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6000 arm64\n```\n\n### Subsystem\n\nnode:mock\n\n### What steps will reproduce the bug?\n\n\nThe issue is for packages that expose esm `module` and cjs `main`. See Repo: https://github.com/sloops77/node-mock-bug for more examples\n\nRather than create my own package the examples below use the `nanoid` 3.x package. The linked repo also shows that it is reproducible for the ethers package (which also exposes `module` and `main`).\n\n```json\n// snippet of package.json from nanoid\n{\n \"name\": \"nanoid\",\n \"version\": \"3.3.11\",\n \"description\": \"A tiny (116 bytes), secure URL-friendly unique string ID generator\",\n \"type\": \"module\",\n \"main\": \"index.cjs\",\n \"module\": \"index.js\",\n ...\n```\n\n```js\n// test.js\nconst {describe, it, mock} = require(\"node:test\");\nconst assert = require(\"node:assert\");\n\nmock.module('nanoid', {\n namedExports: {\n nanoid() {\n return '1234'\n }\n },\n});\n\n\ntest('mocking problem in cjs', () => {\n const {nanoid} = require('nanoid')\n assert.strictEqual(nanoid(), '1234'); // fails test because cjs is not mocked\n})\n\ntest('mocking works for esm', async () => {\n const {nanoid} = await import('nanoid')\n assert.strictEqual(nanoid(), '1234'); // works as it should\n})\n\n```\n\n\n### How often does it reproduce? Is there a required condition?\n\n1. For imported packages it seems 100% consistent. It also occurs for a module mock that uses `defaultExport`.\n2. Node core modules do not reproduce this issue\n\n### What is the expected behavior? Why is that the expected behavior?\n\nCJS & MJS files should receive a mocked module\n\n### What do you see instead?\n\nCJS files receive the original module, not the mock. MJS files are fine as they receive the mock.\n\n### Additional information\n\n_No response_","author":{"url":"https://github.com/sloops77","@type":"Person","name":"sloops77"},"datePublished":"2025-05-08T11:30:49Z","url":"https://github.com/nodejs/node/issues/58231"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:16144e33-9375-5dec-626f-22cc76a2a61b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DADE:20A3B4:55BCD7:7523EB:6AED5BC8 |
| html-safe-nonce | 7f5fb98b379bab9e4dd2e4124ed433420cb736e14c1f838a4dad5062c9780ddf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQURFOjIwQTNCNDo1NUJDRDc6NzUyM0VCOjZBRUQ1QkM4IiwidmlzaXRvcl9pZCI6Ijg1NDAwNjAwMTE5MTg2MTM0NDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fbf6ad44bd620bdba34b1d8926d5a8349ce9df6b5d0bae3ad15af59dc83bda4c |
| hovercard-subject-tag | issue:3048706674 |
| 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/58231/issue_layout |
| twitter:image | https://opengraph.githubassets.com/552d816fe304d9f06a1ba16919a6bbad4fd58208a7fb5fa2a06cd6f03e16a6d6/nodejs/node/issues/58231 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/552d816fe304d9f06a1ba16919a6bbad4fd58208a7fb5fa2a06cd6f03e16a6d6/nodejs/node/issues/58231 |
| og:image:alt | Version 22.15 Platform Darwin MacBook-Pro.local 24.4.0 Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:47 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6000 arm64 Subsystem node:mock What steps... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sloops77 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 56fb42677ae87ed3e64410323d67980aa65d0485568a7858f874e7edfc6d1c11 |
| 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 | 9c9a37e2423ed382cf39f70b5aa4b4858b40a3b0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width