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

Error when mocking core modules with the node: prefix (minor annoyance) #115

Closed
gmahomarf opened this issue Aug 15, 2022 · 3 comments
Closed

Comments

@gmahomarf
Copy link

Context:

// someModule.js

import path from 'node:path';

...

Trying to do something like the following:

const someModule = await esmock('./someModule.js', {
    'node:path': pathMock,
});

Results in the following error:

Error: not a valid path: "node:path"

This works without changing someModule.js:

const someModule = await esmock('./someModule.js', {
    'path': pathMock,
});

However, now the module URLs in the tests and the original module are different. I've opened a PR in iambumblehead/resolvewithplus#27 to add support for the node: prefix.

@iambumblehead
Copy link
Owner

iambumblehead commented Aug 15, 2022

@gmahomarf thanks for the great PR which will be published soonish #116

This temporary work-around was needed for now. The long-term solution is for resolvewithplus to return the final path always with the protocol like import.meta.resolve would. Probably resolvewithplus can be updated before next-week,

diff --git a/src/esmockModule.js b/src/esmockModule.js
index 1df6117..297a9ae 100644
--- a/src/esmockModule.js
+++ b/src/esmockModule.js
@@ -18,7 +18,9 @@ const FILE_PROTOCOL = 'file:///'
 // https://url.spec.whatwg.org/, eg, file:///C:/demo file:///root/linux/path
 const pathAddProtocol = (pathFull, protocol) => {
   if (!protocol)
-    protocol = !resolvewith.iscoremodule(pathFull) ? FILE_PROTOCOL : 'node:'
+    protocol = /^node:/.test(pathFull)
+      ? ''
+      : !resolvewith.iscoremodule(pathFull) ? FILE_PROTOCOL : 'node:'
   if (protocol.includes(FILE_PROTOCOL))
     pathFull = fs.realpathSync.native(pathFull)
   if (process.platform === 'win32')

@iambumblehead
Copy link
Owner

it is published to [email protected]

@iambumblehead
Copy link
Owner

this seems to be resolved but feel free to reopen if necessary

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