|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { AngularHostOptions } from '../angular-host'; |
| 10 | +import { |
| 11 | + AngularCompilation, |
| 12 | + AngularCompilationResult, |
| 13 | + NoopCompilation, |
| 14 | + createAngularCompilation, |
| 15 | +} from './index'; |
| 16 | + |
| 17 | +describe('AngularCompilation', () => { |
| 18 | + class CustomTransformCompilation extends AngularCompilation { |
| 19 | + override async initialize(): Promise<AngularCompilationResult> { |
| 20 | + return { |
| 21 | + compilerOptions: { allowJs: false }, |
| 22 | + referencedFiles: ['/src/main.ts'], |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + override async transformFile(filename: string, content: string) { |
| 27 | + if (filename.endsWith('.ts')) { |
| 28 | + return { |
| 29 | + contents: content + '\n// transformed', |
| 30 | + watchFiles: ['/src/dep.ts'], |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + return null; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + it('allows implementing on-demand transformFile without collectDiagnostics', async () => { |
| 39 | + const compilation = new CustomTransformCompilation(); |
| 40 | + const initResult = await compilation.initialize(); |
| 41 | + |
| 42 | + expect(initResult.referencedFiles).toEqual(['/src/main.ts']); |
| 43 | + expect(initResult.compilerOptions.allowJs).toBe(false); |
| 44 | + |
| 45 | + const transformResult = await compilation.transformFile?.( |
| 46 | + '/src/main.ts', |
| 47 | + 'console.log("hello");', |
| 48 | + ); |
| 49 | + expect(transformResult).toEqual({ |
| 50 | + contents: 'console.log("hello");\n// transformed', |
| 51 | + watchFiles: ['/src/dep.ts'], |
| 52 | + }); |
| 53 | + |
| 54 | + const diagnostics = await compilation.diagnoseFiles(); |
| 55 | + expect(diagnostics).toEqual({}); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('NoopCompilation', () => { |
| 59 | + it('initializes with empty referencedFiles and compiler options', async () => { |
| 60 | + const compilation = new NoopCompilation(); |
| 61 | + const mockHostOptions = {} as AngularHostOptions; |
| 62 | + const result = await compilation.initialize('tsconfig.json', mockHostOptions, (opts) => ({ |
| 63 | + ...opts, |
| 64 | + customOption: true, |
| 65 | + })); |
| 66 | + |
| 67 | + expect(result.referencedFiles).toEqual([]); |
| 68 | + expect(result.compilerOptions['customOption']).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it('throws when calling collectDiagnostics or emitAffectedFiles', () => { |
| 72 | + const compilation = new NoopCompilation(); |
| 73 | + expect(() => |
| 74 | + (compilation as unknown as { collectDiagnostics(): unknown }).collectDiagnostics(), |
| 75 | + ).toThrowError('Not available when using noop compilation.'); |
| 76 | + expect(() => compilation.emitAffectedFiles()).toThrowError( |
| 77 | + 'Not available when using noop compilation.', |
| 78 | + ); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('createAngularCompilation', () => { |
| 83 | + it('creates JitCompilation when mode is "jit"', async () => { |
| 84 | + const compilation = await createAngularCompilation('jit', true, false); |
| 85 | + expect(compilation.constructor.name).toBe('JitCompilation'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('creates AotCompilation when mode is "aot"', async () => { |
| 89 | + const compilation = await createAngularCompilation('aot', true, false); |
| 90 | + expect(compilation.constructor.name).toBe('AotCompilation'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('creates JitCompilation when mode is boolean true (legacy)', async () => { |
| 94 | + const compilation = await createAngularCompilation(true, true, false); |
| 95 | + expect(compilation.constructor.name).toBe('JitCompilation'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('creates AotCompilation when mode is boolean false (legacy)', async () => { |
| 99 | + const compilation = await createAngularCompilation(false, true, false); |
| 100 | + expect(compilation.constructor.name).toBe('AotCompilation'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('throws when mode is "transform"', async () => { |
| 104 | + await expectAsync(createAngularCompilation('transform', true, false)).toBeRejectedWithError( |
| 105 | + 'Transform compilation mode is not supported.', |
| 106 | + ); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments