Skip to content

Commit 65201ab

Browse files
GeoffreyBoothtargos
authored andcommitted
test: isolate globalPreload tests
PR-URL: #49545 Backport-PR-URL: #50669 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 9456094 commit 65201ab

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

@@ -371,7 +370,7 @@ describe('Loader hooks', { concurrency: true }, () => {
371370
});
372371

373372
describe('globalPreload', () => {
374-
it('should emit deprecation warning', async () => {
373+
it('should emit warning', async () => {
375374
const { stderr } = await spawnPromisified(execPath, [
376375
'--experimental-loader',
377376
'data:text/javascript,export function globalPreload(){}',
@@ -382,129 +381,6 @@ describe('Loader hooks', { concurrency: true }, () => {
382381

383382
assert.strictEqual(stderr.match(/`globalPreload` is an experimental feature/g).length, 1);
384383
});
385-
386-
it('should not emit deprecation warning when initialize is supplied', async () => {
387-
const { stderr } = await spawnPromisified(execPath, [
388-
'--experimental-loader',
389-
'data:text/javascript,export function globalPreload(){}export function initialize(){}',
390-
fixtures.path('empty.js'),
391-
]);
392-
393-
assert.doesNotMatch(stderr, /`globalPreload` is an experimental feature/);
394-
});
395-
396-
it('should handle globalPreload returning undefined', async () => {
397-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
398-
'--no-warnings',
399-
'--experimental-loader',
400-
'data:text/javascript,export function globalPreload(){}',
401-
fixtures.path('empty.js'),
402-
]);
403-
404-
assert.strictEqual(stderr, '');
405-
assert.strictEqual(stdout, '');
406-
assert.strictEqual(code, 0);
407-
assert.strictEqual(signal, null);
408-
});
409-
410-
it('should handle loading node:test', async () => {
411-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
412-
'--no-warnings',
413-
'--experimental-loader',
414-
'data:text/javascript,export function globalPreload(){return `getBuiltin("node:test")()`}',
415-
fixtures.path('empty.js'),
416-
]);
417-
418-
assert.strictEqual(stderr, '');
419-
assert.match(stdout, /\n# pass 1\r?\n/);
420-
assert.strictEqual(code, 0);
421-
assert.strictEqual(signal, null);
422-
});
423-
424-
it('should handle loading node:os with node: prefix', async () => {
425-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
426-
'--no-warnings',
427-
'--experimental-loader',
428-
'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("node:os").arch())`}',
429-
fixtures.path('empty.js'),
430-
]);
431-
432-
assert.strictEqual(stderr, '');
433-
assert.strictEqual(stdout.trim(), os.arch());
434-
assert.strictEqual(code, 0);
435-
assert.strictEqual(signal, null);
436-
});
437-
438-
// `os` is used here because it's simple and not mocked (the builtin module otherwise doesn't matter).
439-
it('should handle loading builtin module without node: prefix', async () => {
440-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
441-
'--no-warnings',
442-
'--experimental-loader',
443-
'data:text/javascript,export function globalPreload(){return `console.log(getBuiltin("os").arch())`}',
444-
fixtures.path('empty.js'),
445-
]);
446-
447-
assert.strictEqual(stderr, '');
448-
assert.strictEqual(stdout.trim(), os.arch());
449-
assert.strictEqual(code, 0);
450-
assert.strictEqual(signal, null);
451-
});
452-
453-
it('should throw when loading node:test without node: prefix', async () => {
454-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
455-
'--no-warnings',
456-
'--experimental-loader',
457-
'data:text/javascript,export function globalPreload(){return `getBuiltin("test")()`}',
458-
fixtures.path('empty.js'),
459-
]);
460-
461-
assert.match(stderr, /ERR_UNKNOWN_BUILTIN_MODULE/);
462-
assert.strictEqual(stdout, '');
463-
assert.strictEqual(code, 1);
464-
assert.strictEqual(signal, null);
465-
});
466-
467-
it('should register globals set from globalPreload', async () => {
468-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
469-
'--no-warnings',
470-
'--experimental-loader',
471-
'data:text/javascript,export function globalPreload(){return "this.myGlobal=4"}',
472-
'--print', 'myGlobal',
473-
]);
474-
475-
assert.strictEqual(stderr, '');
476-
assert.strictEqual(stdout.trim(), '4');
477-
assert.strictEqual(code, 0);
478-
assert.strictEqual(signal, null);
479-
});
480-
481-
it('should log console.log calls returned from globalPreload', async () => {
482-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
483-
'--no-warnings',
484-
'--experimental-loader',
485-
'data:text/javascript,export function globalPreload(){return `console.log("Hello from globalPreload")`}',
486-
fixtures.path('empty.js'),
487-
]);
488-
489-
assert.strictEqual(stderr, '');
490-
assert.strictEqual(stdout.trim(), 'Hello from globalPreload');
491-
assert.strictEqual(code, 0);
492-
assert.strictEqual(signal, null);
493-
});
494-
495-
it('should crash if globalPreload returns code that throws', async () => {
496-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
497-
'--no-warnings',
498-
'--experimental-loader',
499-
'data:text/javascript,export function globalPreload(){return `throw new Error("error from globalPreload")`}',
500-
fixtures.path('empty.js'),
501-
]);
502-
503-
assert.match(stderr, /error from globalPreload/);
504-
assert.strictEqual(stdout, '');
505-
assert.strictEqual(code, 1);
506-
assert.strictEqual(signal, null);
507-
});
508384
});
509385

510386
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)