Skip to content

doc: add note (and caveat) for mock.module about customization hooks#62075

Open
JakobJingleheimer wants to merge 1 commit intonodejs:mainfrom
JakobJingleheimer:tester/doc/no-multichain-loaders
Open

doc: add note (and caveat) for mock.module about customization hooks#62075
JakobJingleheimer wants to merge 1 commit intonodejs:mainfrom
JakobJingleheimer:tester/doc/no-multichain-loaders

Conversation

@JakobJingleheimer
Copy link
Member

@JakobJingleheimer JakobJingleheimer commented Mar 2, 2026

So for instance, if mocking a tsx module and a loader was registered via the async API (module.register(), --loader, etc), the test-runner doesn't use it and throws trying to validate format (even though it will actually work):

const { format, url } = sharedState.moduleLoader.resolveSync(caller, request);
debug('module mock, url = "%s", format = "%s", caller = "%s"', url, format, caller);
if (format) { // Format is not yet known for ambiguous files when detection is enabled.
validateOneOf(format, 'format', kSupportedFormats);

This is mainly problem within the ModuleLoader itself, which checks whether sync hooks exist; when they do, it uses that chain and ignores the potential existence of registered async hooks:

resolveSync(parentURL, request) {
const specifier = `${request.specifier}`;
const importAttributes = request.attributes ?? kEmptyObject;
if (syncResolveHooks.length) {
// Has module.registerHooks() hooks, chain the asynchronous hooks in the default step.
return resolveWithSyncHooks(specifier, parentURL, importAttributes, this.#defaultConditions,
this.#resolveAndMaybeBlockOnLoaderThread.bind(this));

The problem for the test runner was created/exposed by switching the test-runner's loader to sync registration, which ensures there's always a sync chain (causing the async chain to be ignored internally).

Geoffrey, Joyee, Matteo, and I discussed this scenario ~a year ago in Dublin. The fix is not simple.

It would need to do something like this

Leveraging async and sync hooks together, constantly jumping into and back out of the upside-down

slack convo

@JakobJingleheimer JakobJingleheimer added fast-track PRs that do not need to wait for 72 hours to land. test_runner Issues and PRs related to the test runner subsystem. labels Mar 2, 2026
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/test_runner

@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

Fast-track has been requested by @JakobJingleheimer. Please 👍 to approve.

@nodejs-github-bot nodejs-github-bot added the doc Issues and PRs related to the documentations. label Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc Issues and PRs related to the documentations. fast-track PRs that do not need to wait for 72 hours to land. test_runner Issues and PRs related to the test runner subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants