|
| 1 | +import { EmptyTree, type Tree } from '@angular-devkit/schematics'; |
| 2 | + |
| 3 | +import { generateFederationTsConfig } from './generate-federation-tsconfig.js'; |
| 4 | +import type { NormalizedOptions } from './normalize-options.js'; |
| 5 | + |
| 6 | +const EXPOSED = ['projects/mfe1/src/app/app.ts']; |
| 7 | + |
| 8 | +function makeOptions(overrides: Partial<NormalizedOptions> = {}): NormalizedOptions { |
| 9 | + return { |
| 10 | + polyfills: [] as unknown as string, |
| 11 | + projectName: 'mfe1', |
| 12 | + projectRoot: 'projects/mfe1', |
| 13 | + projectSourceRoot: 'projects/mfe1/src', |
| 14 | + manifestPath: '', |
| 15 | + manifestRelPath: '', |
| 16 | + main: 'projects/mfe1/src/main.ts', |
| 17 | + port: 4200, |
| 18 | + projectConfig: { |
| 19 | + architect: { |
| 20 | + build: { |
| 21 | + builder: '@angular/build:application', |
| 22 | + options: { tsConfig: 'projects/mfe1/tsconfig.app.json' }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + ...overrides, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +function read(tree: Tree, path: string) { |
| 31 | + return JSON.parse(tree.read(path)!.toString('utf8')); |
| 32 | +} |
| 33 | + |
| 34 | +describe('generateFederationTsConfig', () => { |
| 35 | + let tree: Tree; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + tree = new EmptyTree(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('creates a federation tsconfig extending the app tsconfig', () => { |
| 42 | + const result = generateFederationTsConfig(tree, makeOptions(), EXPOSED); |
| 43 | + |
| 44 | + expect(result).toBe('projects/mfe1/tsconfig.federation.json'); |
| 45 | + expect(read(tree, result)).toEqual({ |
| 46 | + extends: './tsconfig.app.json', |
| 47 | + files: ['src/app/app.ts'], |
| 48 | + include: ['src/**/*.d.ts'], |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + // An empty `files` list is a TypeScript error (TS18002) unless the config also extends |
| 53 | + // another one, so neither key may be dropped from the generated shape. |
| 54 | + it('always emits both extends and a non-empty files list', () => { |
| 55 | + const result = generateFederationTsConfig(tree, makeOptions(), [ |
| 56 | + 'projects/mfe1/src/main.ts', |
| 57 | + ]); |
| 58 | + |
| 59 | + const tsconfig = read(tree, result); |
| 60 | + expect(tsconfig.extends).toBeTruthy(); |
| 61 | + expect(tsconfig.files).toEqual(['src/main.ts']); |
| 62 | + }); |
| 63 | + |
| 64 | + it('derives the include glob from the project source root', () => { |
| 65 | + const result = generateFederationTsConfig( |
| 66 | + tree, |
| 67 | + makeOptions({ projectSourceRoot: 'projects/mfe1/app-src' }), |
| 68 | + EXPOSED |
| 69 | + ); |
| 70 | + |
| 71 | + expect(read(tree, result).include).toEqual(['app-src/**/*.d.ts']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('points extends at a tsconfig that lives outside the project root', () => { |
| 75 | + const result = generateFederationTsConfig( |
| 76 | + tree, |
| 77 | + makeOptions({ |
| 78 | + projectConfig: { |
| 79 | + architect: { |
| 80 | + build: { |
| 81 | + builder: '@angular/build:application', |
| 82 | + options: { tsConfig: 'tsconfig.app.json' }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }), |
| 87 | + EXPOSED |
| 88 | + ); |
| 89 | + |
| 90 | + expect(read(tree, result).extends).toBe('../../tsconfig.app.json'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('leaves an existing federation tsconfig untouched', () => { |
| 94 | + tree.create('projects/mfe1/tsconfig.federation.json', '{ "files": ["src/bootstrap.ts"] }'); |
| 95 | + |
| 96 | + const result = generateFederationTsConfig(tree, makeOptions(), EXPOSED); |
| 97 | + |
| 98 | + expect(read(tree, result)).toEqual({ files: ['src/bootstrap.ts'] }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('does nothing when the project is already on the federation builder', () => { |
| 102 | + const options = makeOptions(); |
| 103 | + options.projectConfig.architect.build.builder = '@angular-architects/native-federation:build'; |
| 104 | + |
| 105 | + const result = generateFederationTsConfig(tree, options, EXPOSED); |
| 106 | + |
| 107 | + expect(tree.exists(result)).toBe(false); |
| 108 | + }); |
| 109 | + |
| 110 | + // esbuild is where a previous run parked the original build target. |
| 111 | + it('falls back to the esbuild target tsConfig', () => { |
| 112 | + const result = generateFederationTsConfig( |
| 113 | + tree, |
| 114 | + makeOptions({ |
| 115 | + projectConfig: { |
| 116 | + architect: { |
| 117 | + build: { builder: '@angular/build:application', options: {} }, |
| 118 | + esbuild: { options: { tsConfig: 'projects/mfe1/tsconfig.app.json' } }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }), |
| 122 | + EXPOSED |
| 123 | + ); |
| 124 | + |
| 125 | + expect(read(tree, result).extends).toBe('./tsconfig.app.json'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('throws when no tsConfig can be found', () => { |
| 129 | + expect(() => |
| 130 | + generateFederationTsConfig( |
| 131 | + tree, |
| 132 | + makeOptions({ |
| 133 | + projectConfig: { |
| 134 | + architect: { build: { builder: '@angular/build:application', options: {} } }, |
| 135 | + }, |
| 136 | + }), |
| 137 | + EXPOSED |
| 138 | + ) |
| 139 | + ).toThrow('has no tsConfig'); |
| 140 | + }); |
| 141 | +}); |
0 commit comments