|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { generateNameFromPath } from './test-discovery'; |
| 9 | +import { generateNameFromPath, getTestEntrypoints } from './test-discovery'; |
| 10 | + |
| 11 | +describe('getTestEntrypoints', () => { |
| 12 | + const workspaceRoot = '/project'; |
| 13 | + const projectSourceRoot = '/project/src'; |
| 14 | + const options = { workspaceRoot, projectSourceRoot }; |
| 15 | + |
| 16 | + it('should generate entry points for unique files', () => { |
| 17 | + const files = ['/project/src/a.spec.ts', '/project/src/b.spec.ts']; |
| 18 | + const result = getTestEntrypoints(files, { ...options, removeTestExtension: true }); |
| 19 | + |
| 20 | + expect(result.size).toBe(2); |
| 21 | + expect(result.get('spec-a')).toBe(files[0]); |
| 22 | + expect(result.get('spec-b')).toBe(files[1]); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should handle collisions with numeric suffixes correctly positioned', () => { |
| 26 | + // To trigger the regex replacement, the name must end in 'spec', 'test', or the prefix. |
| 27 | + const files = ['/project/src/sub-test.spec.ts', '/project/src/sub/test.spec.ts']; |
| 28 | + |
| 29 | + const result = getTestEntrypoints(files, { ...options, removeTestExtension: true }); |
| 30 | + // Both map to 'sub-test' (relative to src root). |
| 31 | + // baseName = 'spec-sub-test'. |
| 32 | + // 1st: 'spec-sub-test'. |
| 33 | + // 2nd: 'spec-sub-test-2'. Regex matches '-test-2'. -> 'spec-sub-2-test'. |
| 34 | + expect(result.get('spec-sub-test')).toBe(files[0]); |
| 35 | + expect(result.get('spec-sub-2-test')).toBe(files[1]); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should handle setup file naming with prefix setup', () => { |
| 39 | + const files = ['/project/src/setup.ts']; |
| 40 | + const result = getTestEntrypoints(files, { |
| 41 | + ...options, |
| 42 | + removeTestExtension: false, |
| 43 | + prefix: 'setup', |
| 44 | + }); |
| 45 | + |
| 46 | + // 'setup.ts' -> 'setup' (via generateNameFromPath). |
| 47 | + // prefix='setup'. |
| 48 | + // baseName = 'setup' === 'setup' ? 'setup' : 'setup-setup' -> 'setup'. |
| 49 | + expect(result.get('setup')).toBe(files[0]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should handle setup file collisions', () => { |
| 53 | + const files = ['/project/src/sub-setup.ts', '/project/src/sub/setup.ts']; |
| 54 | + const result = getTestEntrypoints(files, { |
| 55 | + ...options, |
| 56 | + removeTestExtension: false, |
| 57 | + prefix: 'setup', |
| 58 | + }); |
| 59 | + |
| 60 | + // Both map to 'sub-setup' (baseName: 'setup-sub-setup'). |
| 61 | + // 1st: 'setup-sub-setup'. |
| 62 | + // 2nd: 'setup-sub-setup-2'. Regex matches '-setup-2'. -> 'setup-sub-2-setup'. |
| 63 | + expect(result.get('setup-sub-setup')).toBe(files[0]); |
| 64 | + expect(result.get('setup-sub-2-setup')).toBe(files[1]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle custom prefixes', () => { |
| 68 | + const files = ['/project/src/sub-my-file.ts', '/project/src/sub/my-file.ts']; |
| 69 | + const result = getTestEntrypoints(files, { |
| 70 | + ...options, |
| 71 | + removeTestExtension: false, |
| 72 | + prefix: 'custom', |
| 73 | + }); |
| 74 | + |
| 75 | + // 'sub-my-file.ts' -> 'sub-my-file'. |
| 76 | + // baseName: 'custom-sub-my-file'. |
| 77 | + expect(result.get('custom-sub-my-file')).toBe(files[0]); |
| 78 | + // 'custom-sub-my-file-2'. Does not match regex (no 'spec'/'test'/'custom' at end). |
| 79 | + expect(result.get('custom-sub-my-file-2')).toBe(files[1]); |
| 80 | + }); |
| 81 | +}); |
10 | 82 |
|
11 | 83 | describe('generateNameFromPath', () => { |
12 | 84 | const roots = ['/project/src/', '/project/']; |
|
0 commit comments