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 of lib/internal/loader/ModuleMap.js #16061

Closed
wants to merge 1 commit into from
Closed
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
96 changes: 96 additions & 0 deletions test/parallel/test-internal-module-map-asserts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
const assert = require('assert');
const ModuleMap = require('internal/loader/ModuleMap');

// ModuleMap.get, url argument should only accept string values
{
const errorReg = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "url" argument must be of type string/
}, 5);

const moduleMap = new ModuleMap();

assert.throws(() => moduleMap.get({}), errorReg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can put these in a loop to adhere to the DRY principle:

[{}, [], true, 1, () => {}].forEach((element) => {
  assert.throws(() => moduleMap.get(element), errorReg);
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should likely be made into...

common.expectsError(
  () => moduleMap.get([]),
  {
    code: 'ERR_INVALID_ARG_TYPE',
    type: TypeError,
    message: /^The "url" argument must be of type string/
  }
);

etc


assert.throws(() => moduleMap.get([]), errorReg);

assert.throws(() => moduleMap.get(true), errorReg);

assert.throws(() => moduleMap.get(1), errorReg);

assert.throws(() => moduleMap.get(() => {}), errorReg);
}

// ModuleMap.has, url argument should only accept string values
{
const errorReg = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "url" argument must be of type string/
}, 5);

const moduleMap = new ModuleMap();

assert.throws(() => moduleMap.has({}), errorReg);

assert.throws(() => moduleMap.has([]), errorReg);

assert.throws(() => moduleMap.has(true), errorReg);

assert.throws(() => moduleMap.has(1), errorReg);

assert.throws(() => moduleMap.has(() => {}), errorReg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We don't really need to keep all of the assertions separated by empty lines, and we usually don't do it for single-line assertions. I think it mostly blows up the visual file size in this case.

}

// ModuleMap.set, url argument should only accept string values
{
const errorReg = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "url" argument must be of type string/
}, 5);

// 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...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the extra text, I think the first sentence is enough:

Tried to mock the "job" parameter, but I think it's useless, and was not simple to mock...

const job = undefined;

const moduleMap = new ModuleMap();

assert.throws(() => moduleMap.set({}, job), errorReg);

assert.throws(() => moduleMap.set([], job), errorReg);

assert.throws(() => moduleMap.set(true, job), errorReg);

assert.throws(() => moduleMap.set(1, job), errorReg);

assert.throws(() => moduleMap.set(() => {}, job), errorReg);
}


// ModuleMap.set, job argument should only accept ModuleJob values
{
const errorReg = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "job" argument must be of type ModuleJob/
}, 5);

const moduleMap = new ModuleMap();

assert.throws(() => moduleMap.set('', {}), errorReg);

assert.throws(() => moduleMap.set('', []), errorReg);

assert.throws(() => moduleMap.set('', true), errorReg);

assert.throws(() => moduleMap.set('', 1), errorReg);

assert.throws(() => moduleMap.set('', () => {}), errorReg);
}