test: fix

This commit is contained in:
Princesseuh 2023-10-10 19:24:58 +02:00
parent 12fc88f67e
commit 50f00f480f
No known key found for this signature in database
GPG key ID: 105BBD6D57F2B0C0
2 changed files with 21 additions and 17 deletions

View file

@ -58,24 +58,28 @@ type TSConfigResult<T = {}> = Promise<
/** /**
* Load a tsconfig.json or jsconfig.json is the former is not found * Load a tsconfig.json or jsconfig.json is the former is not found
* @param cwd Directory to start from * @param root The root directory to search in, defaults to `process.cwd()`.
* @param resolve Determine if the function should go up directories like TypeScript would * @param findUp Whether to search for the config file in parent directories, by default only the root directory is searched.
*/ */
export async function loadTSConfig( export async function loadTSConfig(
cwd: string | undefined root: string | undefined,
findUp = false
): Promise<TSConfigResult<{ rawConfig: TSConfckParseResult }>> { ): Promise<TSConfigResult<{ rawConfig: TSConfckParseResult }>> {
const safeCwd = cwd ?? process.cwd(); const safeCwd = root ?? process.cwd();
const [jsconfig, tsconfig] = await Promise.all( const [jsconfig, tsconfig] = await Promise.all(
['jsconfig.json', 'tsconfig.json'].map((configName) => ['jsconfig.json', 'tsconfig.json'].map((configName) =>
// `tsconfck` expects its first argument to be a file path, not a directory path, so we fake one // `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: cwd, configName: configName }) find(join(safeCwd, './dummy.txt'), {
root: findUp ? root : undefined,
configName: configName,
})
) )
); );
// If we have both files, prefer tsconfig.json // If we have both files, prefer tsconfig.json
if (tsconfig) { if (tsconfig) {
const parsedConfig = await safeParse(tsconfig, { root: cwd }); const parsedConfig = await safeParse(tsconfig, { root: root });
if (typeof parsedConfig === 'string') { if (typeof parsedConfig === 'string') {
return parsedConfig; return parsedConfig;
@ -85,7 +89,7 @@ export async function loadTSConfig(
} }
if (jsconfig) { if (jsconfig) {
const parsedConfig = await safeParse(jsconfig, { root: cwd }); const parsedConfig = await safeParse(jsconfig, { root: root });
if (typeof parsedConfig === 'string') { if (typeof parsedConfig === 'string') {
return parsedConfig; return parsedConfig;

View file

@ -14,7 +14,7 @@ describe('TSConfig handling', () => {
}); });
it('can resolve tsconfig.json up directories', async () => { 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).to.not.be.undefined;
expect(config.tsconfigFile).to.equal(path.join(cwd, 'tsconfig.json')); expect(config.tsconfigFile).to.equal(path.join(cwd, 'tsconfig.json'));
@ -39,19 +39,19 @@ describe('TSConfig handling', () => {
}); });
describe('tsconfig / jsconfig updates', () => { describe('tsconfig / jsconfig updates', () => {
it('can update a tsconfig with a framework config', () => { it('can update a tsconfig with a framework config', async () => {
const config = loadTSConfig(cwd); const config = await loadTSConfig(cwd);
const updatedConfig = updateTSConfigForFramework(config.config, 'react'); 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'); expect(updatedConfig.compilerOptions.jsx).to.equal('react-jsx');
}); });
it('produce no changes on invalid frameworks', () => { it('produce no changes on invalid frameworks', async () => {
const config = loadTSConfig(cwd); const config = await loadTSConfig(cwd);
const updatedConfig = updateTSConfigForFramework(config.config, 'doesnt-exist'); const updatedConfig = updateTSConfigForFramework(config.tsconfig, 'doesnt-exist');
expect(config.config).to.deep.equal(updatedConfig); expect(config.tsconfig).to.deep.equal(updatedConfig);
}); });
}); });
}); });