|
| 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 createWorkSpaceConfig(tree: UnitTestTree) { |
| 14 | + const angularConfig: WorkspaceSchema = { |
| 15 | + version: 1, |
| 16 | + projects: { |
| 17 | + app: { |
| 18 | + root: '/project/lib', |
| 19 | + sourceRoot: '/project/app/src', |
| 20 | + projectType: ProjectType.Application, |
| 21 | + prefix: 'app', |
| 22 | + architect: { |
| 23 | + 'app-shell': { |
| 24 | + builder: Builders.AppShell, |
| 25 | + options: { |
| 26 | + browserTarget: 'app:build', |
| 27 | + serverTarget: 'app:server', |
| 28 | + route: '', |
| 29 | + }, |
| 30 | + configurations: { |
| 31 | + production: { |
| 32 | + browserTarget: 'app:build:production', |
| 33 | + serverTarget: 'app:server:production', |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + serve: { |
| 38 | + builder: Builders.DevServer, |
| 39 | + options: { |
| 40 | + browserTarget: 'app:build:development', |
| 41 | + }, |
| 42 | + configurations: { |
| 43 | + production: { |
| 44 | + browserTarget: 'app:build:production', |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + i18n: { |
| 49 | + builder: Builders.ExtractI18n, |
| 50 | + options: { |
| 51 | + browserTarget: 'app:build:production', |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); |
| 60 | +} |
| 61 | + |
| 62 | +describe(`Migration to update 'angular.json'.`, () => { |
| 63 | + const schematicName = 'update-workspace-config'; |
| 64 | + const schematicRunner = new SchematicTestRunner( |
| 65 | + 'migrations', |
| 66 | + require.resolve('../migration-collection.json'), |
| 67 | + ); |
| 68 | + |
| 69 | + let tree: UnitTestTree; |
| 70 | + beforeEach(() => { |
| 71 | + tree = new UnitTestTree(new EmptyTree()); |
| 72 | + createWorkSpaceConfig(tree); |
| 73 | + }); |
| 74 | + |
| 75 | + it(`should replace 'browserTarget' when using '@angular-devkit/build-angular:dev-server'`, async () => { |
| 76 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 77 | + const { |
| 78 | + projects: { app }, |
| 79 | + } = JSON.parse(newTree.readContent('/angular.json')); |
| 80 | + |
| 81 | + const { browserTarget, buildTarget } = app.architect['serve'].options; |
| 82 | + expect(browserTarget).toBeUndefined(); |
| 83 | + expect(buildTarget).toBe('app:build:development'); |
| 84 | + |
| 85 | + const { browserTarget: browserTargetProd, buildTarget: buildTargetProd } = |
| 86 | + app.architect['serve'].configurations['production']; |
| 87 | + expect(browserTargetProd).toBeUndefined(); |
| 88 | + expect(buildTargetProd).toBe('app:build:production'); |
| 89 | + }); |
| 90 | + |
| 91 | + it(`should replace 'browserTarget' when using '@angular-devkit/build-angular:extract-i18n'`, async () => { |
| 92 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 93 | + const { |
| 94 | + projects: { app }, |
| 95 | + } = JSON.parse(newTree.readContent('/angular.json')); |
| 96 | + |
| 97 | + const { browserTarget, buildTarget } = app.architect['i18n'].options; |
| 98 | + expect(browserTarget).toBeUndefined(); |
| 99 | + expect(buildTarget).toBe('app:build:production'); |
| 100 | + }); |
| 101 | + |
| 102 | + it(`should not replace 'browserTarget' when using other builders`, async () => { |
| 103 | + const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); |
| 104 | + const { |
| 105 | + projects: { app }, |
| 106 | + } = JSON.parse(newTree.readContent('/angular.json')); |
| 107 | + |
| 108 | + const { browserTarget, buildTarget } = app.architect['app-shell'].options; |
| 109 | + expect(browserTarget).toBe('app:build'); |
| 110 | + expect(buildTarget).toBeUndefined(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments