Skip to content

Commit

Permalink
feat: add support for custom step timeout
Browse files Browse the repository at this point in the history
Add support for passing in a config param, matching the signature of cucumber-js, to set the step
timeout in cypress.
  • Loading branch information
lukebjerring committed Jun 1, 2020
1 parent 8fe016d commit ee59a4e
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 14 deletions.
8 changes: 7 additions & 1 deletion lib/createTestFromScenario.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable prefer-template */
const statuses = require("cucumber/lib/status").default;
const {
resolveStepDefinition,
resolveAndRunStepDefinition,
resolveAndRunBeforeHooks,
resolveAndRunAfterHooks
Expand All @@ -15,8 +16,13 @@ const replaceParameterTags = (rowData, text) =>

// eslint-disable-next-line func-names
const stepTest = function(state, stepDetails, exampleRowData) {
const step = resolveStepDefinition.call(
this,
stepDetails,
state.feature.name
);
cy.then(() => state.onStartStep(stepDetails))
.then(() =>
.then((step && step.config) || {}, () =>
resolveAndRunStepDefinition.call(
this,
stepDetails,
Expand Down
34 changes: 23 additions & 11 deletions lib/resolveStepDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ class StepDefinitionRegistry {
};

this.definitions = [];
this.runtime = (matcher, implementation) => {
this.runtime = (...args) => {
let matcher;
let config;
let implementation;
if (args.length > 2) {
[matcher, config, implementation] = args;
} else {
[matcher, implementation] = args;
}
let expression;
if (matcher instanceof RegExp) {
expression = new RegularExpression(
Expand All @@ -35,6 +43,7 @@ class StepDefinitionRegistry {
this.definitions.push({
implementation,
expression,
config,
featureName: window.currentFeatureName || "___GLOBAL_EXECUTION___"
});
};
Expand Down Expand Up @@ -178,6 +187,9 @@ function parseHookArgs(args) {
}

module.exports = {
resolveStepDefinition(step, featureName) {
return resolveStepDefinition(step, featureName);
},
resolveAndRunBeforeHooks(scenarioTags, featureName) {
return resolveAndRunHooks(beforeHookRegistry, scenarioTags, featureName);
},
Expand Down Expand Up @@ -210,20 +222,20 @@ module.exports = {
}
throw new Error(`Step implementation missing for: ${stepText}`);
},
given: (expression, implementation) => {
stepDefinitionRegistry.runtime(expression, implementation);
given: (...args) => {
stepDefinitionRegistry.runtime(...args);
},
when: (expression, implementation) => {
stepDefinitionRegistry.runtime(expression, implementation);
when: (...args) => {
stepDefinitionRegistry.runtime(...args);
},
then: (expression, implementation) => {
stepDefinitionRegistry.runtime(expression, implementation);
then: (...args) => {
stepDefinitionRegistry.runtime(...args);
},
and: (expression, implementation) => {
stepDefinitionRegistry.runtime(expression, implementation);
and: (...args) => {
stepDefinitionRegistry.runtime(...args);
},
but: (expression, implementation) => {
stepDefinitionRegistry.runtime(expression, implementation);
but: (...args) => {
stepDefinitionRegistry.runtime(...args);
},
Before: (...args) => {
const { tags, implementation } = parseHookArgs(args);
Expand Down
13 changes: 11 additions & 2 deletions lib/testHelpers/setupTestFramework.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ const {
After
} = require("../resolveStepDefinition");

const mockedThen = (funcOrConfig, func) => {
if (typeof funcOrConfig === "object") {
func();
} else {
funcOrConfig();
}
return { then: mockedThen };
};

const mockedPromise = func => {
func();
return { then: mockedPromise };
return { then: mockedThen };
};

window.defineParameterType = defineParameterType;
Expand All @@ -46,6 +55,6 @@ window.cy = {
startStep: mockedPromise,
finishStep: mockedPromise,
finishTest: mockedPromise,
then: mockedPromise,
then: mockedThen,
end: mockedPromise
};
110 changes: 110 additions & 0 deletions steps/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
export function given(
expression: RegExp | string,
implementation: (...args: any[]) => void
): void;
export function when(
expression: RegExp | string,
implementation: (...args: any[]) => void
): void;
export function then(
expression: RegExp | string,
implementation: (...args: any[]) => void
): void;
export function and(
expression: RegExp | string,
implementation: (...args: any[]) => void
): void;
export function but(
expression: RegExp | string,
implementation: (...args: any[]) => void
): void;
export function given(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function when(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function then(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function and(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function but(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function defineStep(
expression: RegExp | string,
implementation: (...args: any[]) => void
): void;
export function defineParameterType(): void;

// Aliased versions of the above funcs.
export function Given(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function When(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function Then(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function And(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function But(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function Given(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function When(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function Then(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function And(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function But(
expression: RegExp | string,
config: { timeout?: number },
implementation: (...args: any[]) => void
): void;
export function Before(
optionsOrImplementation: object | ((...args: any[]) => void),
implementation?: (...args: any[]) => void
): void;
export function After(
optionsOrImplementation: object | ((...args: any[]) => void),
implementation?: (...args: any[]) => void
): void;

0 comments on commit ee59a4e

Please sign in to comment.