Skip to content

Commit ff9d006

Browse files
authored
feat: add support for custom step timeout (#385)
Add support for passing in a config param, matching the signature of cucumber-js, to set the step timeout in cypress.
1 parent 5a28f9e commit ff9d006

File tree

4 files changed

+146
-14
lines changed

4 files changed

+146
-14
lines changed

lib/createTestFromScenario.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable prefer-template */
22
const statuses = require("cucumber/lib/status").default;
33
const {
4+
resolveStepDefinition,
45
resolveAndRunStepDefinition,
56
resolveAndRunBeforeHooks,
67
resolveAndRunAfterHooks
@@ -15,8 +16,13 @@ const replaceParameterTags = (rowData, text) =>
1516

1617
// eslint-disable-next-line func-names
1718
const stepTest = function(state, stepDetails, exampleRowData) {
19+
const step = resolveStepDefinition.call(
20+
this,
21+
stepDetails,
22+
state.feature.name
23+
);
1824
cy.then(() => state.onStartStep(stepDetails))
19-
.then(() =>
25+
.then((step && step.config) || {}, () =>
2026
resolveAndRunStepDefinition.call(
2127
this,
2228
stepDetails,

lib/resolveStepDefinition.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ class StepDefinitionRegistry {
1818
};
1919

2020
this.definitions = [];
21-
this.runtime = (matcher, implementation) => {
21+
this.runtime = (...args) => {
22+
let matcher;
23+
let config;
24+
let implementation;
25+
if (args.length > 2) {
26+
[matcher, config, implementation] = args;
27+
} else {
28+
[matcher, implementation] = args;
29+
}
2230
let expression;
2331
if (matcher instanceof RegExp) {
2432
expression = new RegularExpression(
@@ -35,6 +43,7 @@ class StepDefinitionRegistry {
3543
this.definitions.push({
3644
implementation,
3745
expression,
46+
config,
3847
featureName: window.currentFeatureName || "___GLOBAL_EXECUTION___"
3948
});
4049
};
@@ -178,6 +187,9 @@ function parseHookArgs(args) {
178187
}
179188

180189
module.exports = {
190+
resolveStepDefinition(step, featureName) {
191+
return resolveStepDefinition(step, featureName);
192+
},
181193
resolveAndRunBeforeHooks(scenarioTags, featureName) {
182194
return resolveAndRunHooks(beforeHookRegistry, scenarioTags, featureName);
183195
},
@@ -210,20 +222,20 @@ module.exports = {
210222
}
211223
throw new Error(`Step implementation missing for: ${stepText}`);
212224
},
213-
given: (expression, implementation) => {
214-
stepDefinitionRegistry.runtime(expression, implementation);
225+
given: (...args) => {
226+
stepDefinitionRegistry.runtime(...args);
215227
},
216-
when: (expression, implementation) => {
217-
stepDefinitionRegistry.runtime(expression, implementation);
228+
when: (...args) => {
229+
stepDefinitionRegistry.runtime(...args);
218230
},
219-
then: (expression, implementation) => {
220-
stepDefinitionRegistry.runtime(expression, implementation);
231+
then: (...args) => {
232+
stepDefinitionRegistry.runtime(...args);
221233
},
222-
and: (expression, implementation) => {
223-
stepDefinitionRegistry.runtime(expression, implementation);
234+
and: (...args) => {
235+
stepDefinitionRegistry.runtime(...args);
224236
},
225-
but: (expression, implementation) => {
226-
stepDefinitionRegistry.runtime(expression, implementation);
237+
but: (...args) => {
238+
stepDefinitionRegistry.runtime(...args);
227239
},
228240
Before: (...args) => {
229241
const { tags, implementation } = parseHookArgs(args);

lib/testHelpers/setupTestFramework.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ const {
2525
After
2626
} = require("../resolveStepDefinition");
2727

28+
const mockedThen = (funcOrConfig, func) => {
29+
if (typeof funcOrConfig === "object") {
30+
func();
31+
} else {
32+
funcOrConfig();
33+
}
34+
return { then: mockedThen };
35+
};
36+
2837
const mockedPromise = func => {
2938
func();
30-
return { then: mockedPromise };
39+
return { then: mockedThen };
3140
};
3241

3342
window.defineParameterType = defineParameterType;
@@ -47,6 +56,6 @@ window.cy = {
4756
startStep: mockedPromise,
4857
finishStep: mockedPromise,
4958
finishTest: mockedPromise,
50-
then: mockedPromise,
59+
then: mockedThen,
5160
end: mockedPromise
5261
};

steps/index.d.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
export function given(
2+
expression: RegExp | string,
3+
implementation: (...args: any[]) => void
4+
): void;
5+
export function when(
6+
expression: RegExp | string,
7+
implementation: (...args: any[]) => void
8+
): void;
9+
export function then(
10+
expression: RegExp | string,
11+
implementation: (...args: any[]) => void
12+
): void;
13+
export function and(
14+
expression: RegExp | string,
15+
implementation: (...args: any[]) => void
16+
): void;
17+
export function but(
18+
expression: RegExp | string,
19+
implementation: (...args: any[]) => void
20+
): void;
21+
export function given(
22+
expression: RegExp | string,
23+
config: { timeout?: number },
24+
implementation: (...args: any[]) => void
25+
): void;
26+
export function when(
27+
expression: RegExp | string,
28+
config: { timeout?: number },
29+
implementation: (...args: any[]) => void
30+
): void;
31+
export function then(
32+
expression: RegExp | string,
33+
config: { timeout?: number },
34+
implementation: (...args: any[]) => void
35+
): void;
36+
export function and(
37+
expression: RegExp | string,
38+
config: { timeout?: number },
39+
implementation: (...args: any[]) => void
40+
): void;
41+
export function but(
42+
expression: RegExp | string,
43+
config: { timeout?: number },
44+
implementation: (...args: any[]) => void
45+
): void;
46+
export function defineStep(
47+
expression: RegExp | string,
48+
implementation: (...args: any[]) => void
49+
): void;
50+
export function defineParameterType(): void;
51+
52+
// Aliased versions of the above funcs.
53+
export function Given(
54+
expression: RegExp | string,
55+
implementation: (...args: any[]) => void
56+
): void;
57+
export function When(
58+
expression: RegExp | string,
59+
implementation: (...args: any[]) => void
60+
): void;
61+
export function Then(
62+
expression: RegExp | string,
63+
implementation: (...args: any[]) => void
64+
): void;
65+
export function And(
66+
expression: RegExp | string,
67+
implementation: (...args: any[]) => void
68+
): void;
69+
export function But(
70+
expression: RegExp | string,
71+
implementation: (...args: any[]) => void
72+
): void;
73+
export function Given(
74+
expression: RegExp | string,
75+
config: { timeout?: number },
76+
implementation: (...args: any[]) => void
77+
): void;
78+
export function When(
79+
expression: RegExp | string,
80+
config: { timeout?: number },
81+
implementation: (...args: any[]) => void
82+
): void;
83+
export function Then(
84+
expression: RegExp | string,
85+
config: { timeout?: number },
86+
implementation: (...args: any[]) => void
87+
): void;
88+
export function And(
89+
expression: RegExp | string,
90+
config: { timeout?: number },
91+
implementation: (...args: any[]) => void
92+
): void;
93+
export function But(
94+
expression: RegExp | string,
95+
config: { timeout?: number },
96+
implementation: (...args: any[]) => void
97+
): void;
98+
export function Before(
99+
optionsOrImplementation: object | ((...args: any[]) => void),
100+
implementation?: (...args: any[]) => void
101+
): void;
102+
export function After(
103+
optionsOrImplementation: object | ((...args: any[]) => void),
104+
implementation?: (...args: any[]) => void
105+
): void;

0 commit comments

Comments
 (0)