node/test/module-hooks/test-module-hooks-load-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

30 lines
953 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { registerHooks } = require('module');
// This tests that required builtins get null as source from default
// step, and the source returned are ignored.
// TODO(joyeecheung): this is to align with the module.register() behavior
// but perhaps the load hooks should not be invoked for builtins at all.
// Pick a builtin that's unlikely to be loaded already - like zlib.
assert(!process.moduleLoadList.includes('NativeModule zlib'));
const hook = registerHooks({
load: common.mustCall(function load(url, context, nextLoad) {
assert.strictEqual(url, 'node:zlib');
const result = nextLoad(url, context);
assert.strictEqual(result.source, null);
return {
source: 'throw new Error("I should not be thrown")',
format: 'builtin',
};
}),
});
assert.strictEqual(typeof require('zlib').createGzip, 'function');
hook.deregister();