Skip to content
Draft
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
18 changes: 12 additions & 6 deletions src/code-generation/step-generation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { globalSteps } from '../global-steps';
import { ParsedStep } from '../models';
import { indent } from './utils';
import { getJestCucumberConfiguration } from '../configuration';

const stepTemplate = (stepKeyword: string, stepMatcher: string, stepArgumentVariables: string[]) => {
return `${stepKeyword}(${stepMatcher}, (${stepArgumentVariables.join(', ')}) => {\n\n});`;
let template = `${stepKeyword}(${stepMatcher}`;
if (!globalSteps.get(stepMatcher)) {
template = `${template}, (${stepArgumentVariables.join(', ')}) => {\n\n}`;
}
return `${template});`;
};

const getStepFunctionWrapperName = (stepKeyword: string, stepText: string) => {
Expand All @@ -11,10 +17,10 @@ const getStepFunctionWrapperName = (stepKeyword: string, stepText: string) => {
};

const stepWrapperFunctionTemplate = (
stepKeyword: string,
stepText: string,
stepMatcher: string,
stepArgumentVariables: string[],
stepKeyword: string,
stepText: string,
stepMatcher: string,
stepArgumentVariables: string[],
) => {
// tslint:disable-next-line:max-line-length
return `export const ${getStepFunctionWrapperName(stepKeyword, stepText)} = (${stepKeyword}) => {\n${indent(stepTemplate(stepKeyword, stepMatcher, stepArgumentVariables), 1).slice(0, -1)}\n}`;
Expand Down Expand Up @@ -69,7 +75,7 @@ const getStepArguments = (step: ParsedStep) => {
const getStepMatcher = (step: ParsedStep) => {
let stepMatcher: string = '';

if (step.stepText.match(stepTextArgumentRegex)) {
if (step.stepText.match(stepTextArgumentRegex) && !getJestCucumberConfiguration().disableRegexGeneration) {
stepMatcher = convertStepTextToRegex(step);
} else {
stepMatcher = `'${step.stepText.replace(/'+/g, `\\'`)}'`;
Expand Down
1 change: 1 addition & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const defaultErrorSettings = {
const defaultConfiguration: Options = {
tagFilter: undefined,
scenarioNameTemplate: undefined,
disableRegexGeneration: undefined,
errors: defaultErrorSettings,
};

Expand Down
8 changes: 4 additions & 4 deletions src/feature-definition-creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
matchSteps,
} from './validation/step-definition-validation';
import { applyTagFilters } from './tag-filtering';
import { globalSteps } from './global-steps';

export type StepsDefinitionCallbackOptions = {
defineStep: DefineStepFunction;
Expand Down Expand Up @@ -247,12 +248,11 @@ const createDefineScenarioFunctionWithAliases = (
};

const createDefineStepFunction = (scenarioFromStepDefinitions: ScenarioFromStepDefinitions) => {
return (stepMatcher: string | RegExp, stepFunction: () => any) => {
return (stepMatcher: string | RegExp, stepFunction?: () => any) => {
const stepDefinition: StepFromStepDefinitions = {
stepMatcher,
stepFunction,
stepFunction: stepFunction || globalSteps.get(stepMatcher as string),
};

scenarioFromStepDefinitions.steps.push(stepDefinition);
};
};
Expand All @@ -270,7 +270,7 @@ export function defineFeature(

if (
parsedFeatureWithTagFiltersApplied.scenarios.length === 0
&& parsedFeatureWithTagFiltersApplied.scenarioOutlines.length === 0
&& parsedFeatureWithTagFiltersApplied.scenarioOutlines.length === 0
) {
return;
}
Expand Down
55 changes: 55 additions & 0 deletions src/global-steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { StepFromStepDefinitions } from './models';

class Steps {
list: StepFromStepDefinitions[] = [];

push(step: StepFromStepDefinitions) {
const { stepMatcher } = step;

const isAlreadyAdded = this.list.some(
step => String(step.stepMatcher) === String(stepMatcher),
);

if (isAlreadyAdded) {
throw new Error(`Existing global step with the name ${stepMatcher}`);
}

this.list.push(step);
}

get(title: StepFromStepDefinitions["stepMatcher"]) {
let params;

const found = this.list.find((step) => {
if (typeof title === 'string') {
const matches = title.match(step.stepMatcher);

if (matches) {
params = matches.slice(1);
return true;
}
}

if (title instanceof RegExp) {
return String(title) === String(step.stepMatcher);
}

return false;
});

if (!found) {
throw Error(`${title} : was not defined in Steps file`);
}

return found.stepFunction.bind(this, ...params);
}
}

export const globalSteps = new Steps();

export function defineGlobalStep(
stepMatcher: StepFromStepDefinitions["stepMatcher"],
stepFunction: StepFromStepDefinitions["stepFunction"]
) {
globalSteps.push({ stepMatcher, stepFunction });
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export {
generateCodeFromFeature,
generateCodeWithSeparateFunctionsFromFeature,
} from './code-generation/generate-code-by-line-number';
export { defineGlobalStep } from './global-steps';
1 change: 1 addition & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type ErrorOptions = {

export type Options = {
loadRelativePath?: boolean;
disableRegexGeneration?: boolean;
tagFilter?: string;
errors?: ErrorOptions | boolean;
scenarioNameTemplate?: (vars: ScenarioNameTemplateVars) => string;
Expand Down