Skip to content

Commit 30797e0

Browse files
committed
feat(@angular-devkit/schematics): add runSchematic and runExternalSchematic methods
These async method are a replacement for the Obervable based `runSchematicAsync` and `runExternalSchematicAsync` methods. DEPRECATED: The Observable based `SchematicTestRunner.runSchematicAsync` and `SchematicTestRunner.runExternalSchematicAsync` method have been deprecated in favor of the Promise based SchematicTestRunner.runSchematic` and `SchematicTestRunner.runExternalSchematic`.
1 parent 9771696 commit 30797e0

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

goldens/public-api/angular_devkit/schematics/testing/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ export class SchematicTestRunner {
2525
// (undocumented)
2626
registerCollection(collectionName: string, collectionPath: string): void;
2727
// (undocumented)
28+
runExternalSchematic<SchematicSchemaT extends object>(collectionName: string, schematicName: string, opts?: SchematicSchemaT, tree?: Tree_2): Promise<UnitTestTree>;
29+
// @deprecated (undocumented)
2830
runExternalSchematicAsync<SchematicSchemaT extends object>(collectionName: string, schematicName: string, opts?: SchematicSchemaT, tree?: Tree_2): Observable<UnitTestTree>;
2931
// (undocumented)
32+
runSchematic<SchematicSchemaT extends object>(schematicName: string, opts?: SchematicSchemaT, tree?: Tree_2): Promise<UnitTestTree>;
33+
// @deprecated (undocumented)
3034
runSchematicAsync<SchematicSchemaT extends object>(schematicName: string, opts?: SchematicSchemaT, tree?: Tree_2): Observable<UnitTestTree>;
3135
// (undocumented)
3236
get tasks(): TaskConfiguration[];

packages/angular_devkit/schematics/testing/schematic-test-runner.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { logging, schema } from '@angular-devkit/core';
10-
import { Observable, of as observableOf } from 'rxjs';
10+
import { Observable, from, lastValueFrom, of as observableOf } from 'rxjs';
1111
import { map } from 'rxjs/operators';
1212
import {
1313
Collection,
@@ -78,34 +78,57 @@ export class SchematicTestRunner {
7878
this._engineHost.registerCollection(collectionName, collectionPath);
7979
}
8080

81-
runSchematicAsync<SchematicSchemaT extends object>(
81+
async runSchematic<SchematicSchemaT extends object>(
8282
schematicName: string,
8383
opts?: SchematicSchemaT,
8484
tree?: Tree,
85-
): Observable<UnitTestTree> {
85+
): Promise<UnitTestTree> {
8686
const schematic = this._collection.createSchematic(schematicName, true);
8787
const host = observableOf(tree || new HostTree());
8888
this._engineHost.clearTasks();
8989

90-
return schematic
91-
.call(opts || {}, host, { logger: this._logger })
92-
.pipe(map((tree) => new UnitTestTree(tree)));
90+
const newTree = await lastValueFrom(schematic.call(opts || {}, host, { logger: this._logger }));
91+
92+
return new UnitTestTree(newTree);
9393
}
9494

95-
runExternalSchematicAsync<SchematicSchemaT extends object>(
96-
collectionName: string,
95+
/**
96+
* @deprecated since version 15.1. Use `runSchematic` instead.
97+
*/
98+
runSchematicAsync<SchematicSchemaT extends object>(
9799
schematicName: string,
98100
opts?: SchematicSchemaT,
99101
tree?: Tree,
100102
): Observable<UnitTestTree> {
103+
return from(this.runSchematic(schematicName, opts, tree));
104+
}
105+
106+
async runExternalSchematic<SchematicSchemaT extends object>(
107+
collectionName: string,
108+
schematicName: string,
109+
opts?: SchematicSchemaT,
110+
tree?: Tree,
111+
): Promise<UnitTestTree> {
101112
const externalCollection = this._engine.createCollection(collectionName);
102113
const schematic = externalCollection.createSchematic(schematicName, true);
103114
const host = observableOf(tree || new HostTree());
104115
this._engineHost.clearTasks();
105116

106-
return schematic
107-
.call(opts || {}, host, { logger: this._logger })
108-
.pipe(map((tree) => new UnitTestTree(tree)));
117+
const newTree = await lastValueFrom(schematic.call(opts || {}, host, { logger: this._logger }));
118+
119+
return new UnitTestTree(newTree);
120+
}
121+
122+
/**
123+
* @deprecated since version 15.1. Use `runExternalSchematic` instead.
124+
*/
125+
runExternalSchematicAsync<SchematicSchemaT extends object>(
126+
collectionName: string,
127+
schematicName: string,
128+
opts?: SchematicSchemaT,
129+
tree?: Tree,
130+
): Observable<UnitTestTree> {
131+
return from(this.runExternalSchematic(collectionName, schematicName, opts, tree));
109132
}
110133

111134
callRule(rule: Rule, tree: Tree, parentContext?: Partial<SchematicContext>): Observable<Tree> {

0 commit comments

Comments
 (0)