Skip to content

Commit 6fac579

Browse files
committed
Properly type preliminaryStepDefinitions
This is some TS shenanigans in prepration of further work.
1 parent 751956b commit 6fac579

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

lib/browser-runtime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type Node = ReturnType<typeof parse>;
8383
type TestStepIds = Map<string, Map<string, string>>;
8484

8585
interface CompositionContext {
86-
registry: Registry;
86+
registry: Registry<Mocha.Context, unknown[]>;
8787
newId: messages.IdGenerator.NewId;
8888
gherkinDocument: messages.GherkinDocument;
8989
astIdsMap: ReturnType<typeof createAstIdMap>;
@@ -1176,7 +1176,7 @@ function afterHandler(this: Mocha.Context, context: CompositionContext) {
11761176
}
11771177

11781178
export default function createTests(
1179-
registry: Registry,
1179+
registry: Registry<Mocha.Context, unknown[]>,
11801180
seed: number,
11811181
source: string,
11821182
gherkinDocument: messages.GherkinDocument,

lib/entrypoint-browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function defineStep<T extends unknown[], C extends Mocha.Context>(
4848
description: string | RegExp,
4949
implementation: IStepDefinitionBody<T, C>,
5050
) {
51-
getRegistry().defineStep(description, implementation);
51+
getRegistry<C, T>().defineStep(description, implementation);
5252
}
5353

5454
function runStepDefinition(

lib/registry.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ function parseMaybeTags(tags?: string): Node {
8888
return tags ? parse(tags) : noopNode;
8989
}
9090

91-
export class Registry {
91+
export class Registry<C extends Mocha.Context, T extends unknown[]> {
9292
public parameterTypeRegistry: ParameterTypeRegistry;
9393

9494
private preliminaryStepDefinitions: {
9595
description: string | RegExp;
96-
implementation: () => void;
96+
implementation: IStepDefinitionBody<T, C>;
9797
position?: Position;
9898
}[] = [];
9999

100-
public stepDefinitions: IStepDefinition<unknown[], Mocha.Context>[] = [];
100+
public stepDefinitions: IStepDefinition<T, C>[] = [];
101101

102102
private preliminaryHooks: Omit<ICaseHook, "id">[] = [];
103103

@@ -151,7 +151,10 @@ export class Registry {
151151
}
152152
}
153153

154-
public defineStep(description: string | RegExp, implementation: () => void) {
154+
public defineStep(
155+
description: string | RegExp,
156+
implementation: IStepDefinitionBody<T, C>,
157+
) {
155158
if (typeof description !== "string" && !(description instanceof RegExp)) {
156159
throw new Error("Unexpected argument for step definition");
157160
}
@@ -281,7 +284,7 @@ export class Registry {
281284
}
282285

283286
public runStepDefinition(
284-
world: Mocha.Context,
287+
world: C,
285288
text: string,
286289
dryRun: boolean,
287290
argument?: DataTable | string,
@@ -290,7 +293,7 @@ export class Registry {
290293

291294
const args = stepDefinition.expression
292295
.match(text)!
293-
.map((match) => match.getValue(world));
296+
.map((match) => match.getValue(world)) as T;
294297

295298
if (argument) {
296299
args.push(argument);
@@ -375,26 +378,31 @@ export class Registry {
375378
const globalPropertyName =
376379
"__cypress_cucumber_preprocessor_registry_dont_use_this";
377380

378-
export function withRegistry(
381+
export function withRegistry<C extends Mocha.Context, T extends unknown[]>(
379382
experimentalSourceMap: boolean,
380383
fn: () => void,
381-
): Registry {
382-
const registry = new Registry(experimentalSourceMap);
384+
): Registry<C, T> {
385+
const registry = new Registry<C, T>(experimentalSourceMap);
383386
assignRegistry(registry);
384387
fn();
385388
freeRegistry();
386389
return registry;
387390
}
388391

389-
export function assignRegistry(registry: Registry) {
392+
export function assignRegistry<C extends Mocha.Context, T extends unknown[]>(
393+
registry: Registry<C, T>,
394+
) {
390395
globalThis[globalPropertyName] = registry;
391396
}
392397

393398
export function freeRegistry() {
394399
delete globalThis[globalPropertyName];
395400
}
396401

397-
export function getRegistry(): Registry {
402+
export function getRegistry<
403+
C extends Mocha.Context,
404+
T extends unknown[],
405+
>(): Registry<C, T> {
398406
return assertAndReturn(
399407
globalThis[globalPropertyName],
400408
"Expected to find a global registry (this usually means you are trying to define steps or hooks in support/e2e.js, which is not supported)",

0 commit comments

Comments
 (0)