Skip to content

Commit 186e2b6

Browse files
committed
Addressing comments
1 parent eaebfe5 commit 186e2b6

File tree

8 files changed

+63
-110
lines changed

8 files changed

+63
-110
lines changed

features/hooks_ordering.feature

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Feature: hooks ordering
22

33
Hooks should be executed in the following order:
44
- before
5+
- BeforeAll
56
- beforeEach
67
- Before
78
- Background steps
@@ -10,14 +11,17 @@ Feature: hooks ordering
1011
- AfterStep (in reverse order)
1112
- After
1213
- afterEach
14+
- AfterAll
1315
- after
1416

17+
@foo
1518
Scenario: with all hooks incrementing a counter
1619
Given a file named "cypress/e2e/a.feature" with:
1720
"""
1821
Feature: a feature
1922
Background:
2023
Given a background step
24+
@foo
2125
Scenario: a scenario
2226
Given an ordinary step
2327
"""
@@ -28,50 +32,58 @@ Feature: hooks ordering
2832
Before,
2933
After,
3034
BeforeStep,
31-
AfterStep
35+
AfterStep,
36+
BeforeAll,
37+
AfterAll
3238
} = require("@badeball/cypress-cucumber-preprocessor")
3339
let counter;
3440
before(function() {
3541
counter = 0;
3642
})
43+
BeforeAll(() => {
44+
expect(counter++, "Expect BeforeAll() to be called after beforeEach()").to.equal(0)
45+
})
3746
beforeEach(function() {
38-
expect(counter++, "Expected beforeEach() to be called after before()").to.equal(0)
47+
expect(counter++, "Expected beforeEach() to be called after before()").to.equal(1)
3948
})
4049
Before(function() {
41-
expect(counter++, "Expected Before() to be called after beforeEach()").to.equal(1)
50+
expect(counter++, "Expected Before() to be called after beforeEach()").to.equal(2)
4251
})
4352
Given("a background step", function() {
44-
expect(counter++, "Expected a background step to be called after Before()").to.equal(2)
53+
expect(counter++, "Expected a background step to be called after Before()").to.equal(3)
4554
})
4655
BeforeStep(function ({ pickleStep }) {
4756
if (pickleStep.text === "an ordinary step") {
48-
expect(counter++, "Expected BeforeStep() to be called before ordinary steps").to.equal(3)
57+
expect(counter++, "Expected BeforeStep() to be called before ordinary steps").to.equal(4)
4958
}
5059
})
5160
Given("an ordinary step", function() {
52-
expect(counter++, "Expected an ordinary step to be called after a background step").to.equal(4)
61+
expect(counter++, "Expected an ordinary step to be called after a background step").to.equal(5)
5362
})
5463
AfterStep(function ({ pickleStep }) {
5564
if (pickleStep.text === "an ordinary step") {
56-
expect(counter++, "Expected AfterStep() to be called after ordinary steps").to.equal(6)
65+
expect(counter++, "Expected AfterStep() to be called after ordinary steps").to.equal(7)
5766
}
5867
})
5968
AfterStep(function ({ pickleStep }) {
6069
if (pickleStep.text === "an ordinary step") {
61-
expect(counter++, "Expected AfterStep() to be called after ordinary steps").to.equal(5)
70+
expect(counter++, "Expected AfterStep() to be called after ordinary steps").to.equal(6)
6271
}
6372
})
6473
After(function() {
65-
expect(counter++, "Expected After() to be called in reverse order of definition").to.equal(8)
74+
expect(counter++, "Expected After() to be called in reverse order of definition").to.equal(9)
6675
})
6776
After(function() {
68-
expect(counter++, "Expected After() to be called after ordinary steps").to.equal(7)
77+
expect(counter++, "Expected After() to be called after ordinary steps").to.equal(8)
6978
})
7079
afterEach(function() {
71-
expect(counter++, "Expected afterEach() to be called after After()").to.equal(9)
80+
expect(counter++, "Expected afterEach() to be called after After()").to.equal(10)
81+
})
82+
AfterAll(function() {
83+
expect(counter++, "Expected AfterAll() to be called after afterEach()").to.equal(11)
7284
})
7385
after(function() {
74-
expect(counter++, "Expected after() to be called after afterEach()").to.equal(10)
86+
expect(counter++, "Expected after() to be called after AfterAll()").to.equal(12)
7587
})
7688
"""
7789
When I run cypress

features/issues/758.feature

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/browser-runtime.ts

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import DataTable from "./data_table";
1818

1919
import {
2020
assignRegistry,
21-
freeRegistry, getRegistry,
21+
freeRegistry,
2222
IHook,
2323
MissingDefinitionError,
2424
Registry,
@@ -303,7 +303,6 @@ function createFeature(context: CompositionContext, feature: messages.Feature) {
303303
describe(feature.name || "<unamed feature>", () => {
304304
before(function () {
305305
beforeHandler.call(this, context);
306-
beforeAllHandler.call(this, context);
307306
});
308307

309308
beforeEach(function () {
@@ -770,39 +769,23 @@ function shouldSkipPickle(testFilter: Node, pickle: messages.Pickle) {
770769
return !testFilter.evaluate(tags) || tags.includes("@skip");
771770
}
772771

773-
function beforeHandler(context: CompositionContext) {
772+
function beforeHandler(this: Mocha.Context, context: CompositionContext) {
774773
if (!retrieveInternalSuiteProperties()?.isEventHandlersAttached) {
775774
fail(
776775
"Missing preprocessor event handlers (this usally means you've not invoked `addCucumberPreprocessorPlugin()` or not returned the config object in `setupNodeEvents()`)"
777776
);
778777
}
779-
780-
taskSpecEnvelopes(context);
781-
}
782-
783-
function beforeAllHandler(this: Mocha.Context, context: CompositionContext) {
778+
// Handle BeforeAll hook
784779
const { registry } = context;
785-
let beforeAllHooks = registry.resolveBeforeAllHooks();
786-
for(const beforeAllHook of beforeAllHooks) {
787-
if (beforeAllHook) {
788-
const hook = beforeAllHook;
789-
cy.then(() => {
790-
const start = createTimestamp();
791-
return cy.wrap(start, { log: false });
792-
})
793-
.then((start) => {
794-
runStepWithLogGroup({
795-
fn: () => registry.runHook(this, hook),
796-
keyword: "BeforeAll"
797-
});
798-
799-
return cy.wrap(start, { log: false });
800-
})
801-
.then((start) => {
802-
const end = createTimestamp();
803-
});
804-
}
805-
}
780+
const beforeAllHooks = registry.resolveBeforeAllHooks();
781+
for (const beforeAllHook of beforeAllHooks) {
782+
const hook = beforeAllHook;
783+
runStepWithLogGroup({
784+
fn: () => registry.runHook(this, hook),
785+
keyword: "BeforeAll",
786+
});
787+
}
788+
taskSpecEnvelopes(context);
806789
}
807790

808791
function beforeEachHandler(context: CompositionContext) {
@@ -1058,26 +1041,13 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
10581041

10591042
function afterAllHandler(this: Mocha.Context, context: CompositionContext) {
10601043
const { registry } = context;
1061-
let afterAllHooks = registry.resolveAfterAllHooks();
1062-
for(const afterAllHook of afterAllHooks) {
1063-
if (afterAllHook) {
1064-
const hook = afterAllHook;
1065-
cy.then(() => {
1066-
const start = createTimestamp();
1067-
return cy.wrap(start, { log: false });
1068-
})
1069-
.then((start) => {
1070-
runStepWithLogGroup({
1071-
fn: () => registry.runHook(this, hook),
1072-
keyword: "AfterAll"
1073-
});
1074-
1075-
return cy.wrap(start, { log: false });
1076-
})
1077-
.then((start) => {
1078-
const end = createTimestamp();
1079-
});
1080-
}
1044+
const afterAllHooks = registry.resolveAfterAllHooks();
1045+
for (const afterAllHook of afterAllHooks) {
1046+
const hook = afterAllHook;
1047+
runStepWithLogGroup({
1048+
fn: () => registry.runHook(this, hook),
1049+
keyword: "AfterAll",
1050+
});
10811051
}
10821052
}
10831053
export default function createTests(

lib/entrypoint-browser.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,17 @@ function defineAfterStep(
127127
}
128128
}
129129

130-
function defineBeforeAll(fn: IHookBody): void;
131-
function defineBeforeAll(
132-
maybeFn?: IHookBody
133-
) {
134-
if (typeof maybeFn === "function") {
135-
getRegistry().defineBeforeAll(maybeFn);
130+
function defineBeforeAll(fn: IHookBody) {
131+
if (typeof fn === "function") {
132+
getRegistry().defineBeforeAll(fn);
136133
} else {
137134
throw new Error("Unexpected argument for BeforeAll hook");
138135
}
139136
}
140137

141-
function defineAfterAll(fn: IHookBody): void;
142-
function defineAfterAll(
143-
maybeFn?: IHookBody
144-
) {
145-
if (typeof maybeFn === "function") {
146-
getRegistry().defineAfterAll(maybeFn);
138+
function defineAfterAll(fn: IHookBody) {
139+
if (typeof fn === "function") {
140+
getRegistry().defineAfterAll(fn);
147141
} else {
148142
throw new Error("Unexpected argument for AfterAll hook");
149143
}
@@ -231,7 +225,7 @@ export {
231225
defineBeforeStep as BeforeStep,
232226
defineAfterStep as AfterStep,
233227
defineBeforeAll as BeforeAll,
234-
defineAfterAll as AfterAll
228+
defineAfterAll as AfterAll,
235229
};
236230

237231
/**

lib/entrypoint-node.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,16 @@ export function AfterStep(
119119
throw createUnimplemented();
120120
}
121121

122-
export function BeforeAll(fn: IStepHookBody): void;
123122
export function BeforeAll(
124123
// eslint-disable-next-line @typescript-eslint/no-unused-vars
125-
maybeFn?: IStepHookBody
124+
fn: IStepHookBody
126125
) {
127126
throw createUnimplemented();
128127
}
129128

130-
export function AfterAll(fn: IStepHookBody): void;
131129
export function AfterAll(
132130
// eslint-disable-next-line @typescript-eslint/no-unused-vars
133-
maybeFn?: IStepHookBody
131+
fn: IHookBody
134132
) {
135133
throw createUnimplemented();
136134
}

lib/public-member-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface IHookOptions {
1212
}
1313

1414
export interface IHookBody {
15-
(this: Mocha.Context, options: IHookParameter): void;
15+
(this: Mocha.Context, options?: IHookParameter): void;
1616
}
1717

1818
export interface IHookParameter {

lib/registry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export class Registry {
101101
this.runStepDefininition = this.runStepDefininition.bind(this);
102102
this.defineParameterType = this.defineParameterType.bind(this);
103103
this.defineBefore = this.defineBefore.bind(this);
104+
this.defineAfter = this.defineAfter.bind(this);
104105

105106
this.parameterTypeRegistry = new ParameterTypeRegistry();
106107
}
@@ -286,7 +287,7 @@ export class Registry {
286287
return this.resolveHooks("After", tags).reverse();
287288
}
288289

289-
public runHook(world: Mocha.Context, hook: IHook, options: IHookParameter) {
290+
public runHook(world: Mocha.Context, hook: IHook, options?: IHookParameter) {
290291
return hook.implementation.call(world, options);
291292
}
292293

test-d/entrypoint-node.test-d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ defineParameterType({
140140
},
141141
});
142142

143+
BeforeAll(function () {
144+
expectType<Mocha.Context>(this);
145+
});
146+
143147
Before(function () {
144148
expectType<Mocha.Context>(this);
145149
});
@@ -164,6 +168,10 @@ After({ tags: "foo", name: "bar" }, function () {
164168
expectType<Mocha.Context>(this);
165169
});
166170

171+
AfterAll(function () {
172+
expectType<Mocha.Context>(this);
173+
});
174+
167175
BeforeStep(function ({
168176
pickle,
169177
pickleStep,

0 commit comments

Comments
 (0)