Skip to content

Commit 7212252

Browse files
alan-agius4dgp1130
authored andcommitted
test(@angular/cli): remove hardcoded versions from ng-update test
We improve the migrations logic to automatically run the needed migrations based on the build version of the Angular CLI. This helps us to avoid having to manually update this test when we bump the major version. Example: https://github.com/angular/angular-cli/pull/22579/files
1 parent 6cf9887 commit 7212252

File tree

4 files changed

+81
-66
lines changed

4 files changed

+81
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { SemVer } from 'semver';
2+
import { createProjectFromAsset } from '../../utils/assets';
3+
import { expectFileMatchToExist, readFile } from '../../utils/fs';
4+
import { setRegistry } from '../../utils/packages';
5+
import { ng, noSilentNg } from '../../utils/process';
6+
import { isPrereleaseCli, useCIChrome, useCIDefaults, NgCLIVersion } from '../../utils/project';
7+
8+
export default async function () {
9+
try {
10+
// We need to use the public registry because in the local NPM server we don't have
11+
// older versions @angular/cli packages which would cause `npm install` during `ng update` to fail.
12+
await setRegistry(false);
13+
await createProjectFromAsset('10.0-project', true);
14+
15+
// CLI proiject version
16+
const { version: cliVersion } = JSON.parse(
17+
await readFile('./node_modules/@angular/cli/package.json'),
18+
);
19+
const cliMajorProjectVersion = new SemVer(cliVersion).major;
20+
21+
// CLI current version.
22+
const cliMajorVersion = NgCLIVersion.major;
23+
24+
for (let version = cliMajorProjectVersion + 1; version < cliMajorVersion; version++) {
25+
// Run all the migrations until the current build major version - 1.
26+
// Example: when the project is using CLI version 10 and the build CLI version is 14.
27+
// We will run the following migrations:
28+
// - 10 -> 11
29+
// - 11 -> 12
30+
// - 12 -> 13
31+
const { stdout } = await ng('update', `@angular/cli@${version}`, `@angular/core@${version}`);
32+
if (!stdout.includes("Executing migrations of package '@angular/cli'")) {
33+
throw new Error('Update did not execute migrations. OUTPUT: \n' + stdout);
34+
}
35+
}
36+
} finally {
37+
await setRegistry(true);
38+
}
39+
40+
// Update Angular current build
41+
const extraUpdateArgs = isPrereleaseCli() ? ['--next', '--force'] : [];
42+
43+
// For the latest/next release we purposely don't run `ng update @angular/core`.
44+
45+
// During a major release when the branch version is bumped from `12.0.0-rc.x` to `12.0.0` there would be a period were in
46+
// the local NPM registry `@angular/cli@latest` will point to `12.0.0`, but on the public NPM repository `@angular/core@latest` will be `11.2.x`.
47+
48+
// This causes `ng update @angular/core` to fail because of mismatching peer dependencies.
49+
50+
// The reason for this is because of our bumping and release strategy. When we release a major version on NPM we don't tag it
51+
// `@latest` right away, but we wait for all teams to release their packages before doing so. While this is good because all team
52+
// packages gets tagged with `@latest` at the same time. This is problematic for our CI, since we test against the public NPM repo and are dependent on tags.
53+
54+
// NB: `ng update @angular/cli` will still cause `@angular/core` packages to be updated therefore we still test updating the core package without running the command.
55+
56+
await ng('update', '@angular/cli', ...extraUpdateArgs);
57+
58+
// Setup testing to use CI Chrome.
59+
await useCIChrome('./');
60+
await useCIChrome('./e2e/');
61+
await useCIDefaults('ten-project');
62+
63+
// Run CLI commands.
64+
await ng('generate', 'component', 'my-comp');
65+
await ng('test', '--watch=false');
66+
await ng('e2e');
67+
await ng('e2e', '--configuration=production');
68+
69+
// Verify project now creates bundles
70+
await noSilentNg('build', '--configuration=production');
71+
await expectFileMatchToExist('dist/ten-project/', /main\.[0-9a-f]{16}\.js/);
72+
}

tests/legacy-cli/e2e/tests/update/update-9.ts

-57
This file was deleted.

tests/legacy-cli/e2e/tests/update/update-multiple-versions.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { expectToFail } from '../../utils/utils';
66

77
export default async function () {
88
try {
9-
await createProjectFromAsset('9.0-project', true, true);
9+
await createProjectFromAsset('10.0-project', true, true);
1010
await setRegistry(false);
1111
await installWorkspacePackages();
1212
await setRegistry(true);
@@ -16,12 +16,12 @@ export default async function () {
1616
extraArgs.push('--next');
1717
}
1818

19-
// Update Angular from v9 to 10
19+
// Update Angular from v10 to 11
2020
const { stdout } = await ng('update', ...extraArgs);
21-
if (!/@angular\/core\s+9\.\d\.\d+ -> 10\.\d\.\d+\s+ng update @angular\/core@10/.test(stdout)) {
22-
// @angular/core 9.x.x -> 10.x.x ng update @angular/core@11
21+
if (!/@angular\/core\s+10\.\d\.\d+ -> 11\.\d\.\d+\s+ng update @angular\/core@11/.test(stdout)) {
22+
// @angular/core 10.x.x -> 11.x.x ng update @angular/core@11
2323
throw new Error(
24-
`Output didn't match "@angular/core 9.x.x -> 10.x.x ng update @angular/core@10". OUTPUT: \n` +
24+
`Output didn't match "@angular/core 10.x.x -> 11.x.x ng update @angular/core@11". OUTPUT: \n` +
2525
stdout,
2626
);
2727
}

tests/legacy-cli/e2e/utils/project.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { prerelease } from 'semver';
3+
import { prerelease, SemVer } from 'semver';
44
import { packages } from '../../../../lib/packages';
55
import { getGlobalVariable } from './env';
66
import { prependToFile, readFile, replaceInFile, writeFile } from './fs';
@@ -225,8 +225,8 @@ export async function useCIChrome(projectDir: string = ''): Promise<void> {
225225
}
226226
}
227227

228-
export function isPrereleaseCli(): boolean {
229-
const pre = prerelease(packages['@angular/cli'].version);
228+
export const NgCLIVersion = new SemVer(packages['@angular/cli'].version);
230229

231-
return pre && pre.length > 0;
230+
export function isPrereleaseCli(): boolean {
231+
return prerelease(NgCLIVersion)?.length > 0;
232232
}

0 commit comments

Comments
 (0)