node/test/parallel/test-module-prototype-mutation.js
RafaelGSS fbdfe9399c lib: add diagnostics_channel events to module loading
This commit adds a tracing channel for module loading
through `import()` and `require()`.

Co-Authored-By: Stephen Belanger <admin@stephenbelanger.com>
PR-URL: https://github.com/nodejs/node/pull/44340
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
2024-06-20 21:25:04 -03:00

53 lines
1.8 KiB
JavaScript

'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const { Channel } = require('diagnostics_channel');
const assert = require('assert');
Object.defineProperty(Object.prototype, 'name', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.name'),
set: function(v) {
// A diagnostic_channel is created to track module loading
// when using `require` or `import`. This class contains a
// `name` property that would cause a false alert for this
// test case. See Channel.prototype.name.
if (!(this instanceof Channel)) {
common.mustNotCall('set %Object.prototype%.name')(v);
}
},
enumerable: false,
});
Object.defineProperty(Object.prototype, 'main', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.main'),
set: common.mustNotCall('set %Object.prototype%.main'),
enumerable: false,
});
Object.defineProperty(Object.prototype, 'type', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.type'),
set: common.mustNotCall('set %Object.prototype%.type'),
enumerable: false,
});
Object.defineProperty(Object.prototype, 'exports', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.exports'),
set: common.mustNotCall('set %Object.prototype%.exports'),
enumerable: false,
});
Object.defineProperty(Object.prototype, 'imports', {
__proto__: null,
get: common.mustNotCall('get %Object.prototype%.imports'),
set: common.mustNotCall('set %Object.prototype%.imports'),
enumerable: false,
});
assert.strictEqual(
require(fixtures.path('es-module-specifiers', 'node_modules', 'no-main-field')),
'no main field'
);
import(fixtures.fileURL('es-module-specifiers', 'index.mjs'))
.then(common.mustCall((module) => assert.strictEqual(module.noMain, 'no main field')));