esmock: Breakage in Node.js v20.6.0

The new loader API in v20.6.0 broke esmock


   FAIL  "should return un-mocked file"
    Cannot find package '[object Object]?esmk=1' imported from /Users/matteo/Repositories/esmock/src/esmock.js

    at nextResolveCall (file:///Users/matteo/Repositories/esmock/src/esmockLoader.js:71:7)
    at resolve (file:///Users/matteo/Repositories/esmock/src/esmockLoader.js:78:12)

I did try a quick replacement with the new initialize, but it looked a bit harder than expected.

cc @GeoffreyBooth for visibility.

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Reactions: 2
  • Comments: 18 (13 by maintainers)

Most upvoted comments

the native resolve function is broken in node 20.6

With --experimental-import-meta-resolve flag?

@koshic thanks for finding the cause and writing the nice summary. I understand.

Another interesting point is https://github.com/nodejs/node/pull/49028

“Unflags import.meta.resolve making the second parentURL argument still gated behind the same --experimental-import-meta-resolve flag so that existing users would retain backwards compatibility, while the default behaviour becomes standards compliant.”

So we have 2 different behaviors of import.meta.resolve depending on experimental flag. And import.meta.resolve is always available.

I can successfully run a few tests with ‘–experimental-import-meta-resolve’ and dirty patched resolve: image

But there is a new error: image

issue comes from here: image

esmockModule -> moduleId, parent ../local/usesImportObjects.js file:///xxxxxx/esmock/tests/tests-node/esmock.node.global.test.js
esmockModule -> moduleFileURL {}
esmockModule -> moduleFileURL to string [object Object]