Skip to content

Commit 24dfd70

Browse files
aduh95targos
authored andcommitted
test: improve coverage for load hooks
Refs: nodejs#43363 (comment) PR-URL: nodejs#43374 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Akhil Marsonya <[email protected]>
1 parent 4c726b0 commit 24dfd70

4 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import { fileURL, path } from '../common/fixtures.mjs';
3+
import { match, ok, notStrictEqual, strictEqual } from 'assert';
4+
import { spawn } from 'child_process';
5+
import { execPath } from 'process';
6+
7+
{
8+
const child = spawn(execPath, [
9+
'--experimental-loader',
10+
fileURL('es-module-loaders', 'thenable-load-hook.mjs').href,
11+
path('es-modules', 'test-esm-ok.mjs'),
12+
]);
13+
14+
let stderr = '';
15+
child.stderr.setEncoding('utf8');
16+
child.stderr.on('data', (data) => {
17+
stderr += data;
18+
});
19+
child.on('close', mustCall((code, _signal) => {
20+
strictEqual(code, 0);
21+
ok(!stderr.includes('must not call'));
22+
}));
23+
}
24+
25+
{
26+
const child = spawn(execPath, [
27+
'--experimental-loader',
28+
fileURL('es-module-loaders', 'thenable-load-hook-rejected.mjs').href,
29+
path('es-modules', 'test-esm-ok.mjs'),
30+
]);
31+
32+
let stderr = '';
33+
child.stderr.setEncoding('utf8');
34+
child.stderr.on('data', (data) => {
35+
stderr += data;
36+
});
37+
child.on('close', mustCall((code, _signal) => {
38+
notStrictEqual(code, 0);
39+
40+
match(stderr, /\sError: must crash the process\r?\n/);
41+
42+
ok(!stderr.includes('must not call'));
43+
}));
44+
}
45+
46+
{
47+
const child = spawn(execPath, [
48+
'--experimental-loader',
49+
fileURL('es-module-loaders', 'thenable-load-hook-rejected-no-arguments.mjs').href,
50+
path('es-modules', 'test-esm-ok.mjs'),
51+
]);
52+
53+
let stderr = '';
54+
child.stderr.setEncoding('utf8');
55+
child.stderr.on('data', (data) => {
56+
stderr += data;
57+
});
58+
child.on('close', mustCall((code, _signal) => {
59+
notStrictEqual(code, 0);
60+
61+
match(stderr, /\sundefined\r?\n/);
62+
63+
ok(!stderr.includes('must not call'));
64+
}));
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function load () {
2+
let thenAlreadyAccessed = false;
3+
return {
4+
get then() {
5+
if (thenAlreadyAccessed) throw new Error('must not call');
6+
thenAlreadyAccessed = true;
7+
return (_, reject) => reject();
8+
}
9+
};
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function load () {
2+
let thenAlreadyAccessed = false;
3+
return {
4+
get then() {
5+
if (thenAlreadyAccessed) throw new Error('must not call');
6+
thenAlreadyAccessed = true;
7+
return (_, reject) => reject(new Error('must crash the process'));
8+
}
9+
};
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function load(url, context, next) {
2+
let thenAlreadyAccessed = false;
3+
return {
4+
get then() {
5+
if (thenAlreadyAccessed) throw new Error('must not call');
6+
thenAlreadyAccessed = true;
7+
return (resolve) => resolve(next(url, context));
8+
}
9+
};
10+
}

0 commit comments

Comments
 (0)