|
| 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.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { EmptyTree } from '@angular-devkit/schematics'; |
| 10 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 11 | +import { Builders, ProjectType, WorkspaceSchema } from '../../utility/workspace-models'; |
| 12 | + |
| 13 | +function readJsonFile(tree: UnitTestTree, path: string): Record<string, Record<string, unknown>> { |
| 14 | + return JSON.parse(tree.readContent(path)); |
| 15 | +} |
| 16 | + |
| 17 | +function createWorkSpaceConfig(tree: UnitTestTree) { |
| 18 | + const angularConfig: WorkspaceSchema = { |
| 19 | + version: 1, |
| 20 | + projects: { |
| 21 | + app: { |
| 22 | + root: '', |
| 23 | + sourceRoot: 'src', |
| 24 | + projectType: ProjectType.Library, |
| 25 | + prefix: 'app', |
| 26 | + architect: { |
| 27 | + build: { |
| 28 | + builder: Builders.NgPackagr, |
| 29 | + options: { |
| 30 | + project: 'ngpackage.json', |
| 31 | + tsConfig: 'tsconfig.lib.json', |
| 32 | + }, |
| 33 | + configurations: { |
| 34 | + production: { |
| 35 | + tsConfig: 'tsconfig.lib.prod.json', |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); |
| 45 | + tree.create( |
| 46 | + '/tsconfig.lib.json', |
| 47 | + JSON.stringify( |
| 48 | + { angularCompilerOptions: { enableIvy: true, fullTemplateTypeCheck: true } }, |
| 49 | + undefined, |
| 50 | + 2, |
| 51 | + ), |
| 52 | + ); |
| 53 | + tree.create( |
| 54 | + '/tsconfig.lib.prod.json', |
| 55 | + JSON.stringify( |
| 56 | + { angularCompilerOptions: { enableIvy: false, fullTemplateTypeCheck: true } }, |
| 57 | + undefined, |
| 58 | + 2, |
| 59 | + ), |
| 60 | + ); |
| 61 | + |
| 62 | + tree.create( |
| 63 | + '/ngpackage.json', |
| 64 | + JSON.stringify( |
| 65 | + { |
| 66 | + lib: { entryFile: 'src/public-api.ts', amdId: 'foo', umdId: 'foo', umdModuleIds: ['foo'] }, |
| 67 | + }, |
| 68 | + undefined, |
| 69 | + 2, |
| 70 | + ), |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +const schematicName = 'update-libraries-v13'; |
| 75 | + |
| 76 | +describe(`Migration to update library projects. ${schematicName}`, () => { |
| 77 | + const schematicRunner = new SchematicTestRunner( |
| 78 | + 'migrations', |
| 79 | + require.resolve('../migration-collection.json'), |
| 80 | + ); |
| 81 | + |
| 82 | + let tree: UnitTestTree; |
| 83 | + beforeEach(() => { |
| 84 | + tree = new UnitTestTree(new EmptyTree()); |
| 85 | + createWorkSpaceConfig(tree); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('TypeScript Config', () => { |
| 89 | + it(`should replace "enableIvy: false" with "compilationMode: "partial" `, async () => { |
| 90 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 91 | + const { angularCompilerOptions } = readJsonFile(newTree, 'tsconfig.lib.prod.json'); |
| 92 | + expect(angularCompilerOptions.compilationMode).toBe('partial'); |
| 93 | + expect(angularCompilerOptions.enableIvy).toBeUndefined(); |
| 94 | + }); |
| 95 | + |
| 96 | + it(`should not replace "enableIvy: true"`, async () => { |
| 97 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 98 | + const { angularCompilerOptions } = readJsonFile(newTree, 'tsconfig.lib.json'); |
| 99 | + expect(angularCompilerOptions.enableIvy).toBeTrue(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('Ng-packagr Config', () => { |
| 104 | + it(`should remove UMD related options from ng-packagr configuration referenced from angular.json`, async () => { |
| 105 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 106 | + const { lib } = readJsonFile(newTree, 'ngpackage.json'); |
| 107 | + expect(lib.entryFile).toBeDefined(); |
| 108 | + expect(lib.amdId).toBeUndefined(); |
| 109 | + expect(lib.umdId).toBeUndefined(); |
| 110 | + expect(lib.umdModuleIds).toBeUndefined(); |
| 111 | + }); |
| 112 | + |
| 113 | + it(`should remove UMD related options from un-referenced ng-packagr configuration (secondary entry-points)`, async () => { |
| 114 | + tree.create( |
| 115 | + '/testing/ng-package.json', |
| 116 | + JSON.stringify( |
| 117 | + { |
| 118 | + lib: { |
| 119 | + entryFile: 'src/public-api.ts', |
| 120 | + amdId: 'foo', |
| 121 | + umdId: 'foo', |
| 122 | + umdModuleIds: ['foo'], |
| 123 | + }, |
| 124 | + }, |
| 125 | + undefined, |
| 126 | + 2, |
| 127 | + ), |
| 128 | + ); |
| 129 | + |
| 130 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 131 | + const { lib } = readJsonFile(newTree, 'testing/ng-package.json'); |
| 132 | + expect(lib.entryFile).toBeDefined(); |
| 133 | + expect(lib.amdId).toBeUndefined(); |
| 134 | + expect(lib.umdId).toBeUndefined(); |
| 135 | + expect(lib.umdModuleIds).toBeUndefined(); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments