From 50f00f480fb822c10f7e1b3b59c3b5a8b15208b0 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Tue, 10 Oct 2023 19:24:58 +0200 Subject: [PATCH] test: fix --- packages/astro/src/core/config/tsconfig.ts | 20 +++++++++++-------- .../test/units/config/config-tsconfig.test.js | 18 ++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/astro/src/core/config/tsconfig.ts b/packages/astro/src/core/config/tsconfig.ts index 408c2a1a4..debb0a3ff 100644 --- a/packages/astro/src/core/config/tsconfig.ts +++ b/packages/astro/src/core/config/tsconfig.ts @@ -58,24 +58,28 @@ type TSConfigResult = Promise< /** * Load a tsconfig.json or jsconfig.json is the former is not found - * @param cwd Directory to start from - * @param resolve Determine if the function should go up directories like TypeScript would + * @param root The root directory to search in, defaults to `process.cwd()`. + * @param findUp Whether to search for the config file in parent directories, by default only the root directory is searched. */ export async function loadTSConfig( - cwd: string | undefined + root: string | undefined, + findUp = false ): Promise> { - const safeCwd = cwd ?? process.cwd(); + const safeCwd = root ?? process.cwd(); const [jsconfig, tsconfig] = await Promise.all( ['jsconfig.json', 'tsconfig.json'].map((configName) => - // `tsconfck` expects its first argument to be a file path, not a directory path, so we fake one - find(join(safeCwd, './dummy.txt'), { root: cwd, configName: configName }) + // `tsconfck` expects its first argument to be a file path, not a directory path, so we'll fake one + find(join(safeCwd, './dummy.txt'), { + root: findUp ? root : undefined, + configName: configName, + }) ) ); // If we have both files, prefer tsconfig.json if (tsconfig) { - const parsedConfig = await safeParse(tsconfig, { root: cwd }); + const parsedConfig = await safeParse(tsconfig, { root: root }); if (typeof parsedConfig === 'string') { return parsedConfig; @@ -85,7 +89,7 @@ export async function loadTSConfig( } if (jsconfig) { - const parsedConfig = await safeParse(jsconfig, { root: cwd }); + const parsedConfig = await safeParse(jsconfig, { root: root }); if (typeof parsedConfig === 'string') { return parsedConfig; diff --git a/packages/astro/test/units/config/config-tsconfig.test.js b/packages/astro/test/units/config/config-tsconfig.test.js index 09556d083..8617e35ea 100644 --- a/packages/astro/test/units/config/config-tsconfig.test.js +++ b/packages/astro/test/units/config/config-tsconfig.test.js @@ -14,7 +14,7 @@ describe('TSConfig handling', () => { }); it('can resolve tsconfig.json up directories', async () => { - const config = await loadTSConfig(path.join(cwd, 'nested-folder')); + const config = await loadTSConfig(cwd); expect(config).to.not.be.undefined; expect(config.tsconfigFile).to.equal(path.join(cwd, 'tsconfig.json')); @@ -39,19 +39,19 @@ describe('TSConfig handling', () => { }); describe('tsconfig / jsconfig updates', () => { - it('can update a tsconfig with a framework config', () => { - const config = loadTSConfig(cwd); - const updatedConfig = updateTSConfigForFramework(config.config, 'react'); + it('can update a tsconfig with a framework config', async () => { + const config = await loadTSConfig(cwd); + const updatedConfig = updateTSConfigForFramework(config.tsconfig, 'react'); - expect(config.config).to.not.equal('react-jsx'); + expect(config.tsconfig).to.not.equal('react-jsx'); expect(updatedConfig.compilerOptions.jsx).to.equal('react-jsx'); }); - it('produce no changes on invalid frameworks', () => { - const config = loadTSConfig(cwd); - const updatedConfig = updateTSConfigForFramework(config.config, 'doesnt-exist'); + it('produce no changes on invalid frameworks', async () => { + const config = await loadTSConfig(cwd); + const updatedConfig = updateTSConfigForFramework(config.tsconfig, 'doesnt-exist'); - expect(config.config).to.deep.equal(updatedConfig); + expect(config.tsconfig).to.deep.equal(updatedConfig); }); }); });