diff --git a/src/transformer.test.ts b/src/transformer.test.ts index 1b884c6..4914cb2 100644 --- a/src/transformer.test.ts +++ b/src/transformer.test.ts @@ -22,7 +22,7 @@ describe('fixImportPath', () => { compilerOptions: { baseUrl: './src', paths: { - '@utils/*': ['utils/*'], + '@utils/*': ['./utils/*'], }, }, }, @@ -117,4 +117,24 @@ describe('fixImportPath', () => { ); expect(result).toBeUndefined(); }); + + it('should not throw an error for module paths with tsConfig without base URL', () => { + fsExistsSyncMock.mockReturnValue(false); + const result = fixImportPath( + './root/src/file.ts', + 'vitest', + resolverCache, + { + ...tsConfig, + config: { + ...tsConfig.config, + compilerOptions: { + ...tsConfig.config.compilerOptions, + baseUrl: undefined, + }, + }, + }, + ); + expect(result).toBeUndefined(); + }); }); diff --git a/src/transformer.ts b/src/transformer.ts index b0a3500..8270b79 100644 --- a/src/transformer.ts +++ b/src/transformer.ts @@ -65,11 +65,15 @@ export function fixImportPath( .map((p) => findSpecifierSuffix(p)) .find((s) => s !== undefined); + const hasNonBaseUrlMatch = + matchingPaths.length === 1 && + !matchingPaths[0]?.endsWith(`/${specifier}`); + if ( !suffix && + matchingPaths.length > 0 && // catch scenario where we default to baseUrl which could be a normal module - (matchingPaths.length !== 1 || - !matchingPaths[0].endsWith(`/${specifier}`)) + hasNonBaseUrlMatch ) { throw new Error(`Could not find valid extension for ${specifier}`); }