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
* @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<TSConfigResult<{ rawConfig: TSConfckParseResult }>> {
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;

View file

@ -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);
});
});
});