|
| 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 { JsonParseMode, parseJson } from '@angular-devkit/core'; |
| 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 | +describe('Migration to update target and module compiler options', () => { |
| 14 | + const schematicName = 'update-module-and-target-compiler-options'; |
| 15 | + |
| 16 | + const schematicRunner = new SchematicTestRunner( |
| 17 | + 'migrations', |
| 18 | + require.resolve('../migration-collection.json'), |
| 19 | + ); |
| 20 | + |
| 21 | + function createJsonFile(tree: UnitTestTree, filePath: string, content: {}) { |
| 22 | + tree.create(filePath, JSON.stringify(content, undefined, 2)); |
| 23 | + } |
| 24 | + |
| 25 | + // tslint:disable-next-line: no-any |
| 26 | + function readJsonFile(tree: UnitTestTree, filePath: string): any { |
| 27 | + // tslint:disable-next-line: no-any |
| 28 | + return parseJson(tree.readContent(filePath).toString(), JsonParseMode.Loose) as any; |
| 29 | + } |
| 30 | + |
| 31 | + function createWorkSpaceConfig(tree: UnitTestTree) { |
| 32 | + const angularConfig: WorkspaceSchema = { |
| 33 | + version: 1, |
| 34 | + projects: { |
| 35 | + app: { |
| 36 | + root: '', |
| 37 | + sourceRoot: 'src', |
| 38 | + projectType: ProjectType.Application, |
| 39 | + prefix: 'app', |
| 40 | + architect: { |
| 41 | + build: { |
| 42 | + builder: Builders.Browser, |
| 43 | + options: { |
| 44 | + tsConfig: 'src/tsconfig.app.json', |
| 45 | + main: '', |
| 46 | + polyfills: '', |
| 47 | + }, |
| 48 | + configurations: { |
| 49 | + production: { |
| 50 | + tsConfig: 'src/tsconfig.app.prod.json', |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + test: { |
| 55 | + builder: Builders.Karma, |
| 56 | + options: { |
| 57 | + karmaConfig: '', |
| 58 | + tsConfig: 'src/tsconfig.spec.json', |
| 59 | + }, |
| 60 | + }, |
| 61 | + e2e: { |
| 62 | + builder: Builders.Protractor, |
| 63 | + options: { |
| 64 | + protractorConfig: 'src/e2e/protractor.conf.js', |
| 65 | + devServerTarget: '', |
| 66 | + }, |
| 67 | + }, |
| 68 | + server: { |
| 69 | + builder: Builders.Server, |
| 70 | + options: { |
| 71 | + tsConfig: 'src/tsconfig.server.json', |
| 72 | + outputPath: '', |
| 73 | + main: '', |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
| 81 | + createJsonFile(tree, 'angular.json', angularConfig); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + let tree: UnitTestTree; |
| 86 | + beforeEach(() => { |
| 87 | + tree = new UnitTestTree(new EmptyTree()); |
| 88 | + createWorkSpaceConfig(tree); |
| 89 | + |
| 90 | + // Create tsconfigs |
| 91 | + const compilerOptions = { target: 'es2015', module: 'esnext' }; |
| 92 | + |
| 93 | + // Workspace |
| 94 | + createJsonFile(tree, 'tsconfig.json', { compilerOptions }); |
| 95 | + |
| 96 | + // Application |
| 97 | + createJsonFile(tree, 'src/tsconfig.app.json', { compilerOptions }); |
| 98 | + createJsonFile(tree, 'src/tsconfig.app.prod.json', { compilerOptions }); |
| 99 | + createJsonFile(tree, 'src/tsconfig.spec.json', { compilerOptions }); |
| 100 | + |
| 101 | + // E2E |
| 102 | + createJsonFile(tree, 'src/e2e/protractor.conf.js', ''); |
| 103 | + createJsonFile(tree, 'src/e2e/tsconfig.json', { compilerOptions: { module: 'commonjs', target: 'es5' } }); |
| 104 | + |
| 105 | + // Universal |
| 106 | + createJsonFile(tree, 'src/tsconfig.server.json', { compilerOptions: { module: 'commonjs' } }); |
| 107 | + }); |
| 108 | + |
| 109 | + it(`should update module and target in workspace 'tsconfig.json'`, async () => { |
| 110 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 111 | + const { module } = readJsonFile(newTree, 'tsconfig.json').compilerOptions; |
| 112 | + expect(module).toBe('es2020'); |
| 113 | + }); |
| 114 | + |
| 115 | + it(`should update module and target in 'tsconfig.json' which is referenced in option`, async () => { |
| 116 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 117 | + const { module } = readJsonFile(newTree, 'src/tsconfig.spec.json').compilerOptions; |
| 118 | + expect(module).toBe('es2020'); |
| 119 | + }); |
| 120 | + |
| 121 | + it(`should update module and target in 'tsconfig.json' which is referenced in a configuration`, async () => { |
| 122 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 123 | + const { module } = readJsonFile(newTree, 'src/tsconfig.app.prod.json').compilerOptions; |
| 124 | + expect(module).toBe('es2020'); |
| 125 | + }); |
| 126 | + |
| 127 | + it(`should update target to es2018 in E2E 'tsconfig.json'`, async () => { |
| 128 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 129 | + const { module, target } = readJsonFile(newTree, 'src/e2e/tsconfig.json').compilerOptions; |
| 130 | + expect(module).toBe('commonjs'); |
| 131 | + expect(target).toBe('es2018'); |
| 132 | + }); |
| 133 | + |
| 134 | + |
| 135 | + it(`should remove module in 'tsconfig.server.json'`, async () => { |
| 136 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 137 | + const { module, target } = readJsonFile(newTree, 'src/tsconfig.server.json').compilerOptions; |
| 138 | + expect(module).toBeUndefined(); |
| 139 | + expect(target).toBeUndefined(); |
| 140 | + }); |
| 141 | +}); |
0 commit comments