Skip to content

Commit e81a6f1

Browse files
clydinalan-agius4
authored andcommitted
test(@angular-devkit/core): 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 1b9880c commit e81a6f1

File tree

4 files changed

+33
-64
lines changed

4 files changed

+33
-64
lines changed

packages/angular_devkit/core/src/experimental/jobs/simple-scheduler_spec.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import { promisify } from 'util';
1313
import { JobHandlerContext, JobOutboundMessage, JobOutboundMessageKind, JobState } from './api';
1414
import { createJobHandler } from './create-job-handler';
1515
import { SimpleJobRegistry } from './simple-registry';
16-
import { SimpleScheduler } from './simple-scheduler';
16+
import {
17+
JobArgumentSchemaValidationError,
18+
JobOutputSchemaValidationError,
19+
SimpleScheduler,
20+
} from './simple-scheduler';
1721

1822
const flush = promisify(setImmediate);
1923

@@ -94,16 +98,9 @@ describe('SimpleScheduler', () => {
9498
);
9599

96100
await scheduler.schedule('add', [1, 2, 3, 4]).output.toPromise();
97-
try {
98-
await scheduler.schedule('add', ['1', 2, 3, 4]).output.toPromise();
99-
expect(true).toBe(false);
100-
} catch (e) {
101-
// TODO: enable this when https://github.com/bazelbuild/rules_typescript/commit/37807e2c4
102-
// is released, otherwise this breaks because bazel downgrade to ES5 which does not support
103-
// extending Error.
104-
// expect(e instanceof JobInboundMessageSchemaValidationError).toBe(true);
105-
expect(e.message).toMatch(/"\/0" must be number/);
106-
}
101+
await expectAsync(
102+
scheduler.schedule('add', ['1', 2, 3, 4]).output.toPromise(),
103+
).toBeRejectedWithError(JobArgumentSchemaValidationError);
107104
});
108105

109106
it('validates outputs', async () => {
@@ -115,16 +112,9 @@ describe('SimpleScheduler', () => {
115112
},
116113
);
117114

118-
try {
119-
await scheduler.schedule('add', [1, 2, 3, 4]).output.toPromise();
120-
expect(true).toBe(false);
121-
} catch (e) {
122-
// TODO: enable this when https://github.com/bazelbuild/rules_typescript/commit/37807e2c4
123-
// is released, otherwise this breaks because bazel downgrade to ES5 which does not support
124-
// extending Error.
125-
// expect(e instanceof JobOutputSchemaValidationError).toBe(true);
126-
expect(e.message).toMatch(/"".*number/);
127-
}
115+
await expectAsync(
116+
scheduler.schedule('add', [1, 2, 3, 4]).output.toPromise(),
117+
).toBeRejectedWithError(JobOutputSchemaValidationError);
128118
});
129119

130120
it('works with dependencies', async () => {

packages/angular_devkit/core/src/workspace/core_spec.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,9 @@ describe('readWorkspace', () => {
116116
},
117117
};
118118

119-
try {
120-
await readWorkspace(requestedPath, host);
121-
fail();
122-
} catch (e) {
123-
expect(e.message).toContain('Unable to determine format for workspace path');
124-
}
119+
await expectAsync(readWorkspace(requestedPath, host)).toBeRejectedWithError(
120+
/Unable to determine format for workspace path/,
121+
);
125122
});
126123

127124
it('errors when reading from specified file path with invalid specified format', async () => {
@@ -146,12 +143,9 @@ describe('readWorkspace', () => {
146143
},
147144
};
148145

149-
try {
150-
await readWorkspace(requestedPath, host, 12 as WorkspaceFormat);
151-
fail();
152-
} catch (e) {
153-
expect(e.message).toContain('Unsupported workspace format');
154-
}
146+
await expectAsync(
147+
readWorkspace(requestedPath, host, 12 as WorkspaceFormat),
148+
).toBeRejectedWithError(/Unsupported workspace format/);
155149
});
156150

157151
it('attempts to find/read from directory path', async () => {
@@ -260,12 +254,9 @@ describe('readWorkspace', () => {
260254
},
261255
};
262256

263-
try {
264-
await readWorkspace(requestedPath, host);
265-
fail();
266-
} catch (e) {
267-
expect(e.message).toContain('Unable to locate a workspace file');
268-
}
257+
await expectAsync(readWorkspace(requestedPath, host)).toBeRejectedWithError(
258+
/Unable to locate a workspace file/,
259+
);
269260
});
270261
});
271262

@@ -324,12 +315,9 @@ describe('writeWorkspace', () => {
324315
},
325316
};
326317

327-
try {
328-
await writeWorkspace({} as WorkspaceDefinition, host, requestedPath, 12 as WorkspaceFormat);
329-
fail();
330-
} catch (e) {
331-
expect(e.message).toContain('Unsupported workspace format');
332-
}
318+
await expectAsync(
319+
writeWorkspace({} as WorkspaceDefinition, host, requestedPath, 12 as WorkspaceFormat),
320+
).toBeRejectedWithError(/Unsupported workspace format/);
333321
});
334322

335323
it('errors when writing custom workspace without specified format', async () => {
@@ -356,11 +344,8 @@ describe('writeWorkspace', () => {
356344
},
357345
};
358346

359-
try {
360-
await writeWorkspace({} as WorkspaceDefinition, host, requestedPath);
361-
fail();
362-
} catch (e) {
363-
expect(e.message).toContain('A format is required');
364-
}
347+
await expectAsync(
348+
writeWorkspace({} as WorkspaceDefinition, host, requestedPath),
349+
).toBeRejectedWithError(/A format is required/);
365350
});
366351
});

packages/angular_devkit/core/src/workspace/json/reader_spec.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,9 @@ describe('readJsonWorkpace Parsing', () => {
120120
}
121121
`);
122122

123-
try {
124-
await readJsonWorkspace('', host);
125-
fail();
126-
} catch (e) {
127-
expect(e.message).toContain('Invalid format version detected');
128-
}
123+
await expectAsync(readJsonWorkspace('', host)).toBeRejectedWithError(
124+
/Invalid format version detected/,
125+
);
129126
});
130127

131128
it('errors on missing version', async () => {
@@ -136,12 +133,9 @@ describe('readJsonWorkpace Parsing', () => {
136133
}
137134
`);
138135

139-
try {
140-
await readJsonWorkspace('', host);
141-
fail();
142-
} catch (e) {
143-
expect(e.message).toContain('version specifier not found');
144-
}
136+
await expectAsync(readJsonWorkspace('', host)).toBeRejectedWithError(
137+
/version specifier not found/,
138+
);
145139
});
146140
});
147141

packages/angular_devkit/core/src/workspace/json/writer_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function createTestCaseHost(inputData = '') {
4545
);
4646
expect(data).toEqual(testCase);
4747
} catch (e) {
48-
fail(`Unable to load test case '${path}': ${e.message || e}`);
48+
fail(`Unable to load test case '${path}': ${e instanceof Error ? e.message : e}`);
4949
}
5050
},
5151
async isFile() {

0 commit comments

Comments
 (0)