|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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 { WorkspaceTargets } from '../../utility/workspace-models'; |
| 12 | + |
| 13 | +// tslint:disable-next-line: no-any |
| 14 | +function getWorkspaceTargets(tree: UnitTestTree): any { |
| 15 | + return JSON.parse(tree.readContent(workspacePath)) |
| 16 | + .projects['migration-lib'].architect; |
| 17 | +} |
| 18 | + |
| 19 | +function updateWorkspaceTargets(tree: UnitTestTree, workspaceTargets: WorkspaceTargets) { |
| 20 | + const config = JSON.parse(tree.readContent(workspacePath)); |
| 21 | + config.projects['migration-lib'].architect = workspaceTargets; |
| 22 | + tree.overwrite(workspacePath, JSON.stringify(config, undefined, 2)); |
| 23 | +} |
| 24 | + |
| 25 | +const workspacePath = '/angular.json'; |
| 26 | +const libProdTsConfig = 'migration-lib/tsconfig.lib.prod.json'; |
| 27 | + |
| 28 | +// tslint:disable:no-big-function |
| 29 | +describe('Migration to version 9', () => { |
| 30 | + describe('Migrate Ivy Libraries', () => { |
| 31 | + const schematicRunner = new SchematicTestRunner( |
| 32 | + 'migrations', |
| 33 | + require.resolve('../migration-collection.json'), |
| 34 | + ); |
| 35 | + |
| 36 | + let tree: UnitTestTree; |
| 37 | + |
| 38 | + beforeEach(async () => { |
| 39 | + tree = new UnitTestTree(new EmptyTree()); |
| 40 | + tree = await schematicRunner |
| 41 | + .runExternalSchematicAsync( |
| 42 | + require.resolve('../../collection.json'), |
| 43 | + 'workspace', |
| 44 | + { |
| 45 | + name: 'migration-test', |
| 46 | + version: '1.2.3', |
| 47 | + directory: '.', |
| 48 | + }, |
| 49 | + tree, |
| 50 | + ) |
| 51 | + .toPromise(); |
| 52 | + tree = await schematicRunner |
| 53 | + .runExternalSchematicAsync( |
| 54 | + require.resolve('../../collection.json'), |
| 55 | + 'library', |
| 56 | + { |
| 57 | + name: 'migration-lib', |
| 58 | + }, |
| 59 | + tree, |
| 60 | + ) |
| 61 | + .toPromise(); |
| 62 | + }); |
| 63 | + |
| 64 | + it(`should add 'tsConfig' option in production when configurations doesn't exists`, async () => { |
| 65 | + let config = getWorkspaceTargets(tree); |
| 66 | + config.build.configurations = undefined; |
| 67 | + |
| 68 | + updateWorkspaceTargets(tree, config); |
| 69 | + |
| 70 | + const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); |
| 71 | + config = getWorkspaceTargets(tree2).build; |
| 72 | + expect(config.configurations.production.tsConfig).toEqual(libProdTsConfig); |
| 73 | + expect(tree2.exists(libProdTsConfig)).toBeTruthy(); |
| 74 | + }); |
| 75 | + |
| 76 | + it(`should add 'tsConfig' option in production when configurations exists`, async () => { |
| 77 | + let config = getWorkspaceTargets(tree); |
| 78 | + config.build.configurations = {}; |
| 79 | + |
| 80 | + updateWorkspaceTargets(tree, config); |
| 81 | + |
| 82 | + const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); |
| 83 | + config = getWorkspaceTargets(tree2).build; |
| 84 | + expect(config.configurations.production.tsConfig).toEqual(libProdTsConfig); |
| 85 | + expect(tree2.exists(libProdTsConfig)).toBeTruthy(); |
| 86 | + }); |
| 87 | + |
| 88 | + it(`should add 'tsConfig' option in production when production configurations exists`, async () => { |
| 89 | + let config = getWorkspaceTargets(tree); |
| 90 | + config.build.configurations = { production: {} }; |
| 91 | + |
| 92 | + updateWorkspaceTargets(tree, config); |
| 93 | + |
| 94 | + const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); |
| 95 | + config = getWorkspaceTargets(tree2).build; |
| 96 | + expect(config.configurations.production.tsConfig).toEqual(libProdTsConfig); |
| 97 | + expect(tree2.exists(libProdTsConfig)).toBeTruthy(); |
| 98 | + }); |
| 99 | + |
| 100 | + it(`should add enableIvy false in prod tsconfig if already exists`, async () => { |
| 101 | + let config = getWorkspaceTargets(tree); |
| 102 | + const prodLibTsConfig = 'migration-lib/tsconfig.library.prod.json'; |
| 103 | + config.build.configurations = { production: { tsConfig: prodLibTsConfig } }; |
| 104 | + |
| 105 | + updateWorkspaceTargets(tree, config); |
| 106 | + |
| 107 | + const tsconfig = { |
| 108 | + compilerOptions: {}, |
| 109 | + }; |
| 110 | + |
| 111 | + tree.create(prodLibTsConfig, JSON.stringify(tsconfig, undefined, 2)); |
| 112 | + |
| 113 | + const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); |
| 114 | + config = getWorkspaceTargets(tree2).build; |
| 115 | + expect(config.configurations.production.tsConfig).toEqual(prodLibTsConfig); |
| 116 | + |
| 117 | + const tsConfigContent = tree2.readContent(prodLibTsConfig); |
| 118 | + expect(JSON.parse(tsConfigContent).angularCompilerOptions).toEqual({ enableIvy: false }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should set enableIvy to false in prod tsconfig if true', async () => { |
| 122 | + let config = getWorkspaceTargets(tree); |
| 123 | + const prodLibTsConfig = 'migration-lib/tsconfig.library.prod.json'; |
| 124 | + config.build.configurations = { production: { tsConfig: prodLibTsConfig } }; |
| 125 | + |
| 126 | + updateWorkspaceTargets(tree, config); |
| 127 | + |
| 128 | + const tsconfig = { |
| 129 | + compilerOptions: {}, |
| 130 | + angularCompilerOptions: { |
| 131 | + enableIvy: true, |
| 132 | + }, |
| 133 | + }; |
| 134 | + |
| 135 | + tree.create(prodLibTsConfig, JSON.stringify(tsconfig, undefined, 2)); |
| 136 | + |
| 137 | + const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); |
| 138 | + config = getWorkspaceTargets(tree2).build; |
| 139 | + expect(config.configurations.production.tsConfig).toEqual(prodLibTsConfig); |
| 140 | + |
| 141 | + const tsConfigContent = tree2.readContent(prodLibTsConfig); |
| 142 | + expect(JSON.parse(tsConfigContent).angularCompilerOptions).toEqual({ enableIvy: false }); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments