Skip to content

Commit 6db26a7

Browse files
committed
Add TypeScript support for custom world in hooks
This fixes #1304 [1]. [1] #1304
1 parent 6fac579 commit 6db26a7

File tree

8 files changed

+185
-105
lines changed

8 files changed

+185
-105
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 TypeScript support for custom world in hooks, fixes [#1304](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1304).
8+
59
## v22.1.0
610

711
- Gracefully handle sourcemaps when running component tests with webpack and chunks enabled (default behavior), fixes [#1296](https://github.com/badeball/cypress-cucumber-preprocessor/issues/1296).

lib/browser-runtime.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function getSourceReferenceFromPosition(
119119
}
120120

121121
interface IStep {
122-
hook?: ICaseHook;
122+
hook?: ICaseHook<Mocha.Context>;
123123
pickleStep?: messages.PickleStep;
124124
}
125125

@@ -244,7 +244,7 @@ function emitSkippedPickle(
244244
const testCaseStartedId = context.newId();
245245
const timestamp = createTimestamp();
246246

247-
const steps: (ICaseHook | messages.PickleStep)[] = [
247+
const steps: (ICaseHook<Mocha.Context> | messages.PickleStep)[] = [
248248
...beforeHooks,
249249
...pickleSteps,
250250
...afterHooks,
@@ -1231,7 +1231,7 @@ export default function createTests(
12311231
const beforeHooks = registry.resolveBeforeHooks(tags);
12321232
const afterHooks = registry.resolveAfterHooks(tags);
12331233

1234-
const hooksToStep = (hook: ICaseHook): messages.TestStep => {
1234+
const hooksToStep = (hook: ICaseHook<Mocha.Context>): messages.TestStep => {
12351235
return {
12361236
id: createTestStepId({
12371237
testStepIds,

lib/entrypoint-browser.ts

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -71,91 +71,109 @@ function defineParameterType<T, C extends Mocha.Context>(
7171
getRegistry().defineParameterType(options);
7272
}
7373

74-
function defineBefore(options: ICaseHookOptions, fn: ICaseHookBody): void;
75-
function defineBefore(fn: ICaseHookBody): void;
76-
function defineBefore(
77-
optionsOrFn: ICaseHookBody | ICaseHookOptions,
78-
maybeFn?: ICaseHookBody,
74+
function defineBefore<C extends Mocha.Context>(
75+
options: ICaseHookOptions,
76+
fn: ICaseHookBody<C>,
77+
): void;
78+
function defineBefore<C extends Mocha.Context>(fn: ICaseHookBody<C>): void;
79+
function defineBefore<C extends Mocha.Context>(
80+
optionsOrFn: ICaseHookBody<C> | ICaseHookOptions,
81+
maybeFn?: ICaseHookBody<C>,
7982
) {
8083
if (typeof optionsOrFn === "function") {
81-
getRegistry().defineBefore({}, optionsOrFn);
84+
getRegistry<C, unknown[]>().defineBefore({}, optionsOrFn);
8285
} else if (typeof optionsOrFn === "object" && typeof maybeFn === "function") {
83-
getRegistry().defineBefore(optionsOrFn, maybeFn);
86+
getRegistry<C, unknown[]>().defineBefore(optionsOrFn, maybeFn);
8487
} else {
8588
throw new Error("Unexpected argument for Before hook");
8689
}
8790
}
8891

89-
function defineAfter(options: ICaseHookOptions, fn: ICaseHookBody): void;
90-
function defineAfter(fn: ICaseHookBody): void;
91-
function defineAfter(
92-
optionsOrFn: ICaseHookBody | ICaseHookOptions,
93-
maybeFn?: ICaseHookBody,
92+
function defineAfter<C extends Mocha.Context>(
93+
options: ICaseHookOptions,
94+
fn: ICaseHookBody<C>,
95+
): void;
96+
function defineAfter<C extends Mocha.Context>(fn: ICaseHookBody<C>): void;
97+
function defineAfter<C extends Mocha.Context>(
98+
optionsOrFn: ICaseHookBody<C> | ICaseHookOptions,
99+
maybeFn?: ICaseHookBody<C>,
94100
) {
95101
if (typeof optionsOrFn === "function") {
96-
getRegistry().defineAfter({}, optionsOrFn);
102+
getRegistry<C, unknown[]>().defineAfter({}, optionsOrFn);
97103
} else if (typeof optionsOrFn === "object" && typeof maybeFn === "function") {
98-
getRegistry().defineAfter(optionsOrFn, maybeFn);
104+
getRegistry<C, unknown[]>().defineAfter(optionsOrFn, maybeFn);
99105
} else {
100106
throw new Error("Unexpected argument for After hook");
101107
}
102108
}
103109

104-
function defineBeforeStep(options: IStepHookOptions, fn: IStepHookBody): void;
105-
function defineBeforeStep(fn: IStepHookBody): void;
106-
function defineBeforeStep(
107-
optionsOrFn: IStepHookBody | IStepHookOptions,
108-
maybeFn?: IStepHookBody,
110+
function defineBeforeStep<C extends Mocha.Context>(
111+
options: IStepHookOptions,
112+
fn: IStepHookBody<C>,
113+
): void;
114+
function defineBeforeStep<C extends Mocha.Context>(fn: IStepHookBody<C>): void;
115+
function defineBeforeStep<C extends Mocha.Context>(
116+
optionsOrFn: IStepHookBody<C> | IStepHookOptions,
117+
maybeFn?: IStepHookBody<C>,
109118
) {
110119
if (typeof optionsOrFn === "function") {
111-
getRegistry().defineBeforeStep({}, optionsOrFn);
120+
getRegistry<C, unknown[]>().defineBeforeStep({}, optionsOrFn);
112121
} else if (typeof optionsOrFn === "object" && typeof maybeFn === "function") {
113-
getRegistry().defineBeforeStep(optionsOrFn, maybeFn);
122+
getRegistry<C, unknown[]>().defineBeforeStep(optionsOrFn, maybeFn);
114123
} else {
115124
throw new Error("Unexpected argument for BeforeStep hook");
116125
}
117126
}
118127

119-
function defineAfterStep(options: IStepHookOptions, fn: IStepHookBody): void;
120-
function defineAfterStep(fn: IStepHookBody): void;
121-
function defineAfterStep(
122-
optionsOrFn: IStepHookBody | IStepHookOptions,
123-
maybeFn?: IStepHookBody,
128+
function defineAfterStep<C extends Mocha.Context>(
129+
options: IStepHookOptions,
130+
fn: IStepHookBody<C>,
131+
): void;
132+
function defineAfterStep<C extends Mocha.Context>(fn: IStepHookBody<C>): void;
133+
function defineAfterStep<C extends Mocha.Context>(
134+
optionsOrFn: IStepHookBody<C> | IStepHookOptions,
135+
maybeFn?: IStepHookBody<C>,
124136
) {
125137
if (typeof optionsOrFn === "function") {
126-
getRegistry().defineAfterStep({}, optionsOrFn);
138+
getRegistry<C, unknown[]>().defineAfterStep({}, optionsOrFn);
127139
} else if (typeof optionsOrFn === "object" && typeof maybeFn === "function") {
128-
getRegistry().defineAfterStep(optionsOrFn, maybeFn);
140+
getRegistry<C, unknown[]>().defineAfterStep(optionsOrFn, maybeFn);
129141
} else {
130142
throw new Error("Unexpected argument for AfterStep hook");
131143
}
132144
}
133145

134-
function defineBeforeAll(options: IRunHookOptions, fn: IRunHookBody): void;
135-
function defineBeforeAll(fn: IRunHookBody): void;
136-
function defineBeforeAll(
137-
optionsOrFn: IRunHookBody | IRunHookOptions,
138-
maybeFn?: IRunHookBody,
146+
function defineBeforeAll<C extends Mocha.Context>(
147+
options: IRunHookOptions,
148+
fn: IRunHookBody<C>,
149+
): void;
150+
function defineBeforeAll<C extends Mocha.Context>(fn: IRunHookBody<C>): void;
151+
function defineBeforeAll<C extends Mocha.Context>(
152+
optionsOrFn: IRunHookBody<C> | IRunHookOptions,
153+
maybeFn?: IRunHookBody<C>,
139154
) {
140155
if (typeof optionsOrFn === "function") {
141-
getRegistry().defineBeforeAll({}, optionsOrFn);
156+
getRegistry<C, unknown[]>().defineBeforeAll({}, optionsOrFn);
142157
} else if (typeof optionsOrFn === "object" && typeof maybeFn === "function") {
143-
getRegistry().defineBeforeAll(optionsOrFn, maybeFn);
158+
getRegistry<C, unknown[]>().defineBeforeAll(optionsOrFn, maybeFn);
144159
} else {
145160
throw new Error("Unexpected argument for BeforeAll hook");
146161
}
147162
}
148163

149-
function defineAfterAll(options: IRunHookOptions, fn: IRunHookBody): void;
150-
function defineAfterAll(fn: IRunHookBody): void;
151-
function defineAfterAll(
152-
optionsOrFn: IRunHookBody | IRunHookOptions,
153-
maybeFn?: IRunHookBody,
164+
function defineAfterAll<C extends Mocha.Context>(
165+
options: IRunHookOptions,
166+
fn: IRunHookBody<C>,
167+
): void;
168+
function defineAfterAll<C extends Mocha.Context>(fn: IRunHookBody<C>): void;
169+
function defineAfterAll<C extends Mocha.Context>(
170+
optionsOrFn: IRunHookBody<C> | IRunHookOptions,
171+
maybeFn?: IRunHookBody<C>,
154172
) {
155173
if (typeof optionsOrFn === "function") {
156-
getRegistry().defineAfterAll({}, optionsOrFn);
174+
getRegistry<C, unknown[]>().defineAfterAll({}, optionsOrFn);
157175
} else if (typeof optionsOrFn === "object" && typeof maybeFn === "function") {
158-
getRegistry().defineAfterAll(optionsOrFn, maybeFn);
176+
getRegistry<C, unknown[]>().defineAfterAll(optionsOrFn, maybeFn);
159177
} else {
160178
throw new Error("Unexpected argument for AfterAll hook");
161179
}

lib/entrypoint-node.ts

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,68 +78,86 @@ export function attach(data: string | ArrayBuffer, mediaType?: string) {
7878
throw createUnimplemented();
7979
}
8080

81-
export function Before(options: ICaseHookOptions, fn: ICaseHookBody): void;
82-
export function Before(fn: ICaseHookBody): void;
83-
export function Before(
81+
export function Before<C extends Mocha.Context>(
82+
options: ICaseHookOptions,
83+
fn: ICaseHookBody<C>,
84+
): void;
85+
export function Before<C extends Mocha.Context>(fn: ICaseHookBody<C>): void;
86+
export function Before<C extends Mocha.Context>(
8487
// eslint-disable-next-line @typescript-eslint/no-unused-vars
85-
optionsOrFn: ICaseHookBody | ICaseHookOptions,
88+
optionsOrFn: ICaseHookBody<C> | ICaseHookOptions,
8689
// eslint-disable-next-line @typescript-eslint/no-unused-vars
87-
maybeFn?: ICaseHookBody,
90+
maybeFn?: ICaseHookBody<C>,
8891
) {
8992
throw createUnimplemented();
9093
}
9194

92-
export function After(options: ICaseHookOptions, fn: ICaseHookBody): void;
93-
export function After(fn: ICaseHookBody): void;
94-
export function After(
95+
export function After<C extends Mocha.Context>(
96+
options: ICaseHookOptions,
97+
fn: ICaseHookBody<C>,
98+
): void;
99+
export function After<C extends Mocha.Context>(fn: ICaseHookBody<C>): void;
100+
export function After<C extends Mocha.Context>(
95101
// eslint-disable-next-line @typescript-eslint/no-unused-vars
96-
optionsOrFn: ICaseHookBody | ICaseHookOptions,
102+
optionsOrFn: ICaseHookBody<C> | ICaseHookOptions,
97103
// eslint-disable-next-line @typescript-eslint/no-unused-vars
98-
maybeFn?: ICaseHookBody,
104+
maybeFn?: ICaseHookBody<C>,
99105
) {
100106
throw createUnimplemented();
101107
}
102108

103-
export function BeforeStep(options: IStepHookOptions, fn: IStepHookBody): void;
104-
export function BeforeStep(fn: IStepHookBody): void;
105-
export function BeforeStep(
109+
export function BeforeStep<C extends Mocha.Context>(
110+
options: IStepHookOptions,
111+
fn: IStepHookBody<C>,
112+
): void;
113+
export function BeforeStep<C extends Mocha.Context>(fn: IStepHookBody<C>): void;
114+
export function BeforeStep<C extends Mocha.Context>(
106115
// eslint-disable-next-line @typescript-eslint/no-unused-vars
107-
optionsOrFn: IStepHookBody | IStepHookOptions,
116+
optionsOrFn: IStepHookBody<C> | IStepHookOptions,
108117
// eslint-disable-next-line @typescript-eslint/no-unused-vars
109-
maybeFn?: IStepHookBody,
118+
maybeFn?: IStepHookBody<C>,
110119
) {
111120
throw createUnimplemented();
112121
}
113122

114-
export function AfterStep(options: IStepHookOptions, fn: IStepHookBody): void;
115-
export function AfterStep(fn: IStepHookBody): void;
116-
export function AfterStep(
123+
export function AfterStep<C extends Mocha.Context>(
124+
options: IStepHookOptions,
125+
fn: IStepHookBody<C>,
126+
): void;
127+
export function AfterStep<C extends Mocha.Context>(fn: IStepHookBody<C>): void;
128+
export function AfterStep<C extends Mocha.Context>(
117129
// eslint-disable-next-line @typescript-eslint/no-unused-vars
118-
optionsOrFn: IStepHookBody | IStepHookOptions,
130+
optionsOrFn: IStepHookBody<C> | IStepHookOptions,
119131
// eslint-disable-next-line @typescript-eslint/no-unused-vars
120-
maybeFn?: IStepHookBody,
132+
maybeFn?: IStepHookBody<C>,
121133
) {
122134
throw createUnimplemented();
123135
}
124136

125-
export function BeforeAll(options: IRunHookOptions, fn: IRunHookBody): void;
126-
export function BeforeAll(fn: IRunHookBody): void;
127-
export function BeforeAll(
137+
export function BeforeAll<C extends Mocha.Context>(
138+
options: IRunHookOptions,
139+
fn: IRunHookBody<C>,
140+
): void;
141+
export function BeforeAll<C extends Mocha.Context>(fn: IRunHookBody<C>): void;
142+
export function BeforeAll<C extends Mocha.Context>(
128143
// eslint-disable-next-line @typescript-eslint/no-unused-vars
129-
optionsOrFn: IRunHookBody | IRunHookOptions,
144+
optionsOrFn: IRunHookBody<C> | IRunHookOptions,
130145
// eslint-disable-next-line @typescript-eslint/no-unused-vars
131-
maybeFn?: IRunHookBody,
146+
maybeFn?: IRunHookBody<C>,
132147
) {
133148
throw createUnimplemented();
134149
}
135150

136-
export function AfterAll(options: IRunHookOptions, fn: IRunHookBody): void;
137-
export function AfterAll(fn: IRunHookBody): void;
138-
export function AfterAll(
151+
export function AfterAll<C extends Mocha.Context>(
152+
options: IRunHookOptions,
153+
fn: IRunHookBody<C>,
154+
): void;
155+
export function AfterAll<C extends Mocha.Context>(fn: IRunHookBody<C>): void;
156+
export function AfterAll<C extends Mocha.Context>(
139157
// eslint-disable-next-line @typescript-eslint/no-unused-vars
140-
optionsOrFn: IRunHookBody | IRunHookOptions,
158+
optionsOrFn: IRunHookBody<C> | IRunHookOptions,
141159
// eslint-disable-next-line @typescript-eslint/no-unused-vars
142-
maybeFn?: IRunHookBody,
160+
maybeFn?: IRunHookBody<C>,
143161
) {
144162
throw createUnimplemented();
145163
}

lib/public-member-types.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export interface IRunHookOptions {
1010
order?: number;
1111
}
1212

13-
export interface IRunHookBody {
14-
(this: Mocha.Context): void;
13+
export interface IRunHookBody<C extends Mocha.Context> {
14+
(this: C): void;
1515
}
1616

1717
export interface ICaseHookOptions {
@@ -26,8 +26,8 @@ export interface ICaseHookParameter {
2626
testCaseStartedId: string;
2727
}
2828

29-
export interface ICaseHookBody {
30-
(this: Mocha.Context, options: ICaseHookParameter): void;
29+
export interface ICaseHookBody<C extends Mocha.Context> {
30+
(this: C, options: ICaseHookParameter): void;
3131
}
3232

3333
export interface IStepHookOptions {
@@ -44,8 +44,8 @@ export interface IStepHookParameter {
4444
testStepId: string;
4545
}
4646

47-
export interface IStepHookBody {
48-
(this: Mocha.Context, options: IStepHookParameter): void;
47+
export interface IStepHookBody<C extends Mocha.Context> {
48+
(this: C, options: IStepHookParameter): void;
4949
}
5050

5151
export interface IStepDefinitionBody<

0 commit comments

Comments
 (0)