|
| 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 | +import { JsonObject } from '@angular-devkit/core'; |
| 9 | +import { EmptyTree } from '@angular-devkit/schematics'; |
| 10 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 11 | +import { BuilderTarget, Builders, ProjectType, WorkspaceSchema } from '../../utility/workspace-models'; |
| 12 | + |
| 13 | +function getBuildTarget(tree: UnitTestTree): BuilderTarget<Builders.Browser, JsonObject> { |
| 14 | + return JSON.parse(tree.readContent('/angular.json')).projects.app.architect.build; |
| 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.Application, |
| 25 | + prefix: 'app', |
| 26 | + architect: { |
| 27 | + build: { |
| 28 | + builder: Builders.Browser, |
| 29 | + options: { |
| 30 | + skipAppShell: true, |
| 31 | + sourceMap: true, |
| 32 | + vendorSourceMap: true, |
| 33 | + evalSourceMap: false, |
| 34 | + buildOptimizer: true, |
| 35 | + // tslint:disable-next-line:no-any |
| 36 | + } as any, |
| 37 | + configurations: { |
| 38 | + one: { |
| 39 | + sourceMap: false, |
| 40 | + vendorSourceMap: false, |
| 41 | + skipAppShell: false, |
| 42 | + evalSourceMap: true, |
| 43 | + buildOptimizer: false, |
| 44 | + }, |
| 45 | + two: { |
| 46 | + sourceMap: { |
| 47 | + styles: false, |
| 48 | + scripts: true, |
| 49 | + }, |
| 50 | + evalSourceMap: true, |
| 51 | + vendorSourceMap: false, |
| 52 | + skipAppShell: true, |
| 53 | + buildOptimizer: true, |
| 54 | + }, |
| 55 | + // tslint:disable-next-line:no-any |
| 56 | + } as any, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }; |
| 62 | + |
| 63 | + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); |
| 64 | +} |
| 65 | + |
| 66 | +describe(`Migration to update 'angular.json'`, () => { |
| 67 | + const schematicName = 'update-angular-config'; |
| 68 | + |
| 69 | + const schematicRunner = new SchematicTestRunner( |
| 70 | + 'migrations', |
| 71 | + require.resolve('../migration-collection.json'), |
| 72 | + ); |
| 73 | + |
| 74 | + let tree: UnitTestTree; |
| 75 | + beforeEach(() => { |
| 76 | + tree = new UnitTestTree(new EmptyTree()); |
| 77 | + createWorkSpaceConfig(tree); |
| 78 | + }); |
| 79 | + |
| 80 | + it(`should remove 'skipAppShell'`, async () => { |
| 81 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 82 | + const { options, configurations } = getBuildTarget(newTree); |
| 83 | + |
| 84 | + expect(options.skipAppShell).toBeUndefined(); |
| 85 | + expect(configurations).toBeDefined(); |
| 86 | + expect(configurations?.one.skipAppShell).toBeUndefined(); |
| 87 | + expect(configurations?.two.skipAppShell).toBeUndefined(); |
| 88 | + }); |
| 89 | + |
| 90 | + it(`should remove 'evalSourceMap'`, async () => { |
| 91 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 92 | + const { options, configurations } = getBuildTarget(newTree); |
| 93 | + |
| 94 | + expect(options.evalSourceMap).toBeUndefined(); |
| 95 | + expect(configurations).toBeDefined(); |
| 96 | + expect(configurations?.one.evalSourceMap).toBeUndefined(); |
| 97 | + expect(configurations?.two.evalSourceMap).toBeUndefined(); |
| 98 | + }); |
| 99 | + |
| 100 | + it(`should remove 'vendorSourceMap' and set 'vendor' option in 'sourceMap'`, async () => { |
| 101 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 102 | + const { options, configurations } = getBuildTarget(newTree); |
| 103 | + |
| 104 | + expect(options.vendorSourceMap).toBeUndefined(); |
| 105 | + expect(options.sourceMap).toEqual({ |
| 106 | + vendor: true, |
| 107 | + scripts: true, |
| 108 | + styles: true, |
| 109 | + }); |
| 110 | + expect(options.vendorSourceMap).toBeUndefined(); |
| 111 | + |
| 112 | + expect(configurations).toBeDefined(); |
| 113 | + expect(configurations?.one.vendorSourceMap).toBeUndefined(); |
| 114 | + expect(configurations?.one.sourceMap).toBe(false); |
| 115 | + |
| 116 | + expect(configurations?.two.vendorSourceMap).toBeUndefined(); |
| 117 | + expect(configurations?.two.sourceMap).toEqual({ |
| 118 | + vendor: false, |
| 119 | + scripts: true, |
| 120 | + styles: false, |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments