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.
This commit is contained in:
Charles Lyding 2022-06-13 15:43:12 -04:00 committed by Alan Agius
parent e81a6f1c46
commit bb43e0a7c4
4 changed files with 21 additions and 40 deletions

View File

@ -159,13 +159,10 @@ describe('Component Schematic', () => {
it('should fail if specified module does not exist', async () => {
const options = { ...defaultOptions, module: '/projects/bar/src/app.moduleXXX.ts' };
let thrownError: Error | null = null;
try {
await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
} catch (err) {
thrownError = err;
}
expect(thrownError).toBeDefined();
await expectAsync(
schematicRunner.runSchematicAsync('component', options, appTree).toPromise(),
).toBeRejected();
});
it('should handle upper case paths', async () => {

View File

@ -108,13 +108,10 @@ describe('Directive Schematic', () => {
it('should fail if specified module does not exist', async () => {
const options = { ...defaultOptions, module: '/projects/bar/src/app/app.moduleXXX.ts' };
let thrownError: Error | null = null;
try {
await schematicRunner.runSchematicAsync('directive', options, appTree).toPromise();
} catch (err) {
thrownError = err;
}
expect(thrownError).toBeDefined();
await expectAsync(
schematicRunner.runSchematicAsync('directive', options, appTree).toPromise(),
).toBeRejected();
});
it('should converts dash-cased-name to a camelCasedSelector', async () => {

View File

@ -72,13 +72,10 @@ describe('Pipe Schematic', () => {
it('should fail if specified module does not exist', async () => {
const options = { ...defaultOptions, module: '/projects/bar/src/app/app.moduleXXX.ts' };
let thrownError: Error | null = null;
try {
await schematicRunner.runSchematicAsync('pipe', options, appTree).toPromise();
} catch (err) {
thrownError = err;
}
expect(thrownError).toBeDefined();
await expectAsync(
schematicRunner.runSchematicAsync('pipe', options, appTree).toPromise(),
).toBeRejected();
});
it('should handle a path in the name and module options', async () => {

View File

@ -43,34 +43,24 @@ describe('find-module', () => {
it('should throw if no modules found', () => {
host.create('/foo/src/app/oops.module.ts', 'app module');
try {
findModule(host, 'foo/src/app/bar');
throw new Error('Succeeded, should have failed');
} catch (err) {
expect(err.message).toMatch(/More than one module matches/);
}
expect(() => findModule(host, 'foo/src/app/bar')).toThrowError(
/More than one module matches/,
);
});
it('should throw if only routing modules were found', () => {
host = new EmptyTree();
host.create('/foo/src/app/anything-routing.module.ts', 'anything routing module');
try {
findModule(host, 'foo/src/app/anything-routing');
throw new Error('Succeeded, should have failed');
} catch (err) {
expect(err.message).toMatch(/Could not find a non Routing NgModule/);
}
expect(() => findModule(host, 'foo/src/app/anything-routing')).toThrowError(
/Could not find a non Routing NgModule/,
);
});
it('should throw if two modules found', () => {
try {
host = new EmptyTree();
findModule(host, 'foo/src/app/bar');
throw new Error('Succeeded, should have failed');
} catch (err) {
expect(err.message).toMatch(/Could not find an NgModule/);
}
host = new EmptyTree();
expect(() => findModule(host, 'foo/src/app/bar')).toThrowError(/Could not find an NgModule/);
});
it('should accept custom ext for module', () => {