node/test/module-hooks/test-module-hooks-load-async-and-sync.js
Joyee Cheung 170848bc18
module: handle null source from async loader hooks in sync hooks
This relaxes the validation in sync hooks so that it accepts
the quirky nullish source returned by the default step of the
async loader when the module being loaded is CommonJS.
When there are no customization hooks registered, a saner
synchronous default load step is used to use a property
instead of a reset nullish source to signify that the module
should go through the CJS monkey patching routes and reduce
excessive reloading from disk.

PR-URL: https://github.com/nodejs/node/pull/59929
Fixes: https://github.com/nodejs/node/issues/59384
Fixes: https://github.com/nodejs/node/issues/57327
Refs: https://github.com/nodejs/node/issues/59666
Refs: https://github.com/dygabo/load_module_test
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
2025-10-14 16:28:22 +00:00

33 lines
1.3 KiB
JavaScript

'use strict';
// This tests that sync and async hooks can be mixed.
require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const fixtures = require('../common/fixtures');
const app = fixtures.path('module-hooks', 'sync-and-async', 'app.js');
const testCases = [
// When mixing sync and async hooks, the sync ones always run first.
{ preload: ['sync-customize', 'async-customize'], stdout: 'customized by sync hook' },
{ preload: ['async-customize', 'sync-customize'], stdout: 'customized by sync hook' },
// It should still work when neither hook does any customization.
{ preload: ['sync-forward', 'async-forward'], stdout: 'Hello world' },
{ preload: ['async-forward', 'sync-forward'], stdout: 'Hello world' },
// It should work when only one hook is customizing.
{ preload: ['sync-customize', 'async-forward'], stdout: 'customized by sync hook' },
{ preload: ['async-customize', 'sync-forward'], stdout: 'customized by async hook' },
];
for (const { preload, stdout } of testCases) {
const importArgs = [];
for (const p of preload) {
importArgs.push('--import', fixtures.fileURL(`module-hooks/sync-and-async/${p}.js`));
}
spawnSyncAndAssert(process.execPath, [...importArgs, app], {
stdout,
trim: true,
});
}