From 6dd55ef984bf047ce628c25d9be5921bc1cbfde0 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 1 Feb 2017 21:05:29 -0800 Subject: [PATCH] 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. --- test/parallel/test-module-loading-error.js | 44 ++++++++++------------ 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/test/parallel/test-module-loading-error.js b/test/parallel/test-module-loading-error.js index d78f61de60fa14..1bff2c4a7c7f5c 100644 --- a/test/parallel/test-module-loading-error.js +++ b/test/parallel/test-module-loading-error.js @@ -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'], @@ -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$/ +);