Skip to content

Commit

Permalink
fixup: add test case for null hook returns
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobJingleheimer committed Jul 1, 2022
1 parent 581d8a8 commit d510852
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
14 changes: 6 additions & 8 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ const { getOptionValue } = require('internal/options');

let emittedSpecifierResolutionWarning = false;

const nullTypeForErr = { constructor: { name: 'null' } };

/**
* A utility function to iterate through a hook chain, track advancement in the
* chain, and generate and supply the `next<HookName>` argument to the custom
Expand Down Expand Up @@ -602,11 +604,11 @@ class ESMLoader {
if (ctx) validateObject(ctx, `${hookErrIdentifier} context`);
};
const validateOutput = (hookErrIdentifier, output) => {
if (typeof output !== 'object') { // [2]
if (typeof output !== 'object' || output === null) { // [2]
throw new ERR_INVALID_RETURN_VALUE(
'an object',
hookErrIdentifier,
output,
output === null ? nullTypeForErr : output,
);
}
};
Expand Down Expand Up @@ -838,15 +840,11 @@ class ESMLoader {
if (ctx) validateObject(ctx, `${hookErrIdentifier} context`);
};
const validateOutput = (hookErrIdentifier, output) => {
if (
typeof output !== 'object' || // [2]
output === null ||
(output.url == null && typeof output.then === 'function')
) {
if (typeof output !== 'object' || output === null) { // [2]
throw new ERR_INVALID_RETURN_VALUE(
'an object',
hookErrIdentifier,
output,
output === null ? nullTypeForErr : output,
);
}
};
Expand Down
46 changes: 44 additions & 2 deletions test/es-module/test-esm-loader-chaining.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ const commonArgs = [
assert.strictEqual(status, 0);
}

{ // Verify chain does break and throws appropriately
{ // Verify resolve chain does break and throws appropriately
const { status, stderr, stdout } = spawnSync(
process.execPath,
[
Expand Down Expand Up @@ -273,7 +273,7 @@ const commonArgs = [
assert.strictEqual(status, 1);
}

{ // Verify chain does break and throws appropriately
{ // Verify load chain does break and throws appropriately
const { status, stderr, stdout } = spawnSync(
process.execPath,
[
Expand Down Expand Up @@ -314,6 +314,27 @@ const commonArgs = [
assert.match(stderr, /'resolve' hook's nextResolve\(\) specifier/);
}

{ // Verify error thrown when resolve hook is invalid
const { status, stderr } = spawnSync(
process.execPath,
[
'--loader',
fixtures.fileURL('es-module-loaders', 'loader-resolve-passthru.mjs'),
'--loader',
fixtures.fileURL('es-module-loaders', 'loader-resolve-null-return.mjs'),
...commonArgs,
],
{ encoding: 'utf8' },
);

assert.strictEqual(status, 1);
assert.match(stderr, /ERR_INVALID_RETURN_VALUE/);
assert.match(stderr, /loader-resolve-null-return\.mjs/);
assert.match(stderr, /'resolve' hook's nextResolve\(\)/);
assert.match(stderr, /an object/);
assert.match(stderr, /instance of null/);
}

{ // Verify error thrown when invalid `context` argument passed to `nextResolve`
const { status, stderr } = spawnSync(
process.execPath,
Expand All @@ -333,6 +354,27 @@ const commonArgs = [
assert.strictEqual(status, 1);
}

{ // Verify error thrown when load hook is invalid
const { status, stderr } = spawnSync(
process.execPath,
[
'--loader',
fixtures.fileURL('es-module-loaders', 'loader-load-passthru.mjs'),
'--loader',
fixtures.fileURL('es-module-loaders', 'loader-load-null-return.mjs'),
...commonArgs,
],
{ encoding: 'utf8' },
);

assert.strictEqual(status, 1);
assert.match(stderr, /ERR_INVALID_RETURN_VALUE/);
assert.match(stderr, /loader-load-null-return\.mjs/);
assert.match(stderr, /'load' hook's nextLoad\(\)/);
assert.match(stderr, /an object/);
assert.match(stderr, /instance of null/);
}

{ // Verify error thrown when invalid `url` argument passed to `nextLoad`
const { status, stderr } = spawnSync(
process.execPath,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/es-module-loaders/loader-load-null-return.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function load(specifier, context, next) {
return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function resolve(specifier, context, next) {
return null;
}

0 comments on commit d510852

Please sign in to comment.