Skip to content

Commit

Permalink
refactor: make context configurable at suite level as well
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 10, 2022
1 parent 304d1f7 commit 0a1210c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
15 changes: 11 additions & 4 deletions src/Contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,21 @@ export type GroupHooksHandler<Context> = (
/**
* The cleanup function for suite hooks
*/
export type SuiteHooksCleanupHandler = (error: null | any, suite: Suite) => Promise<any> | any
export type SuiteHooksCleanupHandler<Context> = (
error: null | any,
suite: Suite<Context>
) => Promise<any> | any

/**
* The function that can be registered as a suite hook
*/
export type SuiteHooksHandler = (
suite: Suite
) => Promise<any> | any | SuiteHooksCleanupHandler | Promise<SuiteHooksCleanupHandler>
export type SuiteHooksHandler<Context> = (
suite: Suite<Context>
) =>
| Promise<any>
| any
| SuiteHooksCleanupHandler<Context>
| Promise<SuiteHooksCleanupHandler<Context>>

/**
* The cleanup function for runner hooks
Expand Down
4 changes: 2 additions & 2 deletions src/Runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Runner extends Macroable {
/**
* A collection of suites
*/
public suites: Suite[] = []
public suites: Suite<any>[] = []

/**
* Registered tests reporter
Expand Down Expand Up @@ -158,7 +158,7 @@ export class Runner extends Macroable {
/**
* Add a suite to the runner
*/
public add(suite: Suite): this {
public add(suite: Suite<any>): this {
this.suites.push(suite)
return this
}
Expand Down
2 changes: 1 addition & 1 deletion src/Suite/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class SuiteRunner {
*/
private hasError: boolean = false

constructor(private suite: Suite, private hooks: Hooks, private emitter: Emitter) {}
constructor(private suite: Suite<any>, private hooks: Hooks, private emitter: Emitter) {}

/**
* Notify the reporter about the suite start
Expand Down
18 changes: 9 additions & 9 deletions src/Suite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { SuiteHooksHandler } from '../Contracts'
* // Runs all the tests inside the registered group
* await suite.exec()
*/
export class Suite extends Macroable {
export class Suite<Context> extends Macroable {
public static macros = {}
public static getters = {}

Expand All @@ -49,13 +49,13 @@ export class Suite extends Macroable {
/**
* Callbacks to invoke on each test and group
*/
private configureTestCallbacks: ((test: Test<any, any>) => void)[] = []
private configureGroupCallbacks: ((group: Group<any>) => void)[] = []
private configureTestCallbacks: ((test: Test<Context, any>) => void)[] = []
private configureGroupCallbacks: ((group: Group<Context>) => void)[] = []

/**
* A collection of tests and groups both
*/
public stack: (Test<any, any> | Group<any>)[] = []
public stack: (Test<Context, any> | Group<Context>)[] = []

constructor(public name: string, private emitter: Emitter) {
super()
Expand All @@ -64,7 +64,7 @@ export class Suite extends Macroable {
/**
* Add a test or a group to the execution stack
*/
public add(testOrGroup: Test<any, any> | Group<any>): this {
public add(testOrGroup: Test<Context, any> | Group<Context>): this {
if (testOrGroup instanceof Group) {
this.configureGroupCallbacks.forEach((callback) => callback(testOrGroup))
}
Expand All @@ -80,31 +80,31 @@ export class Suite extends Macroable {
/**
* Tap into each test and configure it
*/
public onTest(callback: (test: Test<any, any>) => void): this {
public onTest(callback: (test: Test<Context, any>) => void): this {
this.configureTestCallbacks.push(callback)
return this
}

/**
* Tap into each group and configure it
*/
public onGroup(callback: (group: Group<any>) => void): this {
public onGroup(callback: (group: Group<Context>) => void): this {
this.configureGroupCallbacks.push(callback)
return this
}

/**
* Register a test setup function
*/
public setup(handler: SuiteHooksHandler): this {
public setup(handler: SuiteHooksHandler<Context>): this {
this.hooks.add('setup', handler)
return this
}

/**
* Register a test teardown function
*/
public teardown(handler: SuiteHooksHandler): this {
public teardown(handler: SuiteHooksHandler<Context>): this {
this.hooks.add('teardown', handler)
return this
}
Expand Down

0 comments on commit 0a1210c

Please sign in to comment.