Skip to content

Commit 04e7c25

Browse files
alan-agius4angular-robot[bot]
authored andcommitted
test: use NPM_CONFIG_legacy_peer_deps when testing prerelease versions of Angular material.
`@angular/material` pre-release may not support the current version of `@angular/core` pre-release. In this case peer dependency ranges may not resolve causing npm 7+ to fail during tests.
1 parent ef39987 commit 04e7c25

File tree

3 files changed

+80
-38
lines changed

3 files changed

+80
-38
lines changed

tests/legacy-cli/e2e/tests/build/material.ts

+40-25
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,51 @@ import { isPrereleaseCli, updateJsonFile } from '../../utils/project';
77
const snapshots = require('../../ng-snapshot/package.json');
88

99
export default async function () {
10-
let tag = (await isPrereleaseCli()) ? '@next' : '';
11-
await ng('add', `@angular/material${tag}`, '--skip-confirmation');
10+
// `@angular/material` pre-release may not support the current version of `@angular/core` pre-release.
11+
// due to the order of releases FW -> CLI -> Material
12+
// In this case peer dependency ranges may not resolve causing npm 7+ to fail during tests.
13+
const original_NPM_CONFIG_legacy_peer_deps = process.env['NPM_CONFIG_legacy_peer_deps'];
14+
const isPrerelease = await isPrereleaseCli();
1215

13-
const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
14-
if (isSnapshotBuild) {
15-
await updateJsonFile('package.json', (packageJson) => {
16-
const dependencies = packageJson['dependencies'];
17-
// Angular material adds dependencies on other Angular packages
18-
// Iterate over all of the packages to update them to the snapshot version.
19-
for (const [name, version] of Object.entries(snapshots.dependencies)) {
20-
if (name in dependencies) {
21-
dependencies[name] = version;
16+
let tag = isPrerelease ? '@next' : '';
17+
18+
try {
19+
process.env['NPM_CONFIG_legacy_peer_deps'] = isPrerelease
20+
? 'true'
21+
: original_NPM_CONFIG_legacy_peer_deps;
22+
23+
await ng('add', `@angular/material${tag}`, '--skip-confirmation');
24+
25+
const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
26+
if (isSnapshotBuild) {
27+
await updateJsonFile('package.json', (packageJson) => {
28+
const dependencies = packageJson['dependencies'];
29+
// Angular material adds dependencies on other Angular packages
30+
// Iterate over all of the packages to update them to the snapshot version.
31+
for (const [name, version] of Object.entries(snapshots.dependencies)) {
32+
if (name in dependencies) {
33+
dependencies[name] = version;
34+
}
2235
}
23-
}
2436

25-
dependencies['@angular/material-moment-adapter'] =
26-
snapshots.dependencies['@angular/material-moment-adapter'];
27-
});
28-
await installWorkspacePackages();
29-
} else {
30-
if (!tag) {
31-
const installedMaterialVersion = JSON.parse(await readFile('package.json'))['dependencies'][
32-
'@angular/material'
33-
];
34-
tag = `@${installedMaterialVersion}`;
37+
dependencies['@angular/material-moment-adapter'] =
38+
snapshots.dependencies['@angular/material-moment-adapter'];
39+
});
40+
await installWorkspacePackages();
41+
} else {
42+
if (!tag) {
43+
const installedMaterialVersion = JSON.parse(await readFile('package.json'))['dependencies'][
44+
'@angular/material'
45+
];
46+
tag = `@${installedMaterialVersion}`;
47+
}
48+
await installPackage(`@angular/material-moment-adapter${tag}`);
3549
}
36-
await installPackage(`@angular/material-moment-adapter${tag}`);
37-
}
3850

39-
await installPackage('moment');
51+
await installPackage('moment');
52+
} finally {
53+
process.env['NPM_CONFIG_legacy_peer_deps'] = original_NPM_CONFIG_legacy_peer_deps;
54+
}
4055

4156
await ng('build');
4257

tests/legacy-cli/e2e/tests/commands/add/add-material.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
import { assertIsError } from '../../../utils/utils';
22
import { expectFileToMatch, rimraf } from '../../../utils/fs';
33
import { uninstallPackage } from '../../../utils/packages';
4-
import { ng } from '../../../utils/process';
4+
import { execWithEnv } from '../../../utils/process';
55
import { isPrereleaseCli } from '../../../utils/project';
66

77
export default async function () {
88
// forcibly remove in case another test doesn't clean itself up
99
await rimraf('node_modules/@angular/material');
1010

11-
const tag = (await isPrereleaseCli()) ? '@next' : '';
11+
const isPrerelease = await isPrereleaseCli();
12+
const tag = isPrerelease ? '@next' : '';
13+
const processEnv = {
14+
...process.env,
15+
// `@angular/material` pre-release may not support the current version of `@angular/core` pre-release.
16+
// due to the order of releases FW -> CLI -> Material
17+
// In this case peer dependency ranges may not resolve causing npm 7+ to fail during tests.
18+
'NPM_CONFIG_legacy_peer_deps': isPrerelease
19+
? 'true'
20+
: process.env['NPM_CONFIG_legacy_peer_deps'],
21+
};
1222

1323
try {
14-
await ng('add', `@angular/material${tag}`, '--unknown', '--skip-confirmation');
24+
await execWithEnv(
25+
'ng',
26+
['add', `@angular/material${tag}`, '--skip-confirmation', '--unknown'],
27+
processEnv,
28+
);
1529
} catch (error) {
1630
assertIsError(error);
1731
if (!(error as Error).message.includes(`Unknown option: '--unknown'`)) {
1832
throw error;
1933
}
2034
}
2135

22-
await ng(
23-
'add',
24-
`@angular/material${tag}`,
25-
'--theme',
26-
'custom',
27-
'--verbose',
28-
'--skip-confirmation',
36+
await execWithEnv(
37+
'ng',
38+
['add', `@angular/material${tag}`, '--theme', 'custom', '--verbose', '--skip-confirmation'],
39+
processEnv,
2940
);
41+
3042
await expectFileToMatch('package.json', /@angular\/material/);
3143

3244
// Clean up existing cdk package

tests/legacy-cli/e2e/tests/misc/invalid-schematic-dependencies.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expectFileToMatch } from '../../utils/fs';
2-
import { execWithEnv, extractNpmEnv, ng, silentNpm } from '../../utils/process';
2+
import { execWithEnv, extractNpmEnv, silentNpm } from '../../utils/process';
33
import { installPackage, uninstallPackage } from '../../utils/packages';
44
import { isPrereleaseCli } from '../../utils/project';
55

@@ -13,8 +13,23 @@ export default async function () {
1313
// Install outdated and incompatible version
1414
await installPackage('@schematics/angular@7');
1515

16-
const tag = (await isPrereleaseCli()) ? '@next' : '';
17-
await ng('add', `@angular/material${tag}`, '--skip-confirmation');
16+
const isPrerelease = await isPrereleaseCli();
17+
const tag = isPrerelease ? '@next' : '';
18+
19+
await execWithEnv(
20+
'ng',
21+
['add', `@angular/material${tag}`, '--skip-confirmation'],
22+
// `@angular/material` pre-release may not support the current version of `@angular/core` pre-release.
23+
// due to the order of releases FW -> CLI -> Material
24+
// In this case peer dependency ranges may not resolve causing npm 7+ to fail during tests.
25+
{
26+
...process.env,
27+
'NPM_CONFIG_legacy_peer_deps': isPrerelease
28+
? 'true'
29+
: process.env['NPM_CONFIG_legacy_peer_deps'],
30+
},
31+
);
32+
1833
await expectFileToMatch('package.json', /@angular\/material/);
1934

2035
// Clean up existing cdk package

0 commit comments

Comments
 (0)