Skip to content

Commit 479a50c

Browse files
test: isolate globalPreload tests
PR-URL: #49545 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent b500037 commit 479a50c

File tree

3 files changed

+164
-132
lines changed

3 files changed

+164
-132
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { spawnPromisified } from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import assert from 'node:assert';
4+
import os from 'node:os';
5+
import { execPath } from 'node:process';
6+
import { describe, it } from 'node:test';
7+
8+
describe('globalPreload hook', () => {
9+
it('should not emit deprecation warning when initialize is supplied', async () => {
10+
const { stderr } = await spawnPromisified(execPath, [
11+
'--experimental-loader',
12+
'data:text/javascript,export function globalPreload(){}export function initialize(){}',
13+
fixtures.path('empty.js'),
14+
]);
15+
16+
assert.doesNotMatch(stderr, /`globalPreload` is an experimental feature/);
17+
});
18+
19+
it('should handle globalPreload returning undefined', async () => {
20+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
21+
'--no-warnings',
22+
'--experimental-loader',
23+
'data:text/javascript,export function globalPreload(){}',
24+
fixtures.path('empty.js'),
25+
]);
26+
27+
assert.strictEqual(stderr, '');
28+
assert.strictEqual(stdout, '');
29+
assert.strictEqual(code, 0);
30+
assert.strictEqual(signal, null);
31+
});
32+
33+
it('should handle loading node:test', async () => {
34+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
35+
'--no-warnings',
36+
'--experimental-loader',
37+
'data:text/javascript,export function globalPreload(){return `getBuiltin("node:test")()`}',
38+
fixtures.path('empty.js'),
39+
]);
40+
41+
assert.strictEqual(stderr, '');
42+
assert.match(stdout, /\n# pass 1\r?\n/);
43+
assert.strictEqual(code, 0);
44+
assert.strictEqual(signal, null);
45+
});
46+
47+
it('should handle loading node:os with node: prefix', async () => {
48+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
49+
'--no-warnings',
50+
'--experimental-loader',
51+
'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("node:os").arch())`}',
52+
fixtures.path('empty.js'),
53+
]);
54+
55+
assert.strictEqual(stderr, '');
56+
assert.strictEqual(stdout.trim(), os.arch());
57+
assert.strictEqual(code, 0);
58+
assert.strictEqual(signal, null);
59+
});
60+
61+
// `os` is used here because it's simple and not mocked (the builtin module otherwise doesn't matter).
62+
it('should handle loading builtin module without node: prefix', async () => {
63+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
64+
'--no-warnings',
65+
'--experimental-loader',
66+
'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("os").arch())`}',
67+
fixtures.path('empty.js'),
68+
]);
69+
70+
assert.strictEqual(stderr, '');
71+
assert.strictEqual(stdout.trim(), os.arch());
72+
assert.strictEqual(code, 0);
73+
assert.strictEqual(signal, null);
74+
});
75+
76+
it('should throw when loading node:test without node: prefix', async () => {
77+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
78+
'--no-warnings',
79+
'--experimental-loader',
80+
'data:text/javascript,export function globalPreload(){return `getBuiltin("test")()`}',
81+
fixtures.path('empty.js'),
82+
]);
83+
84+
assert.match(stderr, /ERR_UNKNOWN_BUILTIN_MODULE/);
85+
assert.strictEqual(stdout, '');
86+
assert.strictEqual(code, 1);
87+
assert.strictEqual(signal, null);
88+
});
89+
90+
it('should register globals set from globalPreload', async () => {
91+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
92+
'--no-warnings',
93+
'--experimental-loader',
94+
'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}',
95+
'--print', 'myGlobal',
96+
]);
97+
98+
assert.strictEqual(stderr, '');
99+
assert.strictEqual(stdout.trim(), '4');
100+
assert.strictEqual(code, 0);
101+
assert.strictEqual(signal, null);
102+
});
103+
104+
it('should log console.log calls returned from globalPreload', async () => {
105+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
106+
'--no-warnings',
107+
'--experimental-loader',
108+
'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}',
109+
fixtures.path('empty.js'),
110+
]);
111+
112+
assert.strictEqual(stderr, '');
113+
assert.strictEqual(stdout.trim(), 'Hello from globalPreload');
114+
assert.strictEqual(code, 0);
115+
assert.strictEqual(signal, null);
116+
});
117+
118+
it('should crash if globalPreload returns code that throws', async () => {
119+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
120+
'--no-warnings',
121+
'--experimental-loader',
122+
'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}',
123+
fixtures.path('empty.js'),
124+
]);
125+
126+
assert.match(stderr, /error from globalPreload/);
127+
assert.strictEqual(stdout, '');
128+
assert.strictEqual(code, 1);
129+
assert.strictEqual(signal, null);
130+
});
131+
132+
it('should have a `this` value that is not bound to the loader instance', async () => {
133+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
134+
'--no-warnings',
135+
'--experimental-loader',
136+
`data:text/javascript,export ${function globalPreload() {
137+
if (this != null) {
138+
throw new Error('hook function must not be bound to ESMLoader instance');
139+
}
140+
}}`,
141+
fixtures.path('empty.js'),
142+
]);
143+
144+
assert.strictEqual(stderr, '');
145+
assert.strictEqual(stdout, '');
146+
assert.strictEqual(code, 0);
147+
assert.strictEqual(signal, null);
148+
});
149+
});

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

+1-125
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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';
54
import { execPath } from 'node:process';
65
import { describe, it } from 'node:test';
76

@@ -423,7 +422,7 @@ describe('Loader hooks', { concurrency: true }, () => {
423422
});
424423

425424
describe('globalPreload', () => {
426-
it('should emit deprecation warning', async () => {
425+
it('should emit warning', async () => {
427426
const { stderr } = await spawnPromisified(execPath, [
428427
'--experimental-loader',
429428
'data:text/javascript,export function globalPreload(){}',
@@ -434,129 +433,6 @@ describe('Loader hooks', { concurrency: true }, () => {
434433

435434
assert.strictEqual(stderr.match(/`globalPreload` is an experimental feature/g).length, 1);
436435
});
437-
438-
it('should not emit deprecation warning when initialize is supplied', async () => {
439-
const { stderr } = await spawnPromisified(execPath, [
440-
'--experimental-loader',
441-
'data:text/javascript,export function globalPreload(){}export function initialize(){}',
442-
fixtures.path('empty.js'),
443-
]);
444-
445-
assert.doesNotMatch(stderr, /`globalPreload` is an experimental feature/);
446-
});
447-
448-
it('should handle globalPreload returning undefined', async () => {
449-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
450-
'--no-warnings',
451-
'--experimental-loader',
452-
'data:text/javascript,export function globalPreload(){}',
453-
fixtures.path('empty.js'),
454-
]);
455-
456-
assert.strictEqual(stderr, '');
457-
assert.strictEqual(stdout, '');
458-
assert.strictEqual(code, 0);
459-
assert.strictEqual(signal, null);
460-
});
461-
462-
it('should handle loading node:test', async () => {
463-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
464-
'--no-warnings',
465-
'--experimental-loader',
466-
'data:text/javascript,export function globalPreload(){return `getBuiltin("node:test")()`}',
467-
fixtures.path('empty.js'),
468-
]);
469-
470-
assert.strictEqual(stderr, '');
471-
assert.match(stdout, /\n# pass 1\r?\n/);
472-
assert.strictEqual(code, 0);
473-
assert.strictEqual(signal, null);
474-
});
475-
476-
it('should handle loading node:os with node: prefix', async () => {
477-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
478-
'--no-warnings',
479-
'--experimental-loader',
480-
'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("node:os").arch())`}',
481-
fixtures.path('empty.js'),
482-
]);
483-
484-
assert.strictEqual(stderr, '');
485-
assert.strictEqual(stdout.trim(), os.arch());
486-
assert.strictEqual(code, 0);
487-
assert.strictEqual(signal, null);
488-
});
489-
490-
// `os` is used here because it's simple and not mocked (the builtin module otherwise doesn't matter).
491-
it('should handle loading builtin module without node: prefix', async () => {
492-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
493-
'--no-warnings',
494-
'--experimental-loader',
495-
'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("os").arch())`}',
496-
fixtures.path('empty.js'),
497-
]);
498-
499-
assert.strictEqual(stderr, '');
500-
assert.strictEqual(stdout.trim(), os.arch());
501-
assert.strictEqual(code, 0);
502-
assert.strictEqual(signal, null);
503-
});
504-
505-
it('should throw when loading node:test without node: prefix', async () => {
506-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
507-
'--no-warnings',
508-
'--experimental-loader',
509-
'data:text/javascript,export function globalPreload(){return `getBuiltin("test")()`}',
510-
fixtures.path('empty.js'),
511-
]);
512-
513-
assert.match(stderr, /ERR_UNKNOWN_BUILTIN_MODULE/);
514-
assert.strictEqual(stdout, '');
515-
assert.strictEqual(code, 1);
516-
assert.strictEqual(signal, null);
517-
});
518-
519-
it('should register globals set from globalPreload', async () => {
520-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
521-
'--no-warnings',
522-
'--experimental-loader',
523-
'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}',
524-
'--print', 'myGlobal',
525-
]);
526-
527-
assert.strictEqual(stderr, '');
528-
assert.strictEqual(stdout.trim(), '4');
529-
assert.strictEqual(code, 0);
530-
assert.strictEqual(signal, null);
531-
});
532-
533-
it('should log console.log calls returned from globalPreload', async () => {
534-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
535-
'--no-warnings',
536-
'--experimental-loader',
537-
'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}',
538-
fixtures.path('empty.js'),
539-
]);
540-
541-
assert.strictEqual(stderr, '');
542-
assert.strictEqual(stdout.trim(), 'Hello from globalPreload');
543-
assert.strictEqual(code, 0);
544-
assert.strictEqual(signal, null);
545-
});
546-
547-
it('should crash if globalPreload returns code that throws', async () => {
548-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
549-
'--no-warnings',
550-
'--experimental-loader',
551-
'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}',
552-
fixtures.path('empty.js'),
553-
]);
554-
555-
assert.match(stderr, /error from globalPreload/);
556-
assert.strictEqual(stdout, '');
557-
assert.strictEqual(code, 1);
558-
assert.strictEqual(signal, null);
559-
});
560436
});
561437

562438
it('should be fine to call `process.removeAllListeners("beforeExit")` from the main thread', async () => {
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
export function initialize() {
2+
if (this != null) {
3+
throw new Error('hook function must not be bound to loader instance');
4+
}
5+
}
6+
17
export function resolve(url, _, next) {
2-
if (this != null) throw new Error('hook function must not be bound to ESMLoader instance');
8+
if (this != null) {
9+
throw new Error('hook function must not be bound to loader instance');
10+
}
11+
312
return next(url);
413
}
514

615
export function load(url, _, next) {
7-
if (this != null) throw new Error('hook function must not be bound to ESMLoader instance');
8-
return next(url);
9-
}
16+
if (this != null) {
17+
throw new Error('hook function must not be bound to loader instance');
18+
}
1019

11-
export function globalPreload() {
12-
if (this != null) throw new Error('hook function must not be bound to ESMLoader instance');
13-
return "";
20+
return next(url);
1421
}

0 commit comments

Comments
 (0)