Skip to content

Commit 1080a52

Browse files
committed
fix(@schematics/angular): add migration to remove skipTests from @schematics/angular:module
Migration for #20842 Closes #20848
1 parent 43926a2 commit 1080a52

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

packages/schematics/angular/migrations/migration-collection.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@
115115
"factory": "./update-12/update-web-workers",
116116
"description": "Updates Web Worker consumer usage to use the new syntax supported directly by Webpack 5."
117117
},
118+
"schematic-options-12": {
119+
"version": "12.0.1",
120+
"factory": "./update-12/schematic-options",
121+
"description": "Remove invalid 'skipTests' option in '@schematics/angular:module' Angular schematic options."
122+
},
118123
"production-by-default": {
119124
"version": "9999.0.0",
120125
"factory": "./update-12/production-default-config",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 { json } from '@angular-devkit/core';
10+
import { Rule } from '@angular-devkit/schematics';
11+
import { updateWorkspace } from '../../utility/workspace';
12+
13+
export default function (): Rule {
14+
return updateWorkspace((workspace) => {
15+
// Update root level schematics options if present
16+
const rootSchematics = workspace.extensions.schematics;
17+
if (rootSchematics && json.isJsonObject(rootSchematics)) {
18+
updateSchematicsField(rootSchematics);
19+
}
20+
21+
// Update project level schematics options if present
22+
for (const [, project] of workspace.projects) {
23+
const projectSchematics = project.extensions.schematics;
24+
if (projectSchematics && json.isJsonObject(projectSchematics)) {
25+
updateSchematicsField(projectSchematics);
26+
}
27+
}
28+
});
29+
}
30+
31+
function updateSchematicsField(schematics: json.JsonObject): void {
32+
for (const [schematicName, schematicOptions] of Object.entries(schematics)) {
33+
if (!json.isJsonObject(schematicOptions)) {
34+
continue;
35+
}
36+
37+
if (schematicName === '@schematics/angular:module') {
38+
delete schematicOptions.skipTests;
39+
}
40+
}
41+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 remove schematics old options in angular.json', () => {
13+
const workspacePath = '/angular.json';
14+
const schematicName = 'schematic-options-12';
15+
16+
const schematicRunner = new SchematicTestRunner(
17+
'migrations',
18+
require.resolve('../migration-collection.json'),
19+
);
20+
21+
let tree: UnitTestTree;
22+
23+
beforeEach(async () => {
24+
tree = new UnitTestTree(new EmptyTree());
25+
tree = await schematicRunner
26+
.runExternalSchematicAsync(
27+
require.resolve('../../collection.json'),
28+
'ng-new',
29+
{
30+
name: 'migration-test',
31+
version: '1.2.3',
32+
directory: '.',
33+
},
34+
tree,
35+
)
36+
.toPromise();
37+
});
38+
39+
describe('schematic options', () => {
40+
it('should remove `skipTests` from `@schematics/angular:module`', async () => {
41+
const workspace = JSON.parse(tree.readContent(workspacePath));
42+
workspace.schematics = {
43+
'@schematics/angular:module': {
44+
skipTests: true,
45+
},
46+
};
47+
tree.overwrite(workspacePath, JSON.stringify(workspace, undefined, 2));
48+
49+
const tree2 = await schematicRunner
50+
.runSchematicAsync(schematicName, {}, tree.branch())
51+
.toPromise();
52+
const { schematics } = JSON.parse(tree2.readContent(workspacePath));
53+
expect(schematics['@schematics/angular:module'].skipTests).toBeUndefined();
54+
});
55+
56+
it('should not remove `skipTests` from non `@schematics/angular:module` schematic', async () => {
57+
const workspace = JSON.parse(tree.readContent(workspacePath));
58+
workspace.schematics = {
59+
'@schematics/angular:component': {
60+
skipTests: true,
61+
},
62+
'@schematics/some-other:module': {
63+
skipTests: true,
64+
},
65+
};
66+
tree.overwrite(workspacePath, JSON.stringify(workspace, undefined, 2));
67+
68+
const tree2 = await schematicRunner
69+
.runSchematicAsync(schematicName, {}, tree.branch())
70+
.toPromise();
71+
const { schematics } = JSON.parse(tree2.readContent(workspacePath));
72+
expect(schematics['@schematics/angular:component'].skipTests).toBeTrue();
73+
expect(schematics['@schematics/some-other:module'].skipTests).toBeTrue();
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)