Add beforeAll and afterAll hooks.

This commit is contained in:
Bauke 2022-12-23 21:45:29 +01:00
parent 2e844596bd
commit d8f64e1de6
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 19 additions and 0 deletions

View File

@ -17,8 +17,19 @@ export class Group {
public results: Result[] = [];
public tests: Test[] = [];
private _afterAll: (() => Promise<void>) | undefined;
private _beforeAll: (() => Promise<void>) | undefined;
constructor(public name: string) {}
afterAll(fn: Group["_afterAll"]): void {
this._afterAll = fn;
}
beforeAll(fn: Group["_beforeAll"]): void {
this._beforeAll = fn;
}
/** Create a new test case that doesn't get run. */
skip(name: Test["name"], fn: Test["fn"]): void {
this.tests.push(new Test(name, fn, {skip: true}));
@ -31,10 +42,18 @@ export class Group {
/** Run all the tests from this group and display their results. */
async run(): Promise<void> {
if (this._beforeAll !== undefined) {
await this._beforeAll();
}
const results = await Promise.all(
this.tests.map(async (test) => test.run(this.context)),
);
if (this._afterAll !== undefined) {
await this._afterAll();
}
console.log(
`# %c${this.name}`,
"font-weight: bold; text-decoration: underline;",