diff --git a/lib/cli.js b/lib/cli.js index c2ddcbca6..afe552801 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -250,8 +250,8 @@ export default async function loadCli() { // eslint-disable-line complexity setChalk(chalkOptions); if (confError) { - if (confError.parent) { - exit(`${confError.message}\n\n${chalk.gray((confError.parent && confError.parent.stack) || confError.parent)}`); + if (confError.cause) { + exit(`${confError.message}\n\n${chalk.gray(confError.cause?.stack ?? confError.cause)}`); } else { exit(confError.message); } diff --git a/lib/load-config.js b/lib/load-config.js index 9ffa2aa6e..eca4232ae 100644 --- a/lib/load-config.js +++ b/lib/load-config.js @@ -29,17 +29,13 @@ const loadConfigFile = async ({projectDir, configFile}) => { return null; } - throw Object.assign(new Error(`Error loading ${fileForErrorMessage}: ${error.message}`), {parent: error}); + throw Object.assign(new Error(`Error loading ${fileForErrorMessage}: ${error.message}`), {cause: error}); } }; function resolveConfigFile(configFile) { if (configFile) { configFile = path.resolve(configFile); // Relative to CWD - - if (!configFile.endsWith('.js') && !configFile.endsWith('.cjs') && !configFile.endsWith('.mjs')) { - throw new Error('Config files must have .js, .cjs or .mjs extensions'); - } } return configFile; @@ -78,7 +74,7 @@ async function checkJsonFile(searchDir) { } } -export async function loadConfig({configFile, resolveFrom = process.cwd(), defaults = {}} = {}) { +export async function loadConfig({configFile, resolveFrom = process.cwd(), defaults = {}} = {}) { // eslint-disable-line complexity let packageConf = await packageConfig('ava', {cwd: resolveFrom}); const filepath = packageJsonPath(packageConf); const projectDir = filepath === undefined ? resolveFrom : path.dirname(filepath); @@ -94,7 +90,17 @@ export async function loadConfig({configFile, resolveFrom = process.cwd(), defau let fileForErrorMessage; let conflicting = []; if (configFile) { - const loaded = await loadConfigFile({projectDir, configFile}); + let loaded; + try { + loaded = await loadConfigFile({projectDir, configFile}); + } catch (error) { + if (!configFile.endsWith('.js') && !configFile.endsWith('.cjs') && !configFile.endsWith('.mjs')) { + throw Object.assign(new Error('Could not load config file; it should have .js, .cjs or .mjs extension'), {cause: error}); + } + + throw error; + } + if (loaded !== null) { ({config: fileConf, fileForErrorMessage} = loaded); } diff --git a/test/config/loader.js b/test/config/loader.js index 8f4c5ae54..74c43ffbc 100644 --- a/test/config/loader.js +++ b/test/config/loader.js @@ -110,7 +110,7 @@ test.serial('receives a `projectDir` property', (...args) => ok('package-only')( }); test.serial('rethrows wrapped module errors', notOk('throws'), (t, error) => { - t.is(error.parent.message, 'foo'); + t.is(error.cause.message, 'foo'); }); test.serial('throws an error if a .js config file has no default export', notOk('no-default-export')); diff --git a/test/config/snapshots/loader.js.md b/test/config/snapshots/loader.js.md index 9b39303ef..499926429 100644 --- a/test/config/snapshots/loader.js.md +++ b/test/config/snapshots/loader.js.md @@ -14,7 +14,7 @@ Generated by [AVA](https://avajs.dev). > error message - 'Config files must have .js, .cjs or .mjs extensions' + 'Could not load config file; it should have .js, .cjs or .mjs extension' ## throws an error if a config factory does not return a plain object diff --git a/test/config/snapshots/loader.js.snap b/test/config/snapshots/loader.js.snap index d060c1cc3..83344ffc9 100644 Binary files a/test/config/snapshots/loader.js.snap and b/test/config/snapshots/loader.js.snap differ