Skip to content

Commit 61623bc

Browse files
committed
Some cleanup after #1114
This primarily renames a bunch of stuff to more clearly differentiate between the three types of hooks that exists now, namely: - run hooks (BeforeAll, AfterAll) - case hooks (Before, After) - step hooks (BeforeStep, AfterStep) The naming convention comes from cucumber-js [1]. Additionally, the order of AfterAll is reversed before execution. Lastly, documentation has been updated. [1] https://github.com/cucumber/cucumber-js/blob/v10.0.0/src/support_code_library_builder/index.ts#L72-L77
1 parent c5f4c8a commit 61623bc

File tree

10 files changed

+200
-149
lines changed

10 files changed

+200
-149
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## Unreleased
6+
7+
- Add `BeforeAll(..)` and `AfterAll(..)` hooks, fixes [#758](https://github.com/badeball/cypress-cucumber-preprocessor/issues/758).
8+
59
## v19.0.1
610

711
- Fix type members to account for scenario hook names, fixes [#1113](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1113).

docs/cucumber-basics.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Skipped steps](#skipped-steps)
1111
- [Nested steps](#nested-steps)
1212
- [Hooks](#hooks)
13+
- [Run hooks](#run-hooks)
1314
- [Scenario hooks](#scenario-hooks)
1415
- [Step hooks](#step-hooks)
1516
- [Named hooks](#named-hooks)
@@ -141,7 +142,19 @@ When("I fill in the entire form", function () {
141142

142143
# Hooks
143144

144-
There are two types of hooks, scenario hooks and step hooks, each explained below.
145+
There are three types of hooks, run hooks, scenario hooks and step hooks, each explained below.
146+
147+
## Run hooks
148+
149+
`BeforeAll()` and `AfterAll()` are identical to Cypress' `before()` and `after()`.
150+
151+
```ts
152+
import { BeforeAll } from "@badeball/cypress-cucumber-preprocessor";
153+
154+
BeforeAll(function () {
155+
// This hook will be executed once at the beginnig of a feature.
156+
});
157+
```
145158

146159
## Scenario hooks
147160

features/hooks_ordering.feature

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ Feature: hooks ordering
1414
- AfterAll
1515
- after
1616

17-
@foo
1817
Scenario: with all hooks incrementing a counter
1918
Given a file named "cypress/e2e/a.feature" with:
2019
"""
2120
Feature: a feature
2221
Background:
2322
Given a background step
24-
@foo
2523
Scenario: a scenario
2624
Given an ordinary step
2725
"""
@@ -41,7 +39,7 @@ Feature: hooks ordering
4139
counter = 0;
4240
})
4341
BeforeAll(() => {
44-
expect(counter++, "Expect BeforeAll() to be called after beforeEach()").to.equal(0)
42+
expect(counter++, "Expect BeforeAll() to be called after before()").to.equal(0)
4543
})
4644
beforeEach(function() {
4745
expect(counter++, "Expected beforeEach() to be called after before()").to.equal(1)

lib/browser-runtime.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import DataTable from "./data_table";
1919
import {
2020
assignRegistry,
2121
freeRegistry,
22-
IHook,
22+
ICaseHook,
2323
MissingDefinitionError,
2424
Registry,
2525
} from "./registry";
@@ -63,11 +63,7 @@ import { runStepWithLogGroup } from "./helpers/cypress";
6363

6464
import { getTags } from "./helpers/environment";
6565

66-
import {
67-
IHookOptions,
68-
IHookParameter,
69-
IStepHookParameter,
70-
} from "./public-member-types";
66+
import { ICaseHookParameter, IStepHookParameter } from "./public-member-types";
7167

7268
type Node = ReturnType<typeof parse>;
7369

@@ -98,7 +94,7 @@ const sourceReference: messages.SourceReference = {
9894
};
9995

10096
interface IStep {
101-
hook?: IHook;
97+
hook?: ICaseHook;
10298
pickleStep?: messages.PickleStep;
10399
}
104100

@@ -283,7 +279,10 @@ function getTestStepId(options: {
283279
function createStepDescription({
284280
name,
285281
tags,
286-
}: IHookOptions): string | undefined {
282+
}: {
283+
name?: string;
284+
tags?: string;
285+
}): string | undefined {
287286
if (name == null && tags == null) {
288287
return;
289288
} else if (name == null) {
@@ -310,7 +309,7 @@ function createFeature(context: CompositionContext, feature: messages.Feature) {
310309
});
311310

312311
after(function () {
313-
afterAllHandler.call(this, context);
312+
afterHandler.call(this, context);
314313
});
315314

316315
afterEach(function () {
@@ -488,14 +487,14 @@ function createPickle(context: CompositionContext, pickle: messages.Pickle) {
488487
return cy.wrap(start, { log: false });
489488
})
490489
.then((start) => {
491-
const options: IHookParameter = {
490+
const options: ICaseHookParameter = {
492491
pickle,
493492
gherkinDocument,
494493
testCaseStartedId,
495494
};
496495

497496
runStepWithLogGroup({
498-
fn: () => registry.runHook(this, hook, options),
497+
fn: () => registry.runCaseHook(this, hook, options),
499498
keyword: hook.keyword,
500499
text: createStepDescription(hook),
501500
});
@@ -775,16 +774,16 @@ function beforeHandler(this: Mocha.Context, context: CompositionContext) {
775774
"Missing preprocessor event handlers (this usally means you've not invoked `addCucumberPreprocessorPlugin()` or not returned the config object in `setupNodeEvents()`)"
776775
);
777776
}
778-
// Handle BeforeAll hook
777+
779778
const { registry } = context;
780-
const beforeAllHooks = registry.resolveBeforeAllHooks();
781-
for (const beforeAllHook of beforeAllHooks) {
782-
const hook = beforeAllHook;
779+
780+
for (const hook of registry.resolveBeforeAllHooks()) {
783781
runStepWithLogGroup({
784-
fn: () => registry.runHook(this, hook),
782+
fn: () => registry.runRunHook(this, hook),
785783
keyword: "BeforeAll",
786784
});
787785
}
786+
788787
taskSpecEnvelopes(context);
789788
}
790789

@@ -1039,13 +1038,12 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
10391038
});
10401039
}
10411040

1042-
function afterAllHandler(this: Mocha.Context, context: CompositionContext) {
1041+
function afterHandler(this: Mocha.Context, context: CompositionContext) {
10431042
const { registry } = context;
1044-
const afterAllHooks = registry.resolveAfterAllHooks();
1045-
for (const afterAllHook of afterAllHooks) {
1046-
const hook = afterAllHook;
1043+
1044+
for (const hook of registry.resolveAfterAllHooks()) {
10471045
runStepWithLogGroup({
1048-
fn: () => registry.runHook(this, hook),
1046+
fn: () => registry.runRunHook(this, hook),
10491047
keyword: "AfterAll",
10501048
});
10511049
}
@@ -1103,7 +1101,8 @@ export default function createTests(
11031101
const tags = collectTagNames(pickle.tags);
11041102
const beforeHooks = registry.resolveBeforeHooks(tags);
11051103
const afterHooks = registry.resolveAfterHooks(tags);
1106-
const hooksToStep = (hook: IHook): messages.TestStep => {
1104+
1105+
const hooksToStep = (hook: ICaseHook): messages.TestStep => {
11071106
return {
11081107
id: createTestStepId({
11091108
testStepIds,
@@ -1168,7 +1167,7 @@ export default function createTests(
11681167
});
11691168
}
11701169

1171-
for (const hook of registry.hooks) {
1170+
for (const hook of registry.caseHooks) {
11721171
specEnvelopes.push({
11731172
hook: {
11741173
id: hook.id,

lib/entrypoint-browser.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ import DataTable from "./data_table";
2626
import { getRegistry } from "./registry";
2727

2828
import {
29-
IHookBody,
30-
IStepHookBody,
29+
ICaseHookBody,
30+
ICaseHookOptions,
3131
IParameterTypeDefinition,
32+
IRunHookBody,
3233
IStepDefinitionBody,
33-
IHookOptions,
34+
IStepHookBody,
35+
IStepHookOptions,
3436
} from "./public-member-types";
3537

3638
import {
@@ -67,11 +69,11 @@ function defineParameterType<T, C extends Mocha.Context>(
6769
getRegistry().defineParameterType(options);
6870
}
6971

70-
function defineBefore(options: IHookOptions, fn: IHookBody): void;
71-
function defineBefore(fn: IHookBody): void;
72+
function defineBefore(options: ICaseHookOptions, fn: ICaseHookBody): void;
73+
function defineBefore(fn: ICaseHookBody): void;
7274
function defineBefore(
73-
optionsOrFn: IHookBody | IHookOptions,
74-
maybeFn?: IHookBody
75+
optionsOrFn: ICaseHookBody | ICaseHookOptions,
76+
maybeFn?: ICaseHookBody
7577
) {
7678
if (typeof optionsOrFn === "function") {
7779
getRegistry().defineBefore({}, optionsOrFn);
@@ -82,11 +84,11 @@ function defineBefore(
8284
}
8385
}
8486

85-
function defineAfter(options: IHookOptions, fn: IHookBody): void;
86-
function defineAfter(fn: IHookBody): void;
87+
function defineAfter(options: ICaseHookOptions, fn: ICaseHookBody): void;
88+
function defineAfter(fn: ICaseHookBody): void;
8789
function defineAfter(
88-
optionsOrFn: IHookBody | IHookOptions,
89-
maybeFn?: IHookBody
90+
optionsOrFn: ICaseHookBody | ICaseHookOptions,
91+
maybeFn?: ICaseHookBody
9092
) {
9193
if (typeof optionsOrFn === "function") {
9294
getRegistry().defineAfter({}, optionsOrFn);
@@ -97,10 +99,10 @@ function defineAfter(
9799
}
98100
}
99101

100-
function defineBeforeStep(options: IHookOptions, fn: IStepHookBody): void;
102+
function defineBeforeStep(options: IStepHookOptions, fn: IStepHookBody): void;
101103
function defineBeforeStep(fn: IStepHookBody): void;
102104
function defineBeforeStep(
103-
optionsOrFn: IStepHookBody | IHookOptions,
105+
optionsOrFn: IStepHookBody | IStepHookOptions,
104106
maybeFn?: IStepHookBody
105107
) {
106108
if (typeof optionsOrFn === "function") {
@@ -112,10 +114,10 @@ function defineBeforeStep(
112114
}
113115
}
114116

115-
function defineAfterStep(options: IHookOptions, fn: IStepHookBody): void;
117+
function defineAfterStep(options: IStepHookOptions, fn: IStepHookBody): void;
116118
function defineAfterStep(fn: IStepHookBody): void;
117119
function defineAfterStep(
118-
optionsOrFn: IStepHookBody | IHookOptions,
120+
optionsOrFn: IStepHookBody | IStepHookOptions,
119121
maybeFn?: IStepHookBody
120122
) {
121123
if (typeof optionsOrFn === "function") {
@@ -127,15 +129,15 @@ function defineAfterStep(
127129
}
128130
}
129131

130-
function defineBeforeAll(fn: IHookBody) {
132+
function defineBeforeAll(fn: IRunHookBody) {
131133
if (typeof fn === "function") {
132134
getRegistry().defineBeforeAll(fn);
133135
} else {
134136
throw new Error("Unexpected argument for BeforeAll hook");
135137
}
136138
}
137139

138-
function defineAfterAll(fn: IHookBody) {
140+
function defineAfterAll(fn: IRunHookBody) {
139141
if (typeof fn === "function") {
140142
getRegistry().defineAfterAll(fn);
141143
} else {

lib/entrypoint-node.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import DataTable from "./data_table";
22

33
import {
4-
IHookBody,
5-
IStepHookBody,
4+
ICaseHookBody,
5+
ICaseHookOptions,
66
IParameterTypeDefinition,
7+
IRunHookBody,
78
IStepDefinitionBody,
8-
IHookOptions,
9+
IStepHookBody,
10+
IStepHookOptions,
911
} from "./public-member-types";
1012

1113
export {
@@ -75,44 +77,44 @@ export function attach(data: string | ArrayBuffer, mediaType?: string) {
7577
throw createUnimplemented();
7678
}
7779

78-
export function Before(options: IHookOptions, fn: IHookBody): void;
79-
export function Before(fn: IHookBody): void;
80+
export function Before(options: ICaseHookOptions, fn: ICaseHookBody): void;
81+
export function Before(fn: ICaseHookBody): void;
8082
export function Before(
8183
// eslint-disable-next-line @typescript-eslint/no-unused-vars
82-
optionsOrFn: IHookBody | IHookOptions,
84+
optionsOrFn: ICaseHookBody | ICaseHookOptions,
8385
// eslint-disable-next-line @typescript-eslint/no-unused-vars
84-
maybeFn?: IHookBody
86+
maybeFn?: ICaseHookBody
8587
) {
8688
throw createUnimplemented();
8789
}
8890

89-
export function After(options: IHookOptions, fn: IHookBody): void;
90-
export function After(fn: IHookBody): void;
91+
export function After(options: ICaseHookOptions, fn: ICaseHookBody): void;
92+
export function After(fn: ICaseHookBody): void;
9193
export function After(
9294
// eslint-disable-next-line @typescript-eslint/no-unused-vars
93-
optionsOrFn: IHookBody | IHookOptions,
95+
optionsOrFn: ICaseHookBody | ICaseHookOptions,
9496
// eslint-disable-next-line @typescript-eslint/no-unused-vars
95-
maybeFn?: IHookBody
97+
maybeFn?: ICaseHookBody
9698
) {
9799
throw createUnimplemented();
98100
}
99101

100-
export function BeforeStep(options: IHookOptions, fn: IStepHookBody): void;
102+
export function BeforeStep(options: IStepHookOptions, fn: IStepHookBody): void;
101103
export function BeforeStep(fn: IStepHookBody): void;
102104
export function BeforeStep(
103105
// eslint-disable-next-line @typescript-eslint/no-unused-vars
104-
optionsOrFn: IStepHookBody | IHookOptions,
106+
optionsOrFn: IStepHookBody | IStepHookOptions,
105107
// eslint-disable-next-line @typescript-eslint/no-unused-vars
106108
maybeFn?: IStepHookBody
107109
) {
108110
throw createUnimplemented();
109111
}
110112

111-
export function AfterStep(options: IHookOptions, fn: IStepHookBody): void;
113+
export function AfterStep(options: IStepHookOptions, fn: IStepHookBody): void;
112114
export function AfterStep(fn: IStepHookBody): void;
113115
export function AfterStep(
114116
// eslint-disable-next-line @typescript-eslint/no-unused-vars
115-
optionsOrFn: IStepHookBody | IHookOptions,
117+
optionsOrFn: IStepHookBody | IStepHookOptions,
116118
// eslint-disable-next-line @typescript-eslint/no-unused-vars
117119
maybeFn?: IStepHookBody
118120
) {
@@ -121,14 +123,14 @@ export function AfterStep(
121123

122124
export function BeforeAll(
123125
// eslint-disable-next-line @typescript-eslint/no-unused-vars
124-
fn: IStepHookBody
126+
fn: IRunHookBody
125127
) {
126128
throw createUnimplemented();
127129
}
128130

129131
export function AfterAll(
130132
// eslint-disable-next-line @typescript-eslint/no-unused-vars
131-
fn: IHookBody
133+
fn: IRunHookBody
132134
) {
133135
throw createUnimplemented();
134136
}

0 commit comments

Comments
 (0)