node/test/parallel/test-internal-module-map-asserts.js
Ruben Bridgewater ac2fc0dd5f
errors: improve ERR_INVALID_ARG_TYPE
ERR_INVALID_ARG_TYPE is the most common error used throughout the
code base. This improves the error message by providing more details
to the user and by indicating more precisely which values are allowed
ones and which ones are not.

It adds the actual input to the error message in case it's a primitive.
If it's a class instance, it'll print the class name instead of
"object" and "falsy" or similar entries are not named "type" anymore.

PR-URL: https://github.com/nodejs/node/pull/29675
Reviewed-By: Rich Trott <rtrott@gmail.com>
2019-12-20 03:10:13 +01:00

43 lines
1.2 KiB
JavaScript

// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const ModuleMap = require('internal/modules/esm/module_map');
// ModuleMap.get, ModuleMap.has and ModuleMap.set should only accept string
// values as url argument.
{
const errorObj = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "url" argument must be of type string/
};
const moduleMap = new ModuleMap();
// As long as the assertion of "job" argument is done after the assertion of
// "url" argument this test suite is ok. Tried to mock the "job" parameter,
// but I think it's useless, and was not simple to mock...
const job = undefined;
[{}, [], true, 1].forEach((value) => {
assert.throws(() => moduleMap.get(value), errorObj);
assert.throws(() => moduleMap.has(value), errorObj);
assert.throws(() => moduleMap.set(value, job), errorObj);
});
}
// ModuleMap.set, job argument should only accept ModuleJob values.
{
const moduleMap = new ModuleMap();
[{}, [], true, 1].forEach((value) => {
assert.throws(() => moduleMap.set('', value), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: /^The "job" argument must be an instance of ModuleJob/
});
});
}