From 7791d2aed1460bfeaf5eaa74d090f732096bad78 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 18 Oct 2023 17:46:28 +0530 Subject: [PATCH] feat: improvements to onGroup, onTest, tap, and onSuite hooks Earlier these hooks were added when new instances were registered. Now, they are executed for pre-registered instances too --- src/group/main.ts | 1 + src/runner.ts | 1 + src/suite/main.ts | 12 +++++++ test/runner/configure.spec.ts | 28 +++++++++++++++++ test/suite/configure.spec.ts | 59 +++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+) diff --git a/src/group/main.ts b/src/group/main.ts index 1765760..c01486f 100644 --- a/src/group/main.ts +++ b/src/group/main.ts @@ -155,6 +155,7 @@ export class Group> extends Macroable { * Tap into each test and configure it */ tap(callback: (test: Test) => void): this { + this.tests.forEach((test) => callback(test)) this.#tapsCallbacks.push(callback) return this } diff --git a/src/runner.ts b/src/runner.ts index 39e8101..d6590fe 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -116,6 +116,7 @@ export class Runner> extends Macroable { * Tap into each suite and configure it */ onSuite(callback: (suite: Suite) => void): this { + this.suites.forEach((suite) => callback(suite)) this.#configureSuiteCallbacks.push(callback) return this } diff --git a/src/suite/main.ts b/src/suite/main.ts index 8918793..43a5ea3 100644 --- a/src/suite/main.ts +++ b/src/suite/main.ts @@ -88,6 +88,12 @@ export class Suite> extends Macroable { * Tap into each test and configure it */ onTest(callback: (test: Test) => void): this { + this.stack.forEach((testOrGroup) => { + if (testOrGroup instanceof Test) { + callback(testOrGroup) + } + }) + this.#configureTestCallbacks.push(callback) return this } @@ -96,6 +102,12 @@ export class Suite> extends Macroable { * Tap into each group and configure it */ onGroup(callback: (group: Group) => void): this { + this.stack.forEach((testOrGroup) => { + if (testOrGroup instanceof Group) { + callback(testOrGroup) + } + }) + this.#configureGroupCallbacks.push(callback) return this } diff --git a/test/runner/configure.spec.ts b/test/runner/configure.spec.ts index 1cd54f4..17965cd 100644 --- a/test/runner/configure.spec.ts +++ b/test/runner/configure.spec.ts @@ -72,4 +72,32 @@ test.describe('configure | runner', () => { runner.registerReporter(listReporter) assert.deepEqual(runner.reporters, new Set([listReporter])) }) + + test('tap into suites after they have been registered', async () => { + const emitter = new Emitter() + + const runner = new Runner(emitter) + const refiner = new Refiner() + + /** + * First add a suite to the runner + */ + const unitSuite = new Suite('unit', emitter, refiner) + runner.add(unitSuite) + + /** + * Next define the hook + */ + runner.onSuite((suite) => (suite.name = `configured:${suite.name}`)) + + /** + * Add another suite to the runner + */ + const functionalSuite = new Suite('functional', emitter, refiner) + runner.add(functionalSuite) + + assert.deepEqual(runner.suites, [unitSuite, functionalSuite]) + assert.equal(unitSuite.name, 'configured:unit') + assert.equal(functionalSuite.name, 'configured:functional') + }) }) diff --git a/test/suite/configure.spec.ts b/test/suite/configure.spec.ts index 7eeef5d..c6f6074 100644 --- a/test/suite/configure.spec.ts +++ b/test/suite/configure.spec.ts @@ -77,4 +77,63 @@ test.describe('configure', () => { assert.equal(testInstance.options.timeout, 0) }) + + test('tap into tests after they have been registered', async () => { + const emitter = new Emitter() + const refiner = new Refiner({}) + + const suite = new Suite('sample suite', new Emitter(), refiner) + const testInstance = new Test('2 + 2 = 4', new TestContext(), emitter, refiner) + const test1Instance = new Test('2 + 2 = 4', new TestContext(), emitter, refiner) + + /** + * First add test to suite + */ + suite.add(testInstance) + + /** + * Define the hook afterwards + */ + suite.onTest((t) => t.tags(['@hooked'], 'append')) + + /** + * Add another test + */ + suite.add(test1Instance) + + assert.deepEqual(testInstance.options.tags, ['@hooked']) + assert.deepEqual(test1Instance.options.tags, ['@hooked']) + }) + + test('tap into groups after they have been registered', async () => { + const emitter = new Emitter() + const refiner = new Refiner({}) + + const suite = new Suite('sample suite', new Emitter(), refiner) + const group = new Group('sample group', emitter, refiner) + const testInstance = new Test('2 + 2 = 4', new TestContext(), emitter, refiner) + + const group1 = new Group('sample group', emitter, refiner) + const test1Instance = new Test('2 + 2 = 4', new TestContext(), emitter, refiner) + + /** + * First add test to group and group to suite + */ + group.add(testInstance) + suite.add(group) + + /** + * Define hook + */ + suite.onGroup((g) => g.tap((t) => t.tags(['@hooked'], 'append'))) + + /** + * Add another group to suite and test to the group + */ + suite.add(group1) + group1.add(test1Instance) + + assert.deepEqual(testInstance.options.tags, ['@hooked']) + assert.deepEqual(test1Instance.options.tags, ['@hooked']) + }) })