Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve coverage for load hooks #43374

Merged
merged 2 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions test/es-module/test-esm-loader-thenable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { mustCall } from '../common/index.mjs';
import { fileURL, path } from '../common/fixtures.mjs';
import { match, ok, notStrictEqual, strictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';

{
const child = spawn(execPath, [
'--experimental-loader',
fileURL('es-module-loaders', 'thenable-load-hook.mjs').href,
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
path('es-modules', 'test-esm-ok.mjs'),
]);

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
strictEqual(code, 0);
ok(!stderr.includes('must not call'));
}));
}

{
const child = spawn(execPath, [
'--experimental-loader',
fileURL('es-module-loaders', 'thenable-load-hook-rejected.mjs').href,
path('es-modules', 'test-esm-ok.mjs'),
]);

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);

match(stderr, /\sError: must crash the process\r?\n/);

ok(!stderr.includes('must not call'));
}));
}

{
const child = spawn(execPath, [
'--experimental-loader',
fileURL('es-module-loaders', 'thenable-load-hook-rejected-no-arguments.mjs').href,
path('es-modules', 'test-esm-ok.mjs'),
]);

let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, _signal) => {
notStrictEqual(code, 0);

match(stderr, /\sundefined\r?\n/);

ok(!stderr.includes('must not call'));
}));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function load () {
let thenAlreadyAccessed = false;
return {
get then() {
if (thenAlreadyAccessed) throw new Error('must not call');
thenAlreadyAccessed = true;
return (_, reject) => reject();
}
};
}
10 changes: 10 additions & 0 deletions test/fixtures/es-module-loaders/thenable-load-hook-rejected.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function load () {
let thenAlreadyAccessed = false;
return {
get then() {
if (thenAlreadyAccessed) throw new Error('must not call');
thenAlreadyAccessed = true;
return (_, reject) => reject(new Error('must crash the process'));
}
};
}
10 changes: 10 additions & 0 deletions test/fixtures/es-module-loaders/thenable-load-hook.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function load(url, context, next) {
let thenAlreadyAccessed = false;
return {
get then() {
if (thenAlreadyAccessed) throw new Error('must not call');
thenAlreadyAccessed = true;
return (resolve) => resolve(next(url, context));
}
};
}