Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b24fc91

Browse files
committedJul 15, 2023
Put globalPreload tests on its own section
1 parent ae5cf1d commit b24fc91

File tree

2 files changed

+110
-50
lines changed

2 files changed

+110
-50
lines changed
 

‎lib/internal/modules/esm/hooks.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
ERR_INVALID_RETURN_PROPERTY_VALUE,
3232
ERR_INVALID_RETURN_VALUE,
3333
ERR_LOADER_CHAIN_INCOMPLETE,
34+
ERR_UNKNOWN_BUILTIN_MODULE,
3435
ERR_WORKER_UNSERIALIZABLE_ERROR,
3536
} = require('internal/errors').codes;
3637
const { exitCodes: { kUnfinishedTopLevelAwait } } = internalBinding('errors');
@@ -653,13 +654,13 @@ class HooksProxy {
653654
(builtinName) => {
654655
if (StringPrototypeStartsWith(builtinName, 'node:')) {
655656
builtinName = StringPrototypeSlice(builtinName, 5);
656-
} else if (BuiltinModule.canBeRequiredWithoutScheme(builtinName)) {
657-
throw new ERR_INVALID_ARG_VALUE('builtinName', builtinName);
657+
} else if (!BuiltinModule.canBeRequiredWithoutScheme(builtinName)) {
658+
throw new ERR_UNKNOWN_BUILTIN_MODULE(builtinName);
658659
}
659660
if (BuiltinModule.canBeRequiredByUsers(builtinName)) {
660661
return require(builtinName);
661662
}
662-
throw new ERR_INVALID_ARG_VALUE('builtinName', builtinName);
663+
throw new ERR_UNKNOWN_BUILTIN_MODULE(builtinName);
663664
},
664665
// Param port
665666
port,

‎test/es-module/test-esm-loader-hooks.mjs

+106-47
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { spawnPromisified } from '../common/index.mjs';
22
import * as fixtures from '../common/fixtures.mjs';
33
import assert from 'node:assert';
4+
import os from 'node:os';
45
import { execPath } from 'node:process';
56
import { describe, it } from 'node:test';
67

@@ -422,60 +423,118 @@ describe('Loader hooks', { concurrency: true }, () => {
422423
});
423424
});
424425

425-
it('should handle globalPreload returning undefined', async () => {
426-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
427-
'--no-warnings',
428-
'--experimental-loader',
429-
'data:text/javascript,export function globalPreload(){}',
430-
fixtures.path('empty.js'),
431-
]);
426+
describe('globalPreload', () => {
427+
it('should handle globalPreload returning undefined', async () => {
428+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
429+
'--no-warnings',
430+
'--experimental-loader',
431+
'data:text/javascript,export function globalPreload(){}',
432+
fixtures.path('empty.js'),
433+
]);
432434

433-
assert.strictEqual(stderr, '');
434-
assert.strictEqual(stdout, '');
435-
assert.strictEqual(code, 0);
436-
assert.strictEqual(signal, null);
437-
});
435+
assert.strictEqual(stderr, '');
436+
assert.strictEqual(stdout, '');
437+
assert.strictEqual(code, 0);
438+
assert.strictEqual(signal, null);
439+
});
438440

439-
it('should register globals set from globalPreload', async () => {
440-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
441-
'--no-warnings',
442-
'--experimental-loader',
443-
'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}',
444-
'--print', 'myGlobal',
445-
]);
441+
it('should handle loading node:test', async () => {
442+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
443+
'--no-warnings',
444+
'--experimental-loader',
445+
'data:text/javascript,export function globalPreload(){return `getBuiltin("node:test")()`}',
446+
fixtures.path('empty.js'),
447+
]);
446448

447-
assert.strictEqual(stderr, '');
448-
assert.strictEqual(stdout.trim(), '4');
449-
assert.strictEqual(code, 0);
450-
assert.strictEqual(signal, null);
451-
});
449+
assert.strictEqual(stderr, '');
450+
assert.match(stdout, /\n# pass 1\r?\n/);
451+
assert.strictEqual(code, 0);
452+
assert.strictEqual(signal, null);
453+
});
452454

453-
it('should log console.log calls returned from globalPreload', async () => {
454-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
455-
'--no-warnings',
456-
'--experimental-loader',
457-
'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}',
458-
fixtures.path('empty.js'),
459-
]);
455+
it('should handle loading node:os with node: prefix', async () => {
456+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
457+
'--no-warnings',
458+
'--experimental-loader',
459+
'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("node:os").arch())`}',
460+
fixtures.path('empty.js'),
461+
]);
460462

461-
assert.strictEqual(stderr, '');
462-
assert.strictEqual(stdout.trim(), 'Hello from globalPreload');
463-
assert.strictEqual(code, 0);
464-
assert.strictEqual(signal, null);
465-
});
463+
assert.strictEqual(stderr, '');
464+
assert.strictEqual(stdout.trim(), os.arch());
465+
assert.strictEqual(code, 0);
466+
assert.strictEqual(signal, null);
467+
});
466468

467-
it('should crash if globalPreload returns code that throws', async () => {
468-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
469-
'--no-warnings',
470-
'--experimental-loader',
471-
'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}',
472-
fixtures.path('empty.js'),
473-
]);
469+
it('should handle loading node:os without node: prefix', async () => {
470+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
471+
'--no-warnings',
472+
'--experimental-loader',
473+
'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("os").arch())`}',
474+
fixtures.path('empty.js'),
475+
]);
474476

475-
assert.match(stderr, /error from globalPreload/);
476-
assert.strictEqual(stdout, '');
477-
assert.strictEqual(code, 1);
478-
assert.strictEqual(signal, null);
477+
assert.strictEqual(stderr, '');
478+
assert.strictEqual(stdout.trim(), os.arch());
479+
assert.strictEqual(code, 0);
480+
assert.strictEqual(signal, null);
481+
});
482+
483+
it('should throw when loading node:test without node: prefix', async () => {
484+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
485+
'--no-warnings',
486+
'--experimental-loader',
487+
'data:text/javascript,export function globalPreload(){return `getBuiltin("test")()`}',
488+
fixtures.path('empty.js'),
489+
]);
490+
491+
assert.match(stderr, /ERR_UNKNOWN_BUILTIN_MODULE/);
492+
assert.strictEqual(stdout, '');
493+
assert.strictEqual(code, 1);
494+
assert.strictEqual(signal, null);
495+
});
496+
497+
it('should register globals set from globalPreload', async () => {
498+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
499+
'--no-warnings',
500+
'--experimental-loader',
501+
'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}',
502+
'--print', 'myGlobal',
503+
]);
504+
505+
assert.strictEqual(stderr, '');
506+
assert.strictEqual(stdout.trim(), '4');
507+
assert.strictEqual(code, 0);
508+
assert.strictEqual(signal, null);
509+
});
510+
511+
it('should log console.log calls returned from globalPreload', async () => {
512+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
513+
'--no-warnings',
514+
'--experimental-loader',
515+
'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}',
516+
fixtures.path('empty.js'),
517+
]);
518+
519+
assert.strictEqual(stderr, '');
520+
assert.strictEqual(stdout.trim(), 'Hello from globalPreload');
521+
assert.strictEqual(code, 0);
522+
assert.strictEqual(signal, null);
523+
});
524+
525+
it('should crash if globalPreload returns code that throws', async () => {
526+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
527+
'--no-warnings',
528+
'--experimental-loader',
529+
'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}',
530+
fixtures.path('empty.js'),
531+
]);
532+
533+
assert.match(stderr, /error from globalPreload/);
534+
assert.strictEqual(stdout, '');
535+
assert.strictEqual(code, 1);
536+
assert.strictEqual(signal, null);
537+
});
479538
});
480539

481540
it('should be fine to call `process.removeAllListeners("beforeExit")` from the main thread', async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.