|
| 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 | + |
| 12 | +describe('Migration to update Angular packages version prefix to `^` instead of `~`', () => { |
| 13 | + const packageJsonPath = '/package.json'; |
| 14 | + const schematicName = 'update-angular-packages-version-prefix'; |
| 15 | + const schematicRunner = new SchematicTestRunner( |
| 16 | + 'migrations', |
| 17 | + require.resolve('../migration-collection.json'), |
| 18 | + ); |
| 19 | + |
| 20 | + let tree: UnitTestTree; |
| 21 | + beforeEach(() => { |
| 22 | + tree = new UnitTestTree(new EmptyTree()); |
| 23 | + }); |
| 24 | + |
| 25 | + it(`should replace Angular packages versioned with '~' to '^'`, async () => { |
| 26 | + tree.create( |
| 27 | + packageJsonPath, |
| 28 | + JSON.stringify({ |
| 29 | + dependencies: { |
| 30 | + '@angular/animations': '~13.1.0', |
| 31 | + '@angular/common': '~13.1.0', |
| 32 | + '@angular/compiler': '~13.1.0', |
| 33 | + '@angular/core': '~13.1.0', |
| 34 | + '@angular/forms': '~13.1.0', |
| 35 | + '@angular/platform-browser': '~13.1.0', |
| 36 | + '@angular/platform-browser-dynamic': '~13.1.0', |
| 37 | + '@angular/router': '~13.1.0', |
| 38 | + '@nguniversal/commom': '^13.1.0', |
| 39 | + 'rxjs': '~7.4.0', |
| 40 | + 'tslib': '^2.3.0', |
| 41 | + 'zone.js': '~0.11.4', |
| 42 | + }, |
| 43 | + devDependencies: { |
| 44 | + '@angular-devkit/build-angular': '~13.1.3', |
| 45 | + '@angular/cli': '~13.1.3', |
| 46 | + '@angular/compiler-cli': '~13.1.0', |
| 47 | + '@angular/localize': '^13.1.3', |
| 48 | + '@types/jasmine': '~3.10.0', |
| 49 | + '@types/node': '^12.11.1', |
| 50 | + 'jasmine-core': '~3.10.0', |
| 51 | + 'karma': '~6.3.0', |
| 52 | + 'karma-chrome-launcher': '~3.1.0', |
| 53 | + 'karma-coverage': '~2.1.0', |
| 54 | + 'karma-jasmine': '~4.0.0', |
| 55 | + 'karma-jasmine-html-reporter': '~1.7.0', |
| 56 | + 'ng-packagr': '~13.1.3', |
| 57 | + 'typescript': '~4.5.2', |
| 58 | + }, |
| 59 | + }), |
| 60 | + ); |
| 61 | + |
| 62 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 63 | + const pkg = JSON.parse(newTree.readContent(packageJsonPath)); |
| 64 | + |
| 65 | + expect(pkg['dependencies']).toEqual({ |
| 66 | + '@angular/animations': '^13.1.0', |
| 67 | + '@angular/common': '^13.1.0', |
| 68 | + '@angular/compiler': '^13.1.0', |
| 69 | + '@angular/core': '^13.1.0', |
| 70 | + '@angular/forms': '^13.1.0', |
| 71 | + '@angular/platform-browser': '^13.1.0', |
| 72 | + '@angular/platform-browser-dynamic': '^13.1.0', |
| 73 | + '@angular/router': '^13.1.0', |
| 74 | + '@nguniversal/commom': '^13.1.0', |
| 75 | + 'rxjs': '~7.4.0', |
| 76 | + 'tslib': '^2.3.0', |
| 77 | + 'zone.js': '~0.11.4', |
| 78 | + }); |
| 79 | + |
| 80 | + expect(pkg['devDependencies']).toEqual({ |
| 81 | + '@angular-devkit/build-angular': '^13.1.3', |
| 82 | + '@angular/cli': '^13.1.3', |
| 83 | + '@angular/compiler-cli': '^13.1.0', |
| 84 | + '@angular/localize': '^13.1.3', |
| 85 | + '@types/jasmine': '~3.10.0', |
| 86 | + '@types/node': '^12.11.1', |
| 87 | + 'jasmine-core': '~3.10.0', |
| 88 | + 'karma': '~6.3.0', |
| 89 | + 'karma-chrome-launcher': '~3.1.0', |
| 90 | + 'karma-coverage': '~2.1.0', |
| 91 | + 'karma-jasmine': '~4.0.0', |
| 92 | + 'karma-jasmine-html-reporter': '~1.7.0', |
| 93 | + 'ng-packagr': '^13.1.3', |
| 94 | + 'typescript': '~4.5.2', |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should not replace pinned Angular packages versions', async () => { |
| 99 | + tree.create( |
| 100 | + packageJsonPath, |
| 101 | + JSON.stringify({ |
| 102 | + dependencies: { |
| 103 | + '@angular/animations': '13.1.0', |
| 104 | + '@angular/core': '~13.1.0', |
| 105 | + }, |
| 106 | + devDependencies: { |
| 107 | + '@angular-devkit/build-angular': '13.1.3', |
| 108 | + }, |
| 109 | + }), |
| 110 | + ); |
| 111 | + |
| 112 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 113 | + const pkg = JSON.parse(newTree.readContent(packageJsonPath)); |
| 114 | + |
| 115 | + expect(pkg['dependencies']['@angular/animations']).toBe('13.1.0'); |
| 116 | + expect(pkg['dependencies']['@angular/core']).toBe('^13.1.0'); |
| 117 | + expect(pkg['devDependencies']['@angular-devkit/build-angular']).toBe('13.1.3'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should not error when `dependencies` is missing', async () => { |
| 121 | + tree.create( |
| 122 | + packageJsonPath, |
| 123 | + JSON.stringify({ |
| 124 | + devDependencies: { |
| 125 | + '@angular-devkit/build-angular': '~13.1.3', |
| 126 | + }, |
| 127 | + }), |
| 128 | + ); |
| 129 | + |
| 130 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 131 | + const pkg = JSON.parse(newTree.readContent(packageJsonPath)); |
| 132 | + |
| 133 | + expect(pkg['dependencies']).toBeUndefined(); |
| 134 | + expect(pkg['devDependencies']['@angular-devkit/build-angular']).toBe('^13.1.3'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should not error when `devDependencies` is missing', async () => { |
| 138 | + tree.create( |
| 139 | + packageJsonPath, |
| 140 | + JSON.stringify({ |
| 141 | + dependencies: { |
| 142 | + '@angular-devkit/build-angular': '~13.1.3', |
| 143 | + }, |
| 144 | + }), |
| 145 | + ); |
| 146 | + |
| 147 | + const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); |
| 148 | + const pkg = JSON.parse(newTree.readContent(packageJsonPath)); |
| 149 | + |
| 150 | + expect(pkg['dependencies']['@angular-devkit/build-angular']).toBe('^13.1.3'); |
| 151 | + expect(pkg['devDependencies']).toBeUndefined(); |
| 152 | + }); |
| 153 | +}); |
0 commit comments