Skip to content

Commit 9ffd19a

Browse files
committed
fix schematic tests
1 parent 27b3b19 commit 9ffd19a

File tree

14 files changed

+164
-158
lines changed

14 files changed

+164
-158
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ describe('CDK ng-add', () => {
66
let runner: SchematicTestRunner;
77
let appTree: Tree;
88

9-
beforeEach(() => {
9+
beforeEach(async () => {
1010
runner = new SchematicTestRunner('schematics', require.resolve('../collection.json'));
11-
appTree = createTestApp(runner);
11+
appTree = await createTestApp(runner);
1212
});
1313

1414
it('should update the package.json', () => {

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('CDK drag-drop schematic', () => {
2020
runner = new SchematicTestRunner('schematics', require.resolve('../../collection.json'));
2121
});
2222

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

@@ -34,23 +34,23 @@ describe('CDK drag-drop schematic', () => {
3434
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
3535
});
3636

37-
it('should add drag-drop module', () => {
38-
const tree = runner.runSchematic('drag-drop', baseOptions, createTestApp(runner));
37+
it('should add drag-drop module', async () => {
38+
const tree = runner.runSchematic('drag-drop', baseOptions, await createTestApp(runner));
3939
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
4040

4141
expect(moduleContent).toContain('DragDropModule');
4242
});
4343

4444
describe('style option', () => {
45-
it('should respect the option value', () => {
45+
it('should respect the option value', async () => {
4646
const tree = runner.runSchematic(
47-
'drag-drop', {style: 'scss', ...baseOptions}, createTestApp(runner));
47+
'drag-drop', {style: 'scss', ...baseOptions}, await createTestApp(runner));
4848

4949
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
5050
});
5151

52-
it('should respect the deprecated "styleext" option value', () => {
53-
let tree = createTestApp(runner);
52+
it('should respect the deprecated "styleext" option value', async () => {
53+
let tree = await createTestApp(runner);
5454
const workspace = getWorkspace(tree);
5555
const project = getProjectFromWorkspace(workspace);
5656

@@ -66,9 +66,9 @@ describe('CDK drag-drop schematic', () => {
6666
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
6767
});
6868

69-
it('should not generate invalid stylesheets', () => {
69+
it('should not generate invalid stylesheets', async () => {
7070
const tree = runner.runSchematic(
71-
'drag-drop', {style: 'styl', ...baseOptions}, createTestApp(runner));
71+
'drag-drop', {style: 'styl', ...baseOptions}, await createTestApp(runner));
7272

7373
// In this case we expect the schematic to generate a plain "css" file because
7474
// the component schematics are using CSS style templates which are not compatible
@@ -79,56 +79,56 @@ describe('CDK drag-drop schematic', () => {
7979
'Expected the schematic to not generate a "stylus" file');
8080
});
8181

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

8686
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
8787
});
8888
});
8989

9090
describe('inlineStyle option', () => {
91-
it('should respect the option value', () => {
91+
it('should respect the option value', async () => {
9292
const tree = runner.runSchematic(
93-
'drag-drop', {inlineStyle: true, ...baseOptions}, createTestApp(runner));
93+
'drag-drop', {inlineStyle: true, ...baseOptions}, await createTestApp(runner));
9494

9595
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
9696
});
9797

98-
it('should fall back to the @schematics/angular:component option value', () => {
98+
it('should fall back to the @schematics/angular:component option value', async () => {
9999
const tree = runner.runSchematic(
100-
'drag-drop', baseOptions, createTestApp(runner, {inlineStyle: true}));
100+
'drag-drop', baseOptions, await createTestApp(runner, {inlineStyle: true}));
101101

102102
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
103103
});
104104
});
105105

106106
describe('inlineTemplate option', () => {
107-
it('should respect the option value', () => {
107+
it('should respect the option value', async () => {
108108
const tree = runner.runSchematic(
109-
'drag-drop', {inlineTemplate: true, ...baseOptions}, createTestApp(runner));
109+
'drag-drop', {inlineTemplate: true, ...baseOptions}, await createTestApp(runner));
110110

111111
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
112112
});
113113

114-
it('should fall back to the @schematics/angular:component option value', () => {
114+
it('should fall back to the @schematics/angular:component option value', async () => {
115115
const tree = runner.runSchematic(
116-
'drag-drop', baseOptions, createTestApp(runner, {inlineTemplate: true}));
116+
'drag-drop', baseOptions, await createTestApp(runner, {inlineTemplate: true}));
117117

118118
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
119119
});
120120
});
121121

122122
describe('skipTests option', () => {
123-
it('should respect the option value', () => {
123+
it('should respect the option value', async () => {
124124
const tree = runner.runSchematic(
125-
'drag-drop', {skipTests: true, ...baseOptions}, createTestApp(runner));
125+
'drag-drop', {skipTests: true, ...baseOptions}, await createTestApp(runner));
126126

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

130-
it('should respect the deprecated global "spec" option value', () => {
131-
let tree = createTestApp(runner);
130+
it('should respect the deprecated global "spec" option value', async () => {
131+
let tree = await createTestApp(runner);
132132
const workspace = getWorkspace(tree);
133133
const project = getProjectFromWorkspace(workspace);
134134

@@ -144,9 +144,9 @@ describe('CDK drag-drop schematic', () => {
144144
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
145145
});
146146

147-
it('should fall back to the @schematics/angular:component option value', () => {
147+
it('should fall back to the @schematics/angular:component option value', async () => {
148148
const tree = runner.runSchematic(
149-
'drag-drop', baseOptions, createTestApp(runner, {skipTests: true}));
149+
'drag-drop', baseOptions, await createTestApp(runner, {skipTests: true}));
150150

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

src/cdk/schematics/ng-update/test-cases/misc/method-call-checks.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {createTestCaseSetup} from '../../../testing';
44
describe('v6 method call checks', () => {
55

66
it('should properly report invalid method calls', async () => {
7-
const {runFixers, removeTempDir} = createTestCaseSetup('migration-v6', migrationCollection,
8-
[require.resolve('./method-call-checks_input.ts')]);
7+
const {runFixers, removeTempDir} = await createTestCaseSetup('migration-v6',
8+
migrationCollection, [require.resolve('./method-call-checks_input.ts')]);
99

1010
const {logOutput} = await runFixers();
1111

src/cdk/schematics/testing/test-app.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
1010

1111
/** Create a base app used for testing. */
12-
export function createTestApp(runner: SchematicTestRunner, appOptions = {}): UnitTestTree {
12+
export async function createTestApp(runner: SchematicTestRunner, appOptions = {}):
13+
Promise<UnitTestTree> {
1314
const workspaceTree = runner.runExternalSchematic('@schematics/angular', 'workspace', {
1415
name: 'workspace',
1516
version: '6.0.0',
1617
newProjectRoot: 'projects',
1718
});
1819

19-
return runner.runExternalSchematic('@schematics/angular', 'application',
20-
{name: 'material', ...appOptions}, workspaceTree);
20+
return runner.runExternalSchematicAsync('@schematics/angular', 'application',
21+
{name: 'material', ...appOptions}, workspaceTree).toPromise();
2122
}

src/cdk/schematics/testing/test-case-setup.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ export function readFileContent(filePath: string): string {
3030
* Creates a test app schematic tree that will be copied over to a real filesystem location.
3131
* This is necessary because TSLint is not able to read from the virtual filesystem tree.
3232
*/
33-
export function createFileSystemTestApp(runner: SchematicTestRunner) {
33+
export async function createFileSystemTestApp(runner: SchematicTestRunner) {
3434
const tempFileSystemHost = new TempScopedNodeJsSyncHost();
35-
const appTree: UnitTestTree = createTestApp(runner, {name: 'cdk-testing'});
35+
const appTree: UnitTestTree = await createTestApp(runner, {name: 'cdk-testing'});
3636
const tempPath = getSystemPath(tempFileSystemHost.root);
3737

3838
// Since the TSLint fix task expects all files to be present on the real file system, we
@@ -44,7 +44,7 @@ export function createFileSystemTestApp(runner: SchematicTestRunner) {
4444
return {appTree, tempPath, removeTempDir: () => removeSync(tempPath)};
4545
}
4646

47-
export function createTestCaseSetup(migrationName: string, collectionPath: string,
47+
export async function createTestCaseSetup(migrationName: string, collectionPath: string,
4848
inputFiles: string[]) {
4949

5050
const runner = new SchematicTestRunner('schematics', collectionPath);
@@ -53,7 +53,7 @@ export function createTestCaseSetup(migrationName: string, collectionPath: strin
5353
let logOutput = '';
5454
runner.logger.subscribe(entry => logOutput += entry.message);
5555

56-
const {appTree, tempPath, removeTempDir} = createFileSystemTestApp(runner);
56+
const {appTree, tempPath, removeTempDir} = await createFileSystemTestApp(runner);
5757

5858
// Write each test-case input to the file-system. This is necessary because otherwise
5959
// TSLint won't be able to pick up the test cases.
@@ -66,7 +66,7 @@ export function createTestCaseSetup(migrationName: string, collectionPath: strin
6666
});
6767

6868
const runFixers = async function() {
69-
runner.runSchematic(migrationName, {}, appTree);
69+
await runner.runSchematicAsync(migrationName, {}, appTree).toPromise();
7070

7171
// Switch to the new temporary directory because otherwise TSLint cannot read the files.
7272
process.chdir(tempPath);
@@ -149,7 +149,7 @@ export function defineJasmineTestCases(versionName: string, collectionFile: stri
149149

150150
beforeAll(async () => {
151151
const {tempPath, runFixers, removeTempDir} =
152-
createTestCaseSetup(`migration-${versionName}`, collectionFile, inputFiles);
152+
await createTestCaseSetup(`migration-${versionName}`, collectionFile, inputFiles);
153153

154154
await runFixers();
155155

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ describe('ng-add schematic', () => {
1616
let runner: SchematicTestRunner;
1717
let appTree: Tree;
1818

19-
beforeEach(() => {
19+
beforeEach(async () => {
2020
runner = new SchematicTestRunner('schematics', require.resolve('../collection.json'));
21-
appTree = createTestApp(runner);
21+
appTree = await createTestApp(runner);
2222
});
2323

2424
/** Expects the given file to be in the styles of the specified workspace project. */
@@ -77,9 +77,9 @@ describe('ng-add schematic', () => {
7777
'./node_modules/@angular/material/prebuilt-themes/indigo-pink.css');
7878
});
7979

80-
it('should support adding a custom theme', () => {
80+
it('should support adding a custom theme', async () => {
8181
// TODO(devversion): do not re-create test app here.
82-
appTree = createTestApp(runner, {style: 'scss'});
82+
appTree = await createTestApp(runner, {style: 'scss'});
8383

8484
const tree = runner.runSchematic('ng-add-setup-project', {theme: 'custom'}, appTree);
8585

@@ -94,9 +94,9 @@ describe('ng-add schematic', () => {
9494
expect(themeContent).toContain(`$app-primary: mat-palette(`);
9595
});
9696

97-
it('should create a custom theme file if no SCSS file could be found', () => {
97+
it('should create a custom theme file if no SCSS file could be found', async () => {
9898
// TODO(devversion): do not re-create test app here.
99-
appTree = createTestApp(runner, {style: 'css'});
99+
appTree = await createTestApp(runner, {style: 'css'});
100100

101101
const tree = runner.runSchematic('ng-add-setup-project', {theme: 'custom'}, appTree);
102102
const workspace = getWorkspace(tree);

src/material/schematics/ng-generate/address-form/index.spec.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ describe('Material address-form schematic', () => {
1414
runner = new SchematicTestRunner('schematics', require.resolve('../../collection.json'));
1515
});
1616

17-
it('should create address-form files and add them to module', () => {
18-
const tree = runner.runSchematic('address-form', baseOptions, createTestApp(runner));
17+
it('should create address-form files and add them to module', async () => {
18+
const tree = runner.runSchematic('address-form', baseOptions, await createTestApp(runner));
1919
const files = tree.files;
2020

2121
expect(files).toContain('/projects/material/src/app/foo/foo.component.css');
@@ -28,8 +28,8 @@ describe('Material address-form schematic', () => {
2828
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
2929
});
3030

31-
it('should add address-form imports to module', () => {
32-
const tree = runner.runSchematic('address-form', baseOptions, createTestApp(runner));
31+
it('should add address-form imports to module', async () => {
32+
const tree = runner.runSchematic('address-form', baseOptions, await createTestApp(runner));
3333
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
3434

3535
expect(moduleContent).toContain('MatInputModule');
@@ -39,71 +39,72 @@ describe('Material address-form schematic', () => {
3939
expect(moduleContent).toContain('ReactiveFormsModule');
4040
});
4141

42-
it('should throw if no name has been specified', () => {
42+
it('should throw if no name has been specified', async () => {
43+
const appTree = await createTestApp(runner);
4344
expect(() => {
44-
runner.runSchematic('address-form', {project: 'material'}, createTestApp(runner));
45+
runner.runSchematic('address-form', {project: 'material'}, appTree);
4546
}).toThrowError(/required property 'name'/);
4647
});
4748

4849
describe('style option', () => {
49-
it('should respect the option value', () => {
50+
it('should respect the option value', async () => {
5051
const tree = runner.runSchematic(
51-
'address-form', {style: 'scss', ...baseOptions}, createTestApp(runner));
52+
'address-form', {style: 'scss', ...baseOptions}, await createTestApp(runner));
5253

5354
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
5455
});
5556

56-
it('should fall back to the @schematics/angular:component option value', () => {
57+
it('should fall back to the @schematics/angular:component option value', async () => {
5758
const tree = runner.runSchematic(
58-
'address-form', baseOptions, createTestApp(runner, {style: 'less'}));
59+
'address-form', baseOptions, await createTestApp(runner, {style: 'less'}));
5960

6061
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
6162
});
6263
});
6364

6465
describe('inlineStyle option', () => {
65-
it('should respect the option value', () => {
66+
it('should respect the option value', async () => {
6667
const tree = runner.runSchematic(
67-
'address-form', {inlineStyle: true, ...baseOptions}, createTestApp(runner));
68+
'address-form', {inlineStyle: true, ...baseOptions}, await createTestApp(runner));
6869

6970
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
7071
});
7172

72-
it('should fall back to the @schematics/angular:component option value', () => {
73+
it('should fall back to the @schematics/angular:component option value', async () => {
7374
const tree = runner.runSchematic(
74-
'address-form', baseOptions, createTestApp(runner, {inlineStyle: true}));
75+
'address-form', baseOptions, await createTestApp(runner, {inlineStyle: true}));
7576

7677
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
7778
});
7879
});
7980

8081
describe('inlineTemplate option', () => {
81-
it('should respect the option value', () => {
82+
it('should respect the option value', async () => {
8283
const tree = runner.runSchematic(
83-
'address-form', {inlineTemplate: true, ...baseOptions}, createTestApp(runner));
84+
'address-form', {inlineTemplate: true, ...baseOptions}, await createTestApp(runner));
8485

8586
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
8687
});
8788

88-
it('should fall back to the @schematics/angular:component option value', () => {
89+
it('should fall back to the @schematics/angular:component option value', async () => {
8990
const tree = runner.runSchematic(
90-
'address-form', baseOptions, createTestApp(runner, {inlineTemplate: true}));
91+
'address-form', baseOptions, await createTestApp(runner, {inlineTemplate: true}));
9192

9293
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
9394
});
9495
});
9596

9697
describe('skipTests option', () => {
97-
it('should respect the option value', () => {
98+
it('should respect the option value', async () => {
9899
const tree = runner.runSchematic(
99-
'address-form', {skipTests: true, ...baseOptions}, createTestApp(runner));
100+
'address-form', {skipTests: true, ...baseOptions}, await createTestApp(runner));
100101

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

104-
it('should fall back to the @schematics/angular:component option value', () => {
105+
it('should fall back to the @schematics/angular:component option value', async () => {
105106
const tree = runner.runSchematic(
106-
'address-form', baseOptions, createTestApp(runner, {skipTests: true}));
107+
'address-form', baseOptions, await createTestApp(runner, {skipTests: true}));
107108

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

0 commit comments

Comments
 (0)