Skip to content

Commit

Permalink
feat: improvements to onGroup, onTest, tap, and onSuite hooks
Browse files Browse the repository at this point in the history
Earlier these hooks were added when new instances were registered.
Now, they are executed for pre-registered instances too
  • Loading branch information
thetutlage committed Oct 18, 2023
1 parent 7979d68 commit 7791d2a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/group/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export class Group<Context extends Record<any, any>> extends Macroable {
* Tap into each test and configure it
*/
tap(callback: (test: Test<Context, any>) => void): this {
this.tests.forEach((test) => callback(test))
this.#tapsCallbacks.push(callback)
return this
}
Expand Down
1 change: 1 addition & 0 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class Runner<Context extends Record<any, any>> extends Macroable {
* Tap into each suite and configure it
*/
onSuite(callback: (suite: Suite<Context>) => void): this {
this.suites.forEach((suite) => callback(suite))
this.#configureSuiteCallbacks.push(callback)
return this
}
Expand Down
12 changes: 12 additions & 0 deletions src/suite/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ export class Suite<Context extends Record<any, any>> extends Macroable {
* Tap into each test and configure it
*/
onTest(callback: (test: Test<Context, any>) => void): this {
this.stack.forEach((testOrGroup) => {
if (testOrGroup instanceof Test) {
callback(testOrGroup)
}
})

this.#configureTestCallbacks.push(callback)
return this
}
Expand All @@ -96,6 +102,12 @@ export class Suite<Context extends Record<any, any>> extends Macroable {
* Tap into each group and configure it
*/
onGroup(callback: (group: Group<Context>) => void): this {
this.stack.forEach((testOrGroup) => {
if (testOrGroup instanceof Group) {
callback(testOrGroup)
}
})

this.#configureGroupCallbacks.push(callback)
return this
}
Expand Down
28 changes: 28 additions & 0 deletions test/runner/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
59 changes: 59 additions & 0 deletions test/suite/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestContext>('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<TestContext>('sample suite', new Emitter(), refiner)
const group = new Group<TestContext>('sample group', emitter, refiner)
const testInstance = new Test('2 + 2 = 4', new TestContext(), emitter, refiner)

const group1 = new Group<TestContext>('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'])
})
})

0 comments on commit 7791d2a

Please sign in to comment.