Skip to content

Commit

Permalink
fix: handle relative tsconfig.json paths (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
  • Loading branch information
stefanprobst and privatenumber authored Jul 19, 2024
1 parent 232838b commit 9e78ec5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/parse-tsconfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,4 @@ const _parseTsconfig = (
export const parseTsconfig = (
tsconfigPath: string,
cache: Cache<string> = new Map(),
): TsConfigJsonResolved => _parseTsconfig(tsconfigPath, cache);
): TsConfigJsonResolved => _parseTsconfig(path.resolve(tsconfigPath), cache);
43 changes: 41 additions & 2 deletions tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
import { describe } from 'manten';
import { describe, test, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { createTsconfigJson, getTscResolution } from './utils.js';
import { getTsconfig, createPathsMatcher } from '#get-tsconfig';

describe('get-tsconfig', ({ runTestSuite }) => {
await describe('get-tsconfig', ({ runTestSuite }) => {
runTestSuite(import('./specs/get-tsconfig.js'));
runTestSuite(import('./specs/parse-tsconfig/index.js'));
runTestSuite(import('./specs/create-paths-matcher.js'));
runTestSuite(import('./specs/create-files-matcher.js'));
});

/**
* This needs to happen in isolation because it changes process.cwd
*
* TODO: either: 1) update code to not rely on cwd, or 2) update manten to handle parallel: false
*/
test('paths > prefix match > nested directory', async () => {
await using fixture = await createFixture({
'dir/tsconfig.json': createTsconfigJson({
compilerOptions: {
paths: {
'@/*': ['./*'],
},
},
}),
});

const cwd = process.cwd();

try {
process.chdir(fixture.path);

const tsconfig = getTsconfig('./dir/tsconfig.json');
expect(tsconfig).not.toBeNull();

const matcher = createPathsMatcher(tsconfig!)!;
expect(matcher).not.toBeNull();

const resolvedAttempts = await getTscResolution('@/file', fixture.getPath('./dir'));
expect(matcher('@/file')).toStrictEqual([
resolvedAttempts[0].filePath.slice(0, -3),
]);
} finally {
process.chdir(cwd);
}
});
2 changes: 1 addition & 1 deletion tests/specs/create-paths-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default testSuite(({ describe }) => {
});

describe('baseUrl', ({ test }) => {
test('baseUrl', async () => {
test('absolute path', async () => {
await using fixture = await createFixture({
'tsconfig.json': createTsconfigJson({
compilerOptions: {
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/parse-tsconfig/parses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default testSuite(({ describe }) => {
test('non-existent path', async () => {
expect(
() => parseTsconfig('non-existent-path'),
).toThrow('Cannot resolve tsconfig at path: non-existent-path');
).toThrow('Cannot resolve tsconfig at path: ');
});

test('empty file', async () => {
Expand Down

0 comments on commit 9e78ec5

Please sign in to comment.