From d8f64e1de672ebbf4cbc3ed2f11644ebae03423c Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 23 Dec 2022 21:45:29 +0100 Subject: [PATCH] Add beforeAll and afterAll hooks. --- source/group.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/source/group.ts b/source/group.ts index 8dabe9c..10b8c63 100644 --- a/source/group.ts +++ b/source/group.ts @@ -17,8 +17,19 @@ export class Group { public results: Result[] = []; public tests: Test[] = []; + private _afterAll: (() => Promise) | undefined; + private _beforeAll: (() => Promise) | 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 { + 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;",