Skip to content

Commit

Permalink
test: make module testing stricter
Browse files Browse the repository at this point in the history
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.

PR-URL: #11116
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
Trott authored and italoacasas committed Feb 14, 2017
1 parent d8a5e1c commit 7f9b436
Showing 1 changed file with 19 additions and 25 deletions.
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)))
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$/
);

0 comments on commit 7f9b436

Please sign in to comment.