From 7d7218b252028f25be6af33929fff354de375ba5 Mon Sep 17 00:00:00 2001 From: Tom White Date: Fri, 5 Oct 2018 15:00:16 -0700 Subject: [PATCH 1/2] test: add module require tests for certain package.json errors test for unusual error cases: verify that module require() falls back to index if package.json names a missing file and throws an error if package.json is unparseable. --- test/fixtures/packages/missing-main/index.js | 22 +++++++++++++++++++ .../packages/missing-main/package.json | 4 ++++ .../packages/unparseable/package.json | 4 ++++ test/sequential/test-module-loading.js | 11 ++++++++++ 4 files changed, 41 insertions(+) create mode 100644 test/fixtures/packages/missing-main/index.js create mode 100644 test/fixtures/packages/missing-main/package.json create mode 100644 test/fixtures/packages/unparseable/package.json diff --git a/test/fixtures/packages/missing-main/index.js b/test/fixtures/packages/missing-main/index.js new file mode 100644 index 00000000000000..c361a6dc8d1950 --- /dev/null +++ b/test/fixtures/packages/missing-main/index.js @@ -0,0 +1,22 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +exports.ok = 'ok'; diff --git a/test/fixtures/packages/missing-main/package.json b/test/fixtures/packages/missing-main/package.json new file mode 100644 index 00000000000000..feb846703ff796 --- /dev/null +++ b/test/fixtures/packages/missing-main/package.json @@ -0,0 +1,4 @@ +{ + "name": "missingmain", + "main": "doesnotexist.js" +} diff --git a/test/fixtures/packages/unparseable/package.json b/test/fixtures/packages/unparseable/package.json new file mode 100644 index 00000000000000..91e95a5103216b --- /dev/null +++ b/test/fixtures/packages/unparseable/package.json @@ -0,0 +1,4 @@ +{ + "main": "therain" "inspain" +} + diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index c71f9d1edf4d5b..ba2c8f40202acd 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -104,6 +104,15 @@ const d2 = require('../fixtures/b/d'); assert.strictEqual(require('../fixtures/packages/index').ok, 'ok'); assert.strictEqual(require('../fixtures/packages/main').ok, 'ok'); assert.strictEqual(require('../fixtures/packages/main-index').ok, 'ok'); +assert.strictEqual(require('../fixtures/packages/missing-main').ok, 'ok'); + +let unparseableErrorThrown = false; +try { + require('../fixtures/packages/unparseable'); +} catch (e) { + unparseableErrorThrown = true; + assert.strictEqual(/^Error parsing .*/.test(e.message), true); +} { console.error('test cycles containing a .. path'); @@ -267,6 +276,7 @@ try { 'fixtures/packages/index/index.js': {}, 'fixtures/packages/main/package-main-module.js': {}, 'fixtures/packages/main-index/package-main-module/index.js': {}, + 'fixtures/packages/missing-main/index.js': {}, 'fixtures/cycles/root.js': { 'fixtures/cycles/folder/foo.js': {} }, @@ -317,6 +327,7 @@ process.on('exit', function() { assert.strictEqual(d2.D(), 'D done'); assert.strictEqual(errorThrown, true); + assert.strictEqual(unparseableErrorThrown, true); console.log('exit'); }); From c571dab2606e85c80a8d3c360e134297d1364843 Mon Sep 17 00:00:00 2001 From: Tom White Date: Fri, 5 Oct 2018 17:40:39 -0700 Subject: [PATCH 2/2] test: migrate new tests to use assert.throws --- test/sequential/test-module-loading.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index ba2c8f40202acd..42916a7903c344 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -106,13 +106,10 @@ assert.strictEqual(require('../fixtures/packages/main').ok, 'ok'); assert.strictEqual(require('../fixtures/packages/main-index').ok, 'ok'); assert.strictEqual(require('../fixtures/packages/missing-main').ok, 'ok'); -let unparseableErrorThrown = false; -try { - require('../fixtures/packages/unparseable'); -} catch (e) { - unparseableErrorThrown = true; - assert.strictEqual(/^Error parsing .*/.test(e.message), true); -} +assert.throws( + function() { require('../fixtures/packages/unparseable'); }, + /^SyntaxError: Error parsing/ +); { console.error('test cycles containing a .. path'); @@ -327,7 +324,6 @@ process.on('exit', function() { assert.strictEqual(d2.D(), 'D done'); assert.strictEqual(errorThrown, true); - assert.strictEqual(unparseableErrorThrown, true); console.log('exit'); });