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