|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import type { Tsconfig } from '../types'; |
| 4 | +import { getImportExtension } from './tsconfig'; |
| 5 | + |
| 6 | +describe('getImportExtension', () => { |
| 7 | + it('strips a .ts file extension when no tsconfig is provided', () => { |
| 8 | + expect(getImportExtension('.ts')).toBe(''); |
| 9 | + expect(getImportExtension('.gen.ts')).toBe('.gen'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('preserves non-.ts file extensions when no tsconfig is provided', () => { |
| 13 | + expect(getImportExtension('.mjs')).toBe('.mjs'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('keeps the file extension as-is when allowImportingTsExtensions is true', () => { |
| 17 | + const tsconfig: Tsconfig = { |
| 18 | + compilerOptions: { allowImportingTsExtensions: true }, |
| 19 | + }; |
| 20 | + expect(getImportExtension('.gen.ts', tsconfig)).toBe('.gen.ts'); |
| 21 | + expect(getImportExtension('.ts', tsconfig)).toBe('.ts'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('rewrites .ts to .js when module is NodeNext', () => { |
| 25 | + const tsconfig: Tsconfig = { |
| 26 | + compilerOptions: { module: 'NodeNext' }, |
| 27 | + }; |
| 28 | + expect(getImportExtension('.ts', tsconfig)).toBe('.js'); |
| 29 | + expect(getImportExtension('.gen.ts', tsconfig)).toBe('.gen.js'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('rewrites .ts to .js when moduleResolution is Node16', () => { |
| 33 | + const tsconfig: Tsconfig = { |
| 34 | + compilerOptions: { moduleResolution: 'Node16' }, |
| 35 | + }; |
| 36 | + expect(getImportExtension('.ts', tsconfig)).toBe('.js'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('matches NodeNext/Node16 case-insensitively', () => { |
| 40 | + expect( |
| 41 | + getImportExtension('.ts', { compilerOptions: { module: 'nodenext' } }), |
| 42 | + ).toBe('.js'); |
| 43 | + expect( |
| 44 | + getImportExtension('.ts', { |
| 45 | + compilerOptions: { moduleResolution: 'node16' }, |
| 46 | + }), |
| 47 | + ).toBe('.js'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('prefers allowImportingTsExtensions over NodeNext rewrites', () => { |
| 51 | + const tsconfig: Tsconfig = { |
| 52 | + compilerOptions: { |
| 53 | + module: 'NodeNext', |
| 54 | + allowImportingTsExtensions: true, |
| 55 | + }, |
| 56 | + }; |
| 57 | + expect(getImportExtension('.gen.ts', tsconfig)).toBe('.gen.ts'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('falls back to stripping .ts for other module settings', () => { |
| 61 | + const tsconfig: Tsconfig = { |
| 62 | + compilerOptions: { module: 'ESNext' }, |
| 63 | + }; |
| 64 | + expect(getImportExtension('.ts', tsconfig)).toBe(''); |
| 65 | + expect(getImportExtension('.gen.ts', tsconfig)).toBe('.gen'); |
| 66 | + }); |
| 67 | +}); |
0 commit comments