Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for custom step timeout #385

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
};
105 changes: 105 additions & 0 deletions steps/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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,
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 Before(
optionsOrImplementation: object | ((...args: any[]) => void),
implementation?: (...args: any[]) => void
): void;
export function After(
optionsOrImplementation: object | ((...args: any[]) => void),
implementation?: (...args: any[]) => void
): void;