Skip to content

Commit ca2598b

Browse files
Broccohansl
authored andcommitted
test: Refactor schematic test structure
1 parent f633cfc commit ca2598b

16 files changed

Lines changed: 100 additions & 47 deletions

File tree

packages/angular_devkit/schematics/BUILD

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ ts_library(
3737
module_root = "tools"
3838
)
3939

40+
ts_library(
41+
name = "test",
42+
srcs = glob(
43+
include = ["test/**/*.ts"],
44+
exclude = [],
45+
),
46+
deps = [
47+
":schematics",
48+
":tools",
49+
"//packages/angular_devkit/core",
50+
# @deps: rxjs
51+
],
52+
tsconfig = "//:tsconfig.json",
53+
module_name = "@angular-devkit/schematics/test",
54+
module_root = "test"
55+
)
56+
4057
ts_library(
4158
name = "spec",
4259
srcs = glob(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. 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+
export * from './schematic-test-runner';

packages/schematics/angular/utility/schematic-test-runner.ts renamed to packages/angular_devkit/schematics/test/schematic-test-runner.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { SchemaClassFactory } from '@ngtools/json-schema';
1919
import { Observable } from 'rxjs/Observable';
2020

2121

22-
export interface SchemaT {}
22+
export interface SchematicSchemaT {}
2323

2424
export class SchematicTestRunner {
2525
private engineHost: NodeModulesEngineHost;
@@ -34,9 +34,9 @@ export class SchematicTestRunner {
3434
this.engineHost = new NodeModulesEngineHost();
3535
this.engine = new SchematicEngine(this.engineHost);
3636
this.engineHost.registerOptionsTransform((
37-
schematic: FileSystemSchematicDesc, opts: SchemaT) => {
37+
schematic: FileSystemSchematicDesc, opts: SchematicSchemaT) => {
3838
if (schematic.schema && schematic.schemaJson) {
39-
const SchemaMetaClass = SchemaClassFactory<SchemaT>(schematic.schemaJson);
39+
const SchemaMetaClass = SchemaClassFactory<SchematicSchemaT>(schematic.schemaJson);
4040
const schemaClass = new SchemaMetaClass(opts);
4141

4242
return schemaClass.$$root();
@@ -47,44 +47,26 @@ export class SchematicTestRunner {
4747
this.collection = this.engine.createCollection(this.collectionName);
4848
}
4949

50-
runSchematicAsync(schematicName: string, opts: SchemaT, tree?: Tree): Observable<Tree> {
50+
runSchematicAsync(schematicName: string, opts?: SchematicSchemaT, tree?: Tree): Observable<Tree> {
5151
const schematic = this.collection.createSchematic(schematicName);
5252
const host = Observable.of(tree || new VirtualTree);
5353

54-
return schematic.call(opts, host);
54+
return schematic.call(opts || {}, host);
5555
}
5656

57-
runSchematic(schematicName: string, opts: SchemaT, tree?: Tree): Tree {
57+
runSchematic(schematicName: string, opts?: SchematicSchemaT, tree?: Tree): Tree {
5858
const schematic = this.collection.createSchematic(schematicName);
5959

60-
let result: Tree = tree || new VirtualTree;
61-
const host = Observable.of(result);
60+
let result: Tree | null = null;
61+
const host = Observable.of(tree || new VirtualTree);
6262

63-
schematic.call(opts, host)
63+
schematic.call(opts || {}, host)
6464
.subscribe(t => result = t);
6565

66-
return result;
67-
}
68-
69-
public static createAppNgModule(tree: Tree, path?: string): Tree {
70-
tree.create(path || '/src/app/app.module.ts', `
71-
import { BrowserModule } from '@angular/platform-browser';
72-
import { NgModule } from '@angular/core';
73-
import { AppComponent } from './app.component';
74-
75-
@NgModule({
76-
declarations: [
77-
AppComponent
78-
],
79-
imports: [
80-
BrowserModule
81-
],
82-
providers: [],
83-
bootstrap: [AppComponent]
84-
})
85-
export class AppModule { }
86-
`);
66+
if (result === null) {
67+
throw new Error('Schematic is async, please use runSchematicAsync');
68+
}
8769

88-
return tree;
70+
return result;
8971
}
9072
}

packages/schematics/angular/application/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { SchematicTestRunner } from '../utility/schematic-test-runner';
8+
import { SchematicTestRunner } from '@angular-devkit/schematics/test';
99
import { Schema as AppSchema } from './schema';
1010

1111

packages/schematics/angular/class/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { SchematicTestRunner } from '../utility/schematic-test-runner';
8+
import { SchematicTestRunner } from '@angular-devkit/schematics/test';
99
import { Schema as ClassSchema } from './schema';
1010

1111

packages/schematics/angular/component/index_spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { Tree, VirtualTree } from '@angular-devkit/schematics';
9-
import { SchematicTestRunner } from '../utility/schematic-test-runner';
9+
import { SchematicTestRunner } from '@angular-devkit/schematics/test';
10+
import { createAppModule } from '../utility/test';
1011
import { Schema as ComponentSchema } from './schema';
1112

1213

@@ -32,7 +33,7 @@ describe('Component Schematic', () => {
3233

3334
beforeEach(() => {
3435
appTree = new VirtualTree();
35-
appTree = SchematicTestRunner.createAppNgModule(appTree);
36+
appTree = createAppModule(appTree);
3637
});
3738

3839
it('should create a component', () => {

packages/schematics/angular/directive/index_spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { Tree, VirtualTree } from '@angular-devkit/schematics';
9-
import { SchematicTestRunner } from '../utility/schematic-test-runner';
9+
import { SchematicTestRunner } from '@angular-devkit/schematics/test';
10+
import { createAppModule } from '../utility/test';
1011
import { Schema as DirectiveSchema } from './schema';
1112

1213

@@ -27,7 +28,7 @@ describe('Directive Schematic', () => {
2728

2829
beforeEach(() => {
2930
appTree = new VirtualTree();
30-
appTree = SchematicTestRunner.createAppNgModule(appTree);
31+
appTree = createAppModule(appTree);
3132
});
3233

3334
it('should create a directive', () => {

packages/schematics/angular/enum/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { SchematicTestRunner } from '../utility/schematic-test-runner';
8+
import { SchematicTestRunner } from '@angular-devkit/schematics/test';
99
import { Schema as EnumSchematic } from './schema';
1010

1111

packages/schematics/angular/guard/index_spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { Tree, VirtualTree } from '@angular-devkit/schematics';
9-
import { SchematicTestRunner } from '../utility/schematic-test-runner';
9+
import { SchematicTestRunner } from '@angular-devkit/schematics/test';
10+
import { createAppModule } from '../utility/test';
1011
import { Schema as GuardSchema } from './schema';
1112

1213

@@ -25,7 +26,7 @@ describe('Guard Schematic', () => {
2526

2627
beforeEach(() => {
2728
appTree = new VirtualTree();
28-
appTree = SchematicTestRunner.createAppNgModule(appTree);
29+
appTree = createAppModule(appTree);
2930
});
3031

3132
it('should create a guard', () => {

packages/schematics/angular/interface/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { SchematicTestRunner } from '../utility/schematic-test-runner';
8+
import { SchematicTestRunner } from '@angular-devkit/schematics/test';
99
import { Schema as InterfaceSchema } from './schema';
1010

1111

0 commit comments

Comments
 (0)