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;",