From 37b4e3b37360570331450be2d793e5e2a5e11f92 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 1 Nov 2022 12:21:12 +0100 Subject: [PATCH 1/2] refactor: push config file extension checks a bit to allow loaders --- lib/load-config.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/load-config.js b/lib/load-config.js index 9ffa2aa6e..01e20aaad 100644 --- a/lib/load-config.js +++ b/lib/load-config.js @@ -36,10 +36,6 @@ const loadConfigFile = async ({projectDir, configFile}) => { 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 new Error('Config files must have .js, .cjs or .mjs extensions'); + } + + throw error; + } + if (loaded !== null) { ({config: fileConf, fileForErrorMessage} = loaded); } From ef0a1636c3c370fbd69ca4ceac4352f1b3261369 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sun, 13 Nov 2022 16:26:33 +0100 Subject: [PATCH 2/2] Tweak error reporting --- lib/cli.js | 4 ++-- lib/load-config.js | 4 ++-- test/config/loader.js | 2 +- test/config/snapshots/loader.js.md | 2 +- test/config/snapshots/loader.js.snap | Bin 586 -> 594 bytes 5 files changed, 6 insertions(+), 6 deletions(-) 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 01e20aaad..eca4232ae 100644 --- a/lib/load-config.js +++ b/lib/load-config.js @@ -29,7 +29,7 @@ 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}); } }; @@ -95,7 +95,7 @@ export async function loadConfig({configFile, resolveFrom = process.cwd(), defau loaded = await loadConfigFile({projectDir, configFile}); } catch (error) { if (!configFile.endsWith('.js') && !configFile.endsWith('.cjs') && !configFile.endsWith('.mjs')) { - throw new Error('Config files must have .js, .cjs or .mjs extensions'); + throw Object.assign(new Error('Could not load config file; it should have .js, .cjs or .mjs extension'), {cause: error}); } throw error; 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 d060c1cc34a87bd6d0e5d75139010ddc44172b18..83344ffc9c7ecd858f0f0c67af9dad1e56a16a00 100644 GIT binary patch literal 594 zcmV-Y0#|y0crf zAPvoi?H`K>00000000A(mCtU|KoG`LA;g(Cm^+X&G;kqG5OAuBDsE(KkK=9EJJ#;H zO>S`H5jfIE<4HQ}KT+H)PMU*bdo?q^@7tO6-#pQr-nifQIfIoe*t=i%S%x0Je||Q3F$ur3=aZLbv$@1*m3n2?U3_iP zD>k4E;4Mzum(%M|FM|rx8(fn-vLTHL>9S=g$vIi*8Q*q{g4QNjDowyulexeKzBaC| zDz=_7pp7TvWbqLA+W>eu=UQiA*n?)wy6lik`WYw>5*pSEyd@CI*Dy^5T>z)0{O`() zv44#Ci@QRYdbV50m|R0)$=6n_f=Z)U#>q9M5!){YD(1^BO-a7sAiwh=**-G8tlfWV zj7c~jldwZ?s~$!FGC+SH;fp2;ruS~;wMqRE+vCik#@-$`y<-?M%CXjcLUd(Z@l6otdPp-)N?IHS)0s6(lq+A=jCHI$9UXfjFgiDoj^tdsR zp9Uh|#lj)CT*fvhp}=>12*&3)h#`ZCrpRH`Lmb_m82EHQS44zrrw<`k@u^ML9Q^(1 g+?;hMeFeS_WPEtyS77grRE!}%0O7-DwzUQT0O=MZ00000 literal 586 zcmV-Q0=4}?RzV)2_K6H00000000A(l+A9_KoG}MA;g(Cm^}FE^0Z`i<9^+f*S@6n3tw2ibASfOTDG8b zNZA*l*yMU82bQ3=Cs(RmV_~b8;{+zUdhSt#fd$O9HN2&jq&djd4v~ zv-Ok#tvwkhhr7TZM!>JPOlwb->Qpt1c(oKPWj3g6#X2?-_ zwj0P;Vj1>e$v0N=0(FU!A5O1HMRI&GLY|-ZZYuHx2d%y6bn_^~t(<6#DG8@j61M1F z)V=6mM(FP%d{IXs>qEBk#;8!|W@-Fk`hu-|V7)AE93?jN-<4veF%YfBY6>xo^#P zM)FKPS!Zx(v95zyU(k4pr(|3mE(4_u=2c(nrMzEKbwzfu7A{@Mo^ewmKaE7bjhRDk zxr!AhrNFm;2&U&ah#`Y%*cm&UXMosSofvrX&{sr+?xqhR7V+37Yq$KcirZy((8~Wh YlJWk?mH*(6M2sQ-0}l}3Pof3@09`F3N&o-=