Skip to content

Commit b261afa

Browse files
crisbetoandrewseguin
authored andcommitted
test(schematics): move away from deprecated test API (#16150)
In 8.0 the `runSchematic` method is marked as deprecated. These changes switch all the usages to `runSchematicAsync`.
1 parent 2b07377 commit b261afa

File tree

8 files changed

+224
-165
lines changed

8 files changed

+224
-165
lines changed

src/cdk/schematics/ng-add/index.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ describe('CDK ng-add', () => {
1111
appTree = await createTestApp(runner);
1212
});
1313

14-
it('should update the package.json', () => {
15-
const tree = runner.runSchematic('ng-add', {}, appTree);
14+
it('should update the package.json', async () => {
15+
const tree = await runner.runSchematicAsync('ng-add', {}, appTree).toPromise();
1616
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
1717
const dependencies = packageJson.dependencies;
1818

src/cdk/schematics/ng-generate/drag-drop/index.spec.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
22
import {getProjectFromWorkspace} from '@angular/cdk/schematics';
33
import {getWorkspace} from '@schematics/angular/utility/config';
4-
import {getProject} from '@schematics/angular/utility/project';
54
import {createTestApp, getFileContent} from '../../testing';
65
import {Schema} from './schema';
76

@@ -21,7 +20,8 @@ describe('CDK drag-drop schematic', () => {
2120
});
2221

2322
it('should create drag-drop files and add them to module', async () => {
24-
const tree = runner.runSchematic('drag-drop', baseOptions, await createTestApp(runner));
23+
const app = await createTestApp(runner);
24+
const tree = await runner.runSchematicAsync('drag-drop', baseOptions, app).toPromise();
2525
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
2626
const files = tree.files;
2727

@@ -35,16 +35,17 @@ describe('CDK drag-drop schematic', () => {
3535
});
3636

3737
it('should add drag-drop module', async () => {
38-
const tree = runner.runSchematic('drag-drop', baseOptions, await createTestApp(runner));
38+
const app = await createTestApp(runner);
39+
const tree = await runner.runSchematicAsync('drag-drop', baseOptions, app).toPromise();
3940
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
4041

4142
expect(moduleContent).toContain('DragDropModule');
4243
});
4344

4445
describe('style option', () => {
4546
it('should respect the option value', async () => {
46-
const tree = runner.runSchematic(
47-
'drag-drop', {style: 'scss', ...baseOptions}, await createTestApp(runner));
47+
const tree = await runner.runSchematicAsync(
48+
'drag-drop', {style: 'scss', ...baseOptions}, await createTestApp(runner)).toPromise();
4849

4950
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
5051
});
@@ -61,14 +62,14 @@ describe('CDK drag-drop schematic', () => {
6162
project.schematics!['@schematics/angular:component'] = {styleext: 'scss'};
6263

6364
tree.overwrite('angular.json', JSON.stringify(workspace));
64-
tree = runner.runSchematic('drag-drop', baseOptions, tree);
65+
tree = await runner.runSchematicAsync('drag-drop', baseOptions, tree).toPromise();
6566

6667
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
6768
});
6869

6970
it('should not generate invalid stylesheets', async () => {
70-
const tree = runner.runSchematic(
71-
'drag-drop', {style: 'styl', ...baseOptions}, await createTestApp(runner));
71+
const tree = await runner.runSchematicAsync(
72+
'drag-drop', {style: 'styl', ...baseOptions}, await createTestApp(runner)).toPromise();
7273

7374
// In this case we expect the schematic to generate a plain "css" file because
7475
// the component schematics are using CSS style templates which are not compatible
@@ -80,49 +81,51 @@ describe('CDK drag-drop schematic', () => {
8081
});
8182

8283
it('should fall back to the @schematics/angular:component option value', async () => {
83-
const tree = runner.runSchematic(
84-
'drag-drop', baseOptions, await createTestApp(runner, {style: 'less'}));
84+
const tree = await runner.runSchematicAsync(
85+
'drag-drop', baseOptions, await createTestApp(runner, {style: 'less'})).toPromise();
8586

8687
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
8788
});
8889
});
8990

9091
describe('inlineStyle option', () => {
9192
it('should respect the option value', async () => {
92-
const tree = runner.runSchematic(
93-
'drag-drop', {inlineStyle: true, ...baseOptions}, await createTestApp(runner));
93+
const app = await createTestApp(runner);
94+
const tree = await runner.runSchematicAsync(
95+
'drag-drop', {inlineStyle: true, ...baseOptions}, app).toPromise();
9496

9597
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
9698
});
9799

98100
it('should fall back to the @schematics/angular:component option value', async () => {
99-
const tree = runner.runSchematic(
100-
'drag-drop', baseOptions, await createTestApp(runner, {inlineStyle: true}));
101+
const tree = await runner.runSchematicAsync(
102+
'drag-drop', baseOptions, await createTestApp(runner, {inlineStyle: true})).toPromise();
101103

102104
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
103105
});
104106
});
105107

106108
describe('inlineTemplate option', () => {
107109
it('should respect the option value', async () => {
108-
const tree = runner.runSchematic(
109-
'drag-drop', {inlineTemplate: true, ...baseOptions}, await createTestApp(runner));
110+
const app = await createTestApp(runner);
111+
const tree = await runner.runSchematicAsync(
112+
'drag-drop', {inlineTemplate: true, ...baseOptions}, app).toPromise();
110113

111114
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
112115
});
113116

114117
it('should fall back to the @schematics/angular:component option value', async () => {
115-
const tree = runner.runSchematic(
116-
'drag-drop', baseOptions, await createTestApp(runner, {inlineTemplate: true}));
118+
const app = await createTestApp(runner, {inlineTemplate: true});
119+
const tree = await runner.runSchematicAsync('drag-drop', baseOptions, app).toPromise();
117120

118121
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
119122
});
120123
});
121124

122125
describe('skipTests option', () => {
123126
it('should respect the option value', async () => {
124-
const tree = runner.runSchematic(
125-
'drag-drop', {skipTests: true, ...baseOptions}, await createTestApp(runner));
127+
const tree = await runner.runSchematicAsync(
128+
'drag-drop', {skipTests: true, ...baseOptions}, await createTestApp(runner)).toPromise();
126129

127130
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
128131
});
@@ -139,14 +142,14 @@ describe('CDK drag-drop schematic', () => {
139142
project.schematics!['@schematics/angular:component'] = {spec: false};
140143

141144
tree.overwrite('angular.json', JSON.stringify(workspace));
142-
tree = runner.runSchematic('drag-drop', baseOptions, tree);
145+
tree = await runner.runSchematicAsync('drag-drop', baseOptions, tree).toPromise();
143146

144147
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
145148
});
146149

147150
it('should fall back to the @schematics/angular:component option value', async () => {
148-
const tree = runner.runSchematic(
149-
'drag-drop', baseOptions, await createTestApp(runner, {skipTests: true}));
151+
const tree = await runner.runSchematicAsync(
152+
'drag-drop', baseOptions, await createTestApp(runner, {skipTests: true})).toPromise();
150153

151154
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
152155
});

src/material/schematics/ng-add/index.spec.ts

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ describe('ng-add schematic', () => {
3434
tree.overwrite('/package.json', JSON.stringify(packageContent, null, 2));
3535
}
3636

37-
it('should update package.json', () => {
37+
it('should update package.json', async () => {
3838
// By default, the Angular workspace schematic sets up "@angular/animations". In order
3939
// to verify that we would set up the dependency properly if someone doesn't have the
4040
// animations installed already, we remove the animations dependency explicitly.
4141
removePackageJsonDependency(appTree, '@angular/animations');
4242

43-
const tree = runner.runSchematic('ng-add', {}, appTree);
43+
const tree = await runner.runSchematicAsync('ng-add', {}, appTree).toPromise();
4444
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
4545
const dependencies = packageJson.dependencies;
4646
const angularCoreVersion = dependencies['@angular/core'];
@@ -59,16 +59,16 @@ describe('ng-add schematic', () => {
5959
expect(runner.tasks.some(task => task.name === 'run-schematic')).toBe(true);
6060
});
6161

62-
it('should add hammerjs import to project main file', () => {
63-
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
62+
it('should add hammerjs import to project main file', async () => {
63+
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
6464
const fileContent = getFileContent(tree, '/projects/material/src/main.ts');
6565

6666
expect(fileContent).toContain(`import 'hammerjs';`,
6767
'Expected the project main file to contain a HammerJS import.');
6868
});
6969

70-
it('should add default theme', () => {
71-
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
70+
it('should add default theme', async () => {
71+
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
7272

7373
const workspace = getWorkspace(tree);
7474
const project = getProjectFromWorkspace(workspace);
@@ -81,7 +81,8 @@ describe('ng-add schematic', () => {
8181
// TODO(devversion): do not re-create test app here.
8282
appTree = await createTestApp(runner, {style: 'scss'});
8383

84-
const tree = runner.runSchematic('ng-add-setup-project', {theme: 'custom'}, appTree);
84+
const tree = await runner.runSchematicAsync('ng-add-setup-project',
85+
{theme: 'custom'}, appTree).toPromise();
8586

8687
const workspace = getWorkspace(tree);
8788
const project = getProjectFromWorkspace(workspace);
@@ -98,7 +99,8 @@ describe('ng-add schematic', () => {
9899
// TODO(devversion): do not re-create test app here.
99100
appTree = await createTestApp(runner, {style: 'css'});
100101

101-
const tree = runner.runSchematic('ng-add-setup-project', {theme: 'custom'}, appTree);
102+
const tree = await runner.runSchematicAsync('ng-add-setup-project',
103+
{theme: 'custom'}, appTree).toPromise();
102104
const workspace = getWorkspace(tree);
103105
const project = getProjectFromWorkspace(workspace);
104106
const expectedStylesPath = normalize(`/${project.root}/src/custom-theme.scss`);
@@ -107,8 +109,8 @@ describe('ng-add schematic', () => {
107109
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
108110
});
109111

110-
it('should add font links', () => {
111-
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
112+
it('should add font links', async () => {
113+
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
112114
const workspace = getWorkspace(tree);
113115
const project = getProjectFromWorkspace(workspace);
114116

@@ -125,8 +127,8 @@ describe('ng-add schematic', () => {
125127
' <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"');
126128
});
127129

128-
it('should add material app styles', () => {
129-
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
130+
it('should add material app styles', async () => {
131+
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
130132
const workspace = getWorkspace(tree);
131133
const project = getProjectFromWorkspace(workspace);
132134

@@ -140,16 +142,16 @@ describe('ng-add schematic', () => {
140142

141143
describe('gestures disabled', () => {
142144

143-
it('should not add hammerjs to package.json', () => {
144-
const tree = runner.runSchematic('ng-add', {gestures: false}, appTree);
145+
it('should not add hammerjs to package.json', async () => {
146+
const tree = await runner.runSchematicAsync('ng-add', {gestures: false}, appTree).toPromise();
145147
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
146148

147149
expect(packageJson.dependencies['hammerjs'])
148150
.toBeUndefined(`Expected 'hammerjs' to be not added to the package.json`);
149151
});
150152

151-
it('should not add hammerjs import to project main file', () => {
152-
const tree = runner.runSchematic('ng-add', {gestures: false}, appTree);
153+
it('should not add hammerjs import to project main file', async () => {
154+
const tree = await runner.runSchematicAsync('ng-add', {gestures: false}, appTree).toPromise();
153155
const fileContent = getFileContent(tree, '/projects/material/src/main.ts');
154156

155157
expect(fileContent).not.toContain(`import 'hammerjs';`,
@@ -158,15 +160,15 @@ describe('ng-add schematic', () => {
158160
});
159161

160162
describe('animations enabled', () => {
161-
it('should add the BrowserAnimationsModule to the project module', () => {
162-
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
163+
it('should add the BrowserAnimationsModule to the project module', async () => {
164+
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
163165
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
164166

165167
expect(fileContent).toContain('BrowserAnimationsModule',
166168
'Expected the project app module to import the "BrowserAnimationsModule".');
167169
});
168170

169-
it('should not add BrowserAnimationsModule if NoopAnimationsModule is set up', () => {
171+
it('should not add BrowserAnimationsModule if NoopAnimationsModule is set up', async () => {
170172
const workspace = getWorkspace(appTree);
171173
const project = getProjectFromWorkspace(workspace);
172174

@@ -178,16 +180,17 @@ describe('ng-add schematic', () => {
178180
'@angular/platform-browser/animations', project);
179181

180182
spyOn(console, 'warn');
181-
runner.runSchematic('ng-add-setup-project', {}, appTree);
183+
await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
182184

183185
expect(console.warn).toHaveBeenCalledWith(
184186
jasmine.stringMatching(/Could not set up "BrowserAnimationsModule"/));
185187
});
186188
});
187189

188190
describe('animations disabled', () => {
189-
it('should add the NoopAnimationsModule to the project module', () => {
190-
const tree = runner.runSchematic('ng-add-setup-project', {animations: false}, appTree);
191+
it('should add the NoopAnimationsModule to the project module', async () => {
192+
const tree = await runner.runSchematicAsync('ng-add-setup-project',
193+
{animations: false}, appTree).toPromise();
191194
const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
192195

193196
expect(fileContent).toContain('NoopAnimationsModule',
@@ -221,18 +224,25 @@ describe('ng-add schematic', () => {
221224
tree.overwrite('/angular.json', JSON.stringify(workspace, null, 2));
222225
}
223226

224-
it('should throw an error if the "build" target has been changed', () => {
227+
it('should throw an error if the "build" target has been changed', async () => {
225228
overwriteTargetBuilder(appTree, 'build', 'thirdparty-builder');
226229

227-
expect(() => runner.runSchematic('ng-add-setup-project', {}, appTree))
228-
.toThrowError(/not using the default builders.*build/);
230+
let message: string | null = null;
231+
232+
try {
233+
await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
234+
} catch (e) {
235+
message = e.message;
236+
}
237+
238+
expect(message).toMatch(/not using the default builders.*build/);
229239
});
230240

231-
it('should warn if the "test" target has been changed', () => {
241+
it('should warn if the "test" target has been changed', async () => {
232242
overwriteTargetBuilder(appTree, 'test', 'thirdparty-test-builder');
233243

234244
spyOn(console, 'warn');
235-
runner.runSchematic('ng-add-setup-project', {}, appTree);
245+
await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
236246

237247
expect(console.warn).toHaveBeenCalledWith(
238248
jasmine.stringMatching(/not using the default builders.*cannot add the configured theme/));
@@ -260,12 +270,12 @@ describe('ng-add schematic', () => {
260270
tree.overwrite('/angular.json', JSON.stringify(workspace, null, 2));
261271
}
262272

263-
it('should replace existing prebuilt theme files', () => {
273+
it('should replace existing prebuilt theme files', async () => {
264274
const existingThemePath =
265275
'./node_modules/@angular/material/prebuilt-themes/purple-green.css';
266276
writeStyleFileToWorkspace(appTree, existingThemePath);
267277

268-
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
278+
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
269279
const workspace = getWorkspace(tree);
270280
const project = getProjectFromWorkspace(workspace);
271281
const styles = getProjectTargetOptions(project, 'build').styles;
@@ -276,11 +286,11 @@ describe('ng-add schematic', () => {
276286
'Expected the default prebuilt theme to be added.');
277287
});
278288

279-
it('should not replace existing custom theme files', () => {
289+
it('should not replace existing custom theme files', async () => {
280290
spyOn(console, 'warn');
281291
writeStyleFileToWorkspace(appTree, './projects/material/custom-theme.scss');
282292

283-
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
293+
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
284294
const workspace = getWorkspace(tree);
285295
const project = getProjectFromWorkspace(workspace);
286296
const styles = getProjectTargetOptions(project, 'build').styles;
@@ -291,10 +301,10 @@ describe('ng-add schematic', () => {
291301
jasmine.stringMatching(/Could not add the selected theme/));
292302
});
293303

294-
it('should not add a theme file multiple times', () => {
304+
it('should not add a theme file multiple times', async () => {
295305
writeStyleFileToWorkspace(appTree, defaultPrebuiltThemePath);
296306

297-
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
307+
const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise();
298308
const workspace = getWorkspace(tree);
299309
const project = getProjectFromWorkspace(workspace);
300310
const styles = getProjectTargetOptions(project, 'build').styles;
@@ -303,9 +313,10 @@ describe('ng-add schematic', () => {
303313
'Expected the "styles.css" file and default prebuilt theme to be the only styles');
304314
});
305315

306-
it('should not overwrite existing custom theme files', () => {
316+
it('should not overwrite existing custom theme files', async () => {
307317
appTree.create('/projects/material/custom-theme.scss', 'custom-theme');
308-
const tree = runner.runSchematic('ng-add-setup-project', {theme: 'custom'}, appTree);
318+
const tree = await runner.runSchematicAsync('ng-add-setup-project',
319+
{theme: 'custom'}, appTree).toPromise();
309320

310321
expect(tree.readContent('/projects/material/custom-theme.scss')).toBe('custom-theme',
311322
'Expected the old custom theme content to be unchanged.');

0 commit comments

Comments
 (0)