From 73138906705f162f84f461d85e21edd3333ef094 Mon Sep 17 00:00:00 2001 From: kirill-golovan <57108967+kirill-golovan@users.noreply.github.com> Date: Sun, 25 Jul 2021 01:26:19 -0700 Subject: [PATCH] Fix exception rethrow from js config file (#4702) --- lib/cli/config.js | 2 +- test/integration/config.spec.js | 22 +++++++++++++++++++ .../fixtures/config/mocharcWithThrowError.js | 11 ++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/integration/fixtures/config/mocharcWithThrowError.js diff --git a/lib/cli/config.js b/lib/cli/config.js index b7510d7deb..10f16d8aa3 100644 --- a/lib/cli/config.js +++ b/lib/cli/config.js @@ -31,7 +31,7 @@ exports.CONFIG_FILES = [ ]; const isModuleNotFoundError = err => - err.code !== 'MODULE_NOT_FOUND' || + err.code === 'MODULE_NOT_FOUND' || err.message.indexOf('Cannot find module') !== -1; /** diff --git a/test/integration/config.spec.js b/test/integration/config.spec.js index 52fa886220..4c65f76c24 100644 --- a/test/integration/config.spec.js +++ b/test/integration/config.spec.js @@ -47,6 +47,28 @@ describe('config', function() { expect(js, 'to equal', json); }); + it('should rethrow error from absolute path configuration', function() { + function _loadConfig() { + loadConfig(path.join(configDir, 'mocharcWithThrowError.js')); + } + + expect(_loadConfig, 'to throw', { + message: /Error from mocharcWithThrowError/ + }); + }); + + it('should rethrow error from cwd-relative path configuration', function() { + var relConfigDir = configDir.substring(projRootDir.length + 1); + + function _loadConfig() { + loadConfig(path.join('.', relConfigDir, 'mocharcWithThrowError.js')); + } + + expect(_loadConfig, 'to throw', { + message: /Error from mocharcWithThrowError/ + }); + }); + // In other words, path does not begin with '/', './', or '../' describe('when path is neither absolute or relative', function() { var nodeModulesDir = path.join(projRootDir, 'node_modules'); diff --git a/test/integration/fixtures/config/mocharcWithThrowError.js b/test/integration/fixtures/config/mocharcWithThrowError.js new file mode 100644 index 0000000000..f88872027e --- /dev/null +++ b/test/integration/fixtures/config/mocharcWithThrowError.js @@ -0,0 +1,11 @@ +'use strict'; + +throw new Error("Error from mocharcWithThrowError"); + +// a comment +module.exports = { + require: ['foo', 'bar'], + bail: true, + reporter: 'dot', + slow: 60 +};