Skip to content

Commit e681296

Browse files
committed
Adding ordering in Before hook
1 parent 6cbf333 commit e681296

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

features/hooks_ordering.feature

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,47 @@ Feature: hooks ordering
4444
beforeEach(function() {
4545
expect(counter++, "Expected beforeEach() to be called after before()").to.equal(1)
4646
})
47-
Before(function() {
47+
Before({ order: 2 }, function() {
48+
expect(counter++, "Expected Before() to be called after beforeEach()").to.equal(3)
49+
})
50+
Before({ order: 1 }, function() {
4851
expect(counter++, "Expected Before() to be called after beforeEach()").to.equal(2)
4952
})
5053
Given("a background step", function() {
51-
expect(counter++, "Expected a background step to be called after Before()").to.equal(3)
54+
expect(counter++, "Expected a background step to be called after Before()").to.equal(4)
5255
})
5356
BeforeStep(function ({ pickleStep }) {
5457
if (pickleStep.text === "an ordinary step") {
55-
expect(counter++, "Expected BeforeStep() to be called before ordinary steps").to.equal(4)
58+
expect(counter++, "Expected BeforeStep() to be called before ordinary steps").to.equal(5)
5659
}
5760
})
5861
Given("an ordinary step", function() {
59-
expect(counter++, "Expected an ordinary step to be called after a background step").to.equal(5)
62+
expect(counter++, "Expected an ordinary step to be called after a background step").to.equal(6)
6063
})
6164
AfterStep(function ({ pickleStep }) {
6265
if (pickleStep.text === "an ordinary step") {
63-
expect(counter++, "Expected AfterStep() to be called after ordinary steps").to.equal(7)
66+
expect(counter++, "Expected AfterStep() to be called after ordinary steps").to.equal(8)
6467
}
6568
})
6669
AfterStep(function ({ pickleStep }) {
6770
if (pickleStep.text === "an ordinary step") {
68-
expect(counter++, "Expected AfterStep() to be called after ordinary steps").to.equal(6)
71+
expect(counter++, "Expected AfterStep() to be called after ordinary steps").to.equal(7)
6972
}
7073
})
7174
After(function() {
72-
expect(counter++, "Expected After() to be called in reverse order of definition").to.equal(9)
75+
expect(counter++, "Expected After() to be called in reverse order of definition").to.equal(10)
7376
})
7477
After(function() {
75-
expect(counter++, "Expected After() to be called after ordinary steps").to.equal(8)
78+
expect(counter++, "Expected After() to be called after ordinary steps").to.equal(9)
7679
})
7780
afterEach(function() {
78-
expect(counter++, "Expected afterEach() to be called after After()").to.equal(10)
81+
expect(counter++, "Expected afterEach() to be called after After()").to.equal(11)
7982
})
8083
AfterAll(function() {
81-
expect(counter++, "Expected AfterAll() to be called after afterEach()").to.equal(11)
84+
expect(counter++, "Expected AfterAll() to be called after afterEach()").to.equal(12)
8285
})
8386
after(function() {
84-
expect(counter++, "Expected after() to be called after AfterAll()").to.equal(12)
87+
expect(counter++, "Expected after() to be called after AfterAll()").to.equal(13)
8588
})
8689
"""
8790
When I run cypress

lib/public-member-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface IRunHookBody {
1313
export interface ICaseHookOptions {
1414
name?: string;
1515
tags?: string;
16+
order?: number;
1617
}
1718

1819
export interface ICaseHookParameter {

lib/registry.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export interface ICaseHook {
6565
position?: Position;
6666
tags?: string;
6767
name?: string;
68+
order?: number;
6869
}
6970

7071
export interface IStepHook {
@@ -291,7 +292,18 @@ export class Registry {
291292
}
292293

293294
public resolveBeforeHooks(tags: string[]) {
294-
return this.resolveCaseHooks("Before", tags);
295+
return this.resolveCaseHooks("Before", tags).sort(
296+
(a: ICaseHook, b: ICaseHook) => {
297+
// If order is not specified, the hook will be executed at the end
298+
if (a.order === undefined) {
299+
a.order = Number.MAX_SAFE_INTEGER;
300+
}
301+
if (b.order === undefined) {
302+
b.order = Number.MAX_SAFE_INTEGER;
303+
}
304+
return a.order - b.order;
305+
}
306+
);
295307
}
296308

297309
public resolveAfterHooks(tags: string[]) {

test-d/entrypoint-browser.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Before({}, function () {
156156
expectType<Mocha.Context>(this);
157157
});
158158

159-
Before({ tags: "foo", name: "bar" }, function () {
159+
Before({ tags: "foo", name: "bar", order: 1 }, function () {
160160
expectType<Mocha.Context>(this);
161161
});
162162

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Before({}, function () {
156156
expectType<Mocha.Context>(this);
157157
});
158158

159-
Before({ tags: "foo", name: "bar" }, function () {
159+
Before({ tags: "foo", name: "bar", order: 1 }, function () {
160160
expectType<Mocha.Context>(this);
161161
});
162162

0 commit comments

Comments
 (0)