-
-
Notifications
You must be signed in to change notification settings - Fork 35k
Description
What is the problem this feature will solve?
Right now, Module.syncBuiltinESMExports() is a manual process. It’s a bit of a pain because if you forget to call it after changing a built-in's exports (like during mocking), the ESM imports don't actually update.
I just ran into this while working on a fix for Issue #62081 mocking node:timers/promises only worked because we manually triggered this sync. It’s very easy to miss, and it makes the native mocking system feel a bit brittle because every new test feature has to "remember" to pull this lever.
What is the feature you are proposing to solve the problem?
I think we should look into making these ESM built-in facades stay in sync automatically. Instead of having to call a sync function everywhere, maybe we could use something like a Proxy or dynamic getters on the ModuleWrap so that the ESM side always sees the latest CJS state.
Even just adding an internal observer that triggers the existing sync logic whenever those core exports objects are mutated would be a huge win. It would make the native mocking (and APM tools) a lot more reliable without developers needing to know about the sync utility.
What alternatives have you considered?
The only real alternative is sticking with the manual calls we have now, but it’s just too error-prone. We could also look at moving more of the resolution logic into C++ to "harden" it, but automating the current JS synchronization seems like a much simpler and more direct way to solve the immediate problem.