node/test/module-hooks/test-module-hooks-resolve-builtin-builtin-require.js
Joyee Cheung e85964610c module: implement module.registerHooks()
PR-URL: https://github.com/nodejs/node/pull/55698
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
2024-12-09 23:27:08 +00:00

27 lines
679 B
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const { registerHooks } = require('module');
// This tests that builtins can be redirected to another builtin.
// Pick a builtin that's unlikely to be loaded already - like zlib.
assert(!process.moduleLoadList.includes('NativeModule zlib'));
const hook = registerHooks({
resolve(specifier, context, nextLoad) {
if (specifier === 'assert') {
return {
url: 'node:zlib',
shortCircuit: true,
};
}
},
});
// Check assert, which is already loaded.
// zlib.createGzip is a function.
assert.strictEqual(typeof require('assert').createGzip, 'function');
hook.deregister();