|
| 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 { JsonObject } from '@angular-devkit/core'; |
| 10 | +import { EmptyTree } from '@angular-devkit/schematics'; |
| 11 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 12 | +import { |
| 13 | + BuilderTarget, |
| 14 | + Builders, |
| 15 | + ProjectType, |
| 16 | + WorkspaceSchema, |
| 17 | +} from '../../utility/workspace-models'; |
| 18 | + |
| 19 | +function getServerTarget(tree: UnitTestTree): BuilderTarget<Builders.Server, JsonObject> { |
| 20 | + const target = (tree.readJson('/angular.json') as unknown as WorkspaceSchema).projects.app |
| 21 | + .architect?.server; |
| 22 | + |
| 23 | + return target as unknown as BuilderTarget<Builders.Server, JsonObject>; |
| 24 | +} |
| 25 | + |
| 26 | +function createWorkSpaceConfig(tree: UnitTestTree) { |
| 27 | + const angularConfig: WorkspaceSchema = { |
| 28 | + version: 1, |
| 29 | + projects: { |
| 30 | + app: { |
| 31 | + root: '', |
| 32 | + sourceRoot: 'src', |
| 33 | + projectType: ProjectType.Application, |
| 34 | + prefix: 'app', |
| 35 | + architect: { |
| 36 | + server: { |
| 37 | + builder: Builders.Server, |
| 38 | + options: { |
| 39 | + main: './server.ts', |
| 40 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 41 | + } as any, |
| 42 | + configurations: { |
| 43 | + development: { |
| 44 | + optimization: false, |
| 45 | + }, |
| 46 | + production: { |
| 47 | + optimization: true, |
| 48 | + }, |
| 49 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 50 | + } as any, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); |
| 58 | +} |
| 59 | + |
| 60 | +const schematicName = 'update-server-builder-config'; |
| 61 | + |
| 62 | +describe(`Migration to update '@angular-devkit/build-angular:server' options. ${schematicName}`, () => { |
| 63 | + const schematicRunner = new SchematicTestRunner( |
| 64 | + 'migrations', |
| 65 | + require.resolve('../migration-collection.json'), |
| 66 | + ); |
| 67 | + |
| 68 | + let tree: UnitTestTree; |
| 69 | + beforeEach(() => { |
| 70 | + tree = new UnitTestTree(new EmptyTree()); |
| 71 | + createWorkSpaceConfig(tree); |
| 72 | + }); |
| 73 | + |
| 74 | + it(`should add 'buildOptimizer: false' to 'optimization' is 'false'`, async () => { |
| 75 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 76 | + const { configurations } = getServerTarget(newTree); |
| 77 | + expect(configurations?.development.buildOptimizer).toBeFalse(); |
| 78 | + }); |
| 79 | + |
| 80 | + it(`should not add 'buildOptimizer' option when to 'optimization' is not defined.`, async () => { |
| 81 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 82 | + const { options } = getServerTarget(newTree); |
| 83 | + expect(options.buildOptimizer).toBeUndefined(); |
| 84 | + }); |
| 85 | + |
| 86 | + it(`should add 'buildOptimizer: true' to 'optimization' is 'true'`, async () => { |
| 87 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 88 | + const { configurations } = getServerTarget(newTree); |
| 89 | + expect(configurations?.production.buildOptimizer).toBeTrue(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments