Skip to content
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
test: make module testing stricter
In test-module-loading-error:

* Do not skip the rest of the test just because we are running on a
  platform for which the test does not know the expected system error
  message. Simply skip the message validation but run the remainder of
  the test.
* Use assert.throws() in place of try/catch
* Make checks more strict. Instead of partial string matches, match the
  entire string. Add check for Error name to at least do some validation
  in situations where we do not have the system error message.
  • Loading branch information
Trott committed Feb 2, 2017
commit 6dd55ef984bf047ce628c25d9be5921bc1cbfde0
44 changes: 19 additions & 25 deletions test/parallel/test-module-loading-error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');

console.error('load test-module-loading-error.js');

const error_desc = {
win32: ['%1 is not a valid Win32 application'],
linux: ['file too short', 'Exec format error'],
Expand All @@ -12,27 +10,23 @@ const error_desc = {
};
const dlerror_msg = error_desc[process.platform];

if (!dlerror_msg) {
common.skip('platform not supported.');
return;
}

try {
require('../fixtures/module-loading-error.node');
} catch (e) {
assert.strictEqual(dlerror_msg.some((errMsgCase) => {
return e.toString().indexOf(errMsgCase) !== -1;
}), true);
}
assert.throws(
() => { require('../fixtures/module-loading-error.node'); },
(e) => {
if (dlerror_msg && !dlerror_msg.some((msg) => e.message.includes(msg)))
Copy link
Copy Markdown
Contributor

@cjihrig cjihrig Feb 2, 2017

Choose a reason for hiding this comment

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

Would you be open to writing this check like this:

// Skip platforms where the error is not known.
if (!dlerror_msg)
  return true;

return e.name === 'Error' && dlerror_msg.some((msg) => e.message.includes(msg));

EDIT: Nevermind. We don't need to skip the e.name check on any platforms.

return false;
if (e.name !== 'Error')
return false;
return true;
}
);

try {
require();
} catch (e) {
assert.ok(e.toString().includes('missing path'));
}
assert.throws(
require,
/^AssertionError: missing path$/
);

try {
require({});
} catch (e) {
assert.ok(e.toString().includes('path must be a string'));
}
assert.throws(
() => { require({}); },
/^AssertionError: path must be a string$/
);