Skip to content

Commit 37f83b7

Browse files
author
netoctone
committed
feat(ts): improve hook types (Background/Before/After/BeforeSuite/AfterSuite)
also, introduce hook aliases global.GherkinBefore and global.GherkinAfter because gherkin hooks types are incompatible with mocha global.Before and global.After
1 parent b90f04a commit 37f83b7

8 files changed

Lines changed: 98 additions & 16 deletions

File tree

‎lib/container.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,8 @@ async function loadGherkinStepsAsync(paths) {
778778
// Import BDD module to access step file tracking functions and step DSL
779779
const bddModule = await import('./mocha/bdd.js')
780780

781-
global.Before = fn => event.dispatcher.on(event.test.started, fn)
782-
global.After = fn => event.dispatcher.on(event.test.finished, fn)
781+
global.Before = global.GherkinBefore = fn => event.dispatcher.on(event.test.started, fn)
782+
global.After = global.GherkinAfter = fn => event.dispatcher.on(event.test.finished, fn)
783783
global.Fail = fn => event.dispatcher.on(event.test.failed, fn)
784784

785785
// Scope-inject Given/When/Then/And while loading step files so they work
@@ -818,7 +818,9 @@ async function loadGherkinStepsAsync(paths) {
818818
}
819819

820820
delete global.Before
821+
delete global.GherkinBefore
821822
delete global.After
823+
delete global.GherkinAfter
822824
delete global.Fail
823825
if (injectStepDsl) {
824826
delete global.Given

‎lib/mocha/bdd.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,33 @@ const Before = fn => {
145145
event.dispatcher.on(event.test.started, fn)
146146
}
147147

148+
const GherkinBefore = Before
149+
148150
const After = fn => {
149151
event.dispatcher.on(event.test.finished, fn)
150152
}
151153

154+
const GherkinAfter = After
155+
152156
const Fail = fn => {
153157
event.dispatcher.on(event.test.failed, fn)
154158
}
155159

156-
export { Given, When, Then, And, Before, After, Fail, matchStep, getSteps, clearSteps, defineParameterType }
160+
export {
161+
Given,
162+
When,
163+
Then,
164+
And,
165+
Before,
166+
GherkinBefore,
167+
After,
168+
GherkinAfter,
169+
Fail,
170+
matchStep,
171+
getSteps,
172+
clearSteps,
173+
defineParameterType
174+
}
157175

158176
export default {
159177
Given: addStep,

‎test/data/sandbox/codecept.workers-gherkin.conf.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const config = {
1212
},
1313
gherkin: {
1414
features: './workers-gherkin/*.feature',
15-
steps: ['./workers-gherkin/steps.js'],
15+
steps: ['./workers-gherkin/steps.js', './workers-gherkin/hooks.js'],
1616
},
1717
include: {},
1818
bootstrap: false,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let beforeCounter = 0;
2+
let afterCounter = 0;
3+
GherkinBefore((test) => {
4+
test.addNote('before', `beforeCounter=${++beforeCounter}`);
5+
});
6+
GherkinAfter((test) => {
7+
test.addNote('after', `afterCounter=${++afterCounter}`);
8+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: Worker gherkin multi scenario
2+
3+
Scenario Outline: runs once @worker_gherkin_multi
4+
Given I open a browser on a site
5+
6+
Examples:
7+
| id |
8+
| 1 |
9+
| 2 |

‎test/unit/worker_test.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,31 @@ describe('Workers', function () {
444444
})
445445
})
446446

447+
it('should run GherkinBefore and GherkinAfter hooks', done => {
448+
const workerConfig = {
449+
by: 'test',
450+
testConfig: './test/data/sandbox/codecept.workers-gherkin.conf.js',
451+
options: {
452+
grep: '@worker_gherkin_multi',
453+
},
454+
}
455+
456+
let passedCount = 0
457+
const workers = new Workers(1, workerConfig)
458+
459+
workers.on(event.test.passed, (test) => {
460+
passedCount += 1
461+
expect(test.notes[0].type).to.equal('before')
462+
expect(test.notes[0].text).to.equal(`beforeCounter=${passedCount}`)
463+
expect(test.notes[1].type).to.equal('after')
464+
expect(test.notes[1].text).to.equal(`afterCounter=${passedCount}`)
465+
})
466+
467+
workers.run()
468+
469+
workers.on(event.all.result, result => {
470+
done()
471+
})
472+
})
473+
447474
})

‎typings/index.d.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,12 @@ declare namespace CodeceptJS {
468468
// Could get extended by user generated typings
469469
interface Methods extends ActorStatic {}
470470
interface I {}
471-
interface IHook {}
471+
interface IHook {
472+
(callback: HookCallback): HookConfig
473+
}
474+
interface IHookSuite {
475+
(callback: HookSuiteCallback): HookConfig
476+
}
472477
interface IScenario {}
473478
interface IFeature {
474479
(title: string, opts?: { [key: string]: any }): FeatureConfig
@@ -508,8 +513,22 @@ declare namespace CodeceptJS {
508513

509514
type StringOrSecret = string | CodeceptJS.Secret
510515

516+
interface GherkinCallback {
517+
(test: Test): void | Promise<void>
518+
}
519+
interface GherkinEventEmitter {}
520+
interface GherkinHook {
521+
(callback: GherkinCallback): GherkinEventEmitter
522+
}
511523
interface HookCallback {
512-
(args: SupportObject): void | Promise<void>
524+
// `suite` has type Mocha.Suite if hook (Background/Before/After) is called before `Feature()`
525+
// `suite` has type CodeceptJS.Suite if hook is called after `Feature()`
526+
(args: SupportObject & { suite: Mocha.Suite; test: Test }): void | Promise<void>
527+
}
528+
interface HookSuiteCallback {
529+
// `suite` has type Mocha.Suite if hook (BeforeSuite/AfterSuite) is called before `Feature()`
530+
// `suite` has type CodeceptJS.Suite if hook is called after `Feature()`
531+
(args: SupportObject & { suite: Mocha.Suite }): void | Promise<void>
513532
}
514533
interface Scenario extends IScenario {
515534
only: IScenario
@@ -530,9 +549,6 @@ declare namespace CodeceptJS {
530549
(title: string, callback?: HookCallback): ScenarioConfig
531550
(title: string, opts: { [key: string]: any }, callback: HookCallback): ScenarioConfig
532551
}
533-
interface IHook {
534-
(callback: HookCallback): HookConfig
535-
}
536552

537553
interface Globals {
538554
codeceptjs: typeof codeceptjs
@@ -580,11 +596,13 @@ declare function xData(data: any): CodeceptJS.IData
580596
declare function DefineParameterType(options: CodeceptJS.IParameterTypeDefinition<any>): void
581597

582598
// Hooks
583-
declare const BeforeSuite: CodeceptJS.IHook
584-
declare const AfterSuite: CodeceptJS.IHook
599+
declare const BeforeSuite: CodeceptJS.IHookSuite
600+
declare const AfterSuite: CodeceptJS.IHookSuite
585601
declare const Background: CodeceptJS.IHook
586602
declare const Before: CodeceptJS.IHook
587603
declare const After: CodeceptJS.IHook
604+
declare const GherkinBefore: CodeceptJS.GherkinHook
605+
declare const GherkinAfter: CodeceptJS.GherkinHook
588606

589607
// Plugins
590608
declare const __: any

‎typings/tests/global-variables.types.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ expectType<CodeceptJS.ScenarioConfig>(
5454
expectType<CodeceptJS.ScenarioConfig>(
5555
Scenario('scenario', args => {
5656
// @ts-ignore
57-
expectType<CodeceptJS.SupportObject>(args)
57+
expectType<CodeceptJS.SupportObject & { suite: Mocha.Suite; test: CodeceptJS.Test }>(args)
5858
// @ts-ignore
5959
expectType<CodeceptJS.I>(args.I) // $ExpectType I
6060
}),
@@ -72,7 +72,7 @@ expectType<CodeceptJS.ScenarioConfig>(
7272
expectType<CodeceptJS.HookConfig>(
7373
Before(args => {
7474
// @ts-ignore
75-
expectType<CodeceptJS.SupportObject>(args)
75+
expectType<CodeceptJS.SupportObject & { suite: Mocha.Suite; test: CodeceptJS.Test }>(args)
7676
// @ts-ignore
7777
expectType<CodeceptJS.I>(args.I)
7878
}),
@@ -82,7 +82,7 @@ expectType<CodeceptJS.HookConfig>(
8282
expectType<CodeceptJS.HookConfig>(
8383
BeforeSuite(args => {
8484
// @ts-ignore
85-
expectType<CodeceptJS.SupportObject>(args)
85+
expectType<CodeceptJS.SupportObject & { suite: Mocha.Suite }>(args)
8686
// @ts-ignore
8787
expectType<CodeceptJS.I>(args.I)
8888
}),
@@ -92,7 +92,7 @@ expectType<CodeceptJS.HookConfig>(
9292
expectType<CodeceptJS.HookConfig>(
9393
After(args => {
9494
// @ts-ignore
95-
expectType<CodeceptJS.SupportObject>(args)
95+
expectType<CodeceptJS.SupportObject & { suite: Mocha.Suite; test: CodeceptJS.Test }>(args)
9696
// @ts-ignore
9797
expectType<CodeceptJS.I>(args.I)
9898
}),
@@ -102,7 +102,7 @@ expectType<CodeceptJS.HookConfig>(
102102
expectType<CodeceptJS.HookConfig>(
103103
AfterSuite(args => {
104104
// @ts-ignore
105-
expectType<CodeceptJS.SupportObject>(args)
105+
expectType<CodeceptJS.SupportObject & { suite: Mocha.Suite }>(args)
106106
// @ts-ignore
107107
expectType<CodeceptJS.I>(args.I)
108108
}),

0 commit comments

Comments
 (0)