Skip to content

Commit bb43e0a

Browse files
clydinalan-agius4
authored andcommitted
test(@schematics/angular): update several tests to use async expectations
Using Jasmine's `expectAsync` allows for reduced test code when testing that a function returning a promise rejects with a specific error.
1 parent e81a6f1 commit bb43e0a

File tree

4 files changed

+21
-40
lines changed

4 files changed

+21
-40
lines changed

packages/schematics/angular/component/index_spec.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,10 @@ describe('Component Schematic', () => {
159159

160160
it('should fail if specified module does not exist', async () => {
161161
const options = { ...defaultOptions, module: '/projects/bar/src/app.moduleXXX.ts' };
162-
let thrownError: Error | null = null;
163-
try {
164-
await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
165-
} catch (err) {
166-
thrownError = err;
167-
}
168-
expect(thrownError).toBeDefined();
162+
163+
await expectAsync(
164+
schematicRunner.runSchematicAsync('component', options, appTree).toPromise(),
165+
).toBeRejected();
169166
});
170167

171168
it('should handle upper case paths', async () => {

packages/schematics/angular/directive/index_spec.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,10 @@ describe('Directive Schematic', () => {
108108

109109
it('should fail if specified module does not exist', async () => {
110110
const options = { ...defaultOptions, module: '/projects/bar/src/app/app.moduleXXX.ts' };
111-
let thrownError: Error | null = null;
112-
try {
113-
await schematicRunner.runSchematicAsync('directive', options, appTree).toPromise();
114-
} catch (err) {
115-
thrownError = err;
116-
}
117-
expect(thrownError).toBeDefined();
111+
112+
await expectAsync(
113+
schematicRunner.runSchematicAsync('directive', options, appTree).toPromise(),
114+
).toBeRejected();
118115
});
119116

120117
it('should converts dash-cased-name to a camelCasedSelector', async () => {

packages/schematics/angular/pipe/index_spec.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,10 @@ describe('Pipe Schematic', () => {
7272

7373
it('should fail if specified module does not exist', async () => {
7474
const options = { ...defaultOptions, module: '/projects/bar/src/app/app.moduleXXX.ts' };
75-
let thrownError: Error | null = null;
76-
try {
77-
await schematicRunner.runSchematicAsync('pipe', options, appTree).toPromise();
78-
} catch (err) {
79-
thrownError = err;
80-
}
81-
expect(thrownError).toBeDefined();
75+
76+
await expectAsync(
77+
schematicRunner.runSchematicAsync('pipe', options, appTree).toPromise(),
78+
).toBeRejected();
8279
});
8380

8481
it('should handle a path in the name and module options', async () => {

packages/schematics/angular/utility/find-module_spec.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,24 @@ describe('find-module', () => {
4343

4444
it('should throw if no modules found', () => {
4545
host.create('/foo/src/app/oops.module.ts', 'app module');
46-
try {
47-
findModule(host, 'foo/src/app/bar');
48-
throw new Error('Succeeded, should have failed');
49-
} catch (err) {
50-
expect(err.message).toMatch(/More than one module matches/);
51-
}
46+
47+
expect(() => findModule(host, 'foo/src/app/bar')).toThrowError(
48+
/More than one module matches/,
49+
);
5250
});
5351

5452
it('should throw if only routing modules were found', () => {
5553
host = new EmptyTree();
5654
host.create('/foo/src/app/anything-routing.module.ts', 'anything routing module');
5755

58-
try {
59-
findModule(host, 'foo/src/app/anything-routing');
60-
throw new Error('Succeeded, should have failed');
61-
} catch (err) {
62-
expect(err.message).toMatch(/Could not find a non Routing NgModule/);
63-
}
56+
expect(() => findModule(host, 'foo/src/app/anything-routing')).toThrowError(
57+
/Could not find a non Routing NgModule/,
58+
);
6459
});
6560

6661
it('should throw if two modules found', () => {
67-
try {
68-
host = new EmptyTree();
69-
findModule(host, 'foo/src/app/bar');
70-
throw new Error('Succeeded, should have failed');
71-
} catch (err) {
72-
expect(err.message).toMatch(/Could not find an NgModule/);
73-
}
62+
host = new EmptyTree();
63+
expect(() => findModule(host, 'foo/src/app/bar')).toThrowError(/Could not find an NgModule/);
7464
});
7565

7666
it('should accept custom ext for module', () => {

0 commit comments

Comments
 (0)