|
| 1 | +import { getInheritedFileScope } from '#/compilers/getInheritedFileScope'; |
| 2 | +import { posixJoin } from '#/modules/path/modules/posixJoin'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +const examplesDir = posixJoin(process.cwd(), 'examples'); |
| 6 | + |
| 7 | +describe('getInheritedFileScope', () => { |
| 8 | + it('returns include and exclude directly declared in the file', () => { |
| 9 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.base.json'); |
| 10 | + const result = getInheritedFileScope(tsconfigPath); |
| 11 | + |
| 12 | + expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']); |
| 13 | + expect(result.exclude).toEqual(['dist/**', 'node_modules/**']); |
| 14 | + }); |
| 15 | + |
| 16 | + it('inherits include from base when child has no include', () => { |
| 17 | + // tsconfig.inherit.mid.json extends base and has only exclude |
| 18 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.mid.json'); |
| 19 | + const result = getInheritedFileScope(tsconfigPath); |
| 20 | + |
| 21 | + expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']); |
| 22 | + expect(result.exclude).toEqual(['dist/**', 'node_modules/**', '**/*.test.ts']); |
| 23 | + }); |
| 24 | + |
| 25 | + it('traverses two levels to find include', () => { |
| 26 | + // tsconfig.inherit.leaf.json → mid (no include) → base (has include) |
| 27 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.leaf.json'); |
| 28 | + const result = getInheritedFileScope(tsconfigPath); |
| 29 | + |
| 30 | + expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']); |
| 31 | + expect(result.exclude).toEqual(['dist/**', 'node_modules/**', '**/*.test.ts']); |
| 32 | + }); |
| 33 | + |
| 34 | + it('child include overrides base include', () => { |
| 35 | + // tsconfig.inherit.override.json extends base but declares its own include |
| 36 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.override.json'); |
| 37 | + const result = getInheritedFileScope(tsconfigPath); |
| 38 | + |
| 39 | + expect(result.include).toEqual(['src/**/*.ts']); |
| 40 | + expect(result.exclude).toEqual(['dist/**', 'node_modules/**']); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns empty arrays when no include or exclude found in chain', () => { |
| 44 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.empty.json'); |
| 45 | + const result = getInheritedFileScope(tsconfigPath); |
| 46 | + |
| 47 | + expect(result.include).toEqual([]); |
| 48 | + expect(result.exclude).toEqual([]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns only exclude when chain has no include', () => { |
| 52 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.no-include.json'); |
| 53 | + const result = getInheritedFileScope(tsconfigPath); |
| 54 | + |
| 55 | + expect(result.include).toEqual([]); |
| 56 | + expect(result.exclude).toEqual(['dist/**']); |
| 57 | + }); |
| 58 | + |
| 59 | + it('does not loop infinitely on a non-existent extends target', () => { |
| 60 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.base.json'); |
| 61 | + // base has no extends, so traversal stops after one file |
| 62 | + const result = getInheritedFileScope(tsconfigPath); |
| 63 | + |
| 64 | + expect(result).toBeDefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('handles array extends (TypeScript 5.0+) and inherits include from the first entry', () => { |
| 68 | + // tsconfig.inherit.array-extends.json: { "extends": ["./tsconfig.inherit.base.json"] } |
| 69 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.array-extends.json'); |
| 70 | + const result = getInheritedFileScope(tsconfigPath); |
| 71 | + |
| 72 | + expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']); |
| 73 | + expect(result.exclude).toEqual(['dist/**', 'node_modules/**']); |
| 74 | + }); |
| 75 | + |
| 76 | + it('resolves extends path that omits the .json extension', () => { |
| 77 | + // tsconfig.inherit.no-ext.json: { "extends": "./tsconfig.inherit.base" } |
| 78 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.no-ext.json'); |
| 79 | + const result = getInheritedFileScope(tsconfigPath); |
| 80 | + |
| 81 | + expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']); |
| 82 | + expect(result.exclude).toEqual(['dist/**', 'node_modules/**']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('stops traversal when extends array contains a non-string entry', () => { |
| 86 | + // tsconfig.inherit.invalid-extends.json: { "extends": [123], "include": [...] } |
| 87 | + // has its own include, but extends[0] is a number → traversal stops before following parent |
| 88 | + const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.invalid-extends.json'); |
| 89 | + const result = getInheritedFileScope(tsconfigPath); |
| 90 | + |
| 91 | + expect(result.include).toEqual(['src/**/*.ts']); |
| 92 | + expect(result.exclude).toEqual([]); |
| 93 | + }); |
| 94 | +}); |
0 commit comments