-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
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); | ||
} |
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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...
etc