Repository: TheBrainFamily/cypress-cucumber-preprocessor
Pull request #173 (2019-05-27 13:20:09)

Adds defineStep method
As a step could be used with different keywords, like Given and When for example, defineStep will allow to create a step without linking it to a unique keyword in the code.

Related to https://github.com/TheBrainFamily/cypress-cucumber-preprocessor/issues/172.

josselinbuils: Should I add documentation somewhere ?

Patch:
diff --git a/cypress-tags.js b/cypress-tags.js
index b89e28f..a36daf5 100755
--- a/cypress-tags.js
+++ b/cypress-tags.js
@@ -47,7 +47,9 @@ paths.forEach(featurePath => {
 
 try {
   execFileSync(
-    `${__dirname}/../.bin/cypress`,
+    process.platform === "win32"
+      ? `${__dirname}/../.bin/cypress.cmd`
+      : `${__dirname}/../.bin/cypress`,
     [...process.argv.slice(2), "--spec", featuresToRun.join(",")],
     {
       stdio: [process.stdin, process.stdout, process.stderr]
diff --git a/cypress/integration/DefineStep.feature b/cypress/integration/DefineStep.feature
new file mode 100644
index 0000000..085daa9
--- /dev/null
+++ b/cypress/integration/DefineStep.feature
@@ -0,0 +1,4 @@
+Feature: Using defineStep
+  Scenario: Should be able to use steps defined with defineStep
+    When I set the value to 2
+    Then The value equals 2
diff --git a/cypress/integration/TagsInheritance.feature b/cypress/integration/TagsInheritance.feature
new file mode 100644
index 0000000..43a88ea
--- /dev/null
+++ b/cypress/integration/TagsInheritance.feature
@@ -0,0 +1,9 @@
+@inherited-tag
+Feature: Tags Implementation
+
+  As a cucumber user I can use tag expression using both feature and scenario tags
+
+  @own-tag
+  Scenario: This scenario should run if @inherited-tag and @own-tag is present in env
+    Given '@inherited-tag and @own-tag' is in current TAGS environmental variable
+    Then this should run
diff --git a/cypress/support/step_definitions/usingDefineSteps.js b/cypress/support/step_definitions/usingDefineSteps.js
new file mode 100644
index 0000000..8e6fd62
--- /dev/null
+++ b/cypress/support/step_definitions/usingDefineSteps.js
@@ -0,0 +1,11 @@
+/* global defineStep */
+
+let storedValue = 0;
+
+defineStep("I set the value to {int}", value => {
+  storedValue = value;
+});
+
+defineStep("The value equals {int}", expectedValue => {
+  expect(storedValue).to.equal(expectedValue);
+});
diff --git a/lib/loader.js b/lib/loader.js
index fc4a110..7e10465 100644
--- a/lib/loader.js
+++ b/lib/loader.js
@@ -6,13 +6,14 @@ const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
 // feature file
 const createCucumber = (spec, toRequire) =>
   `
-  const {resolveAndRunStepDefinition, defineParameterType, given, when, then, and, but} = require('cypress-cucumber-preprocessor/lib/resolveStepDefinition');
+  const {resolveAndRunStepDefinition, defineParameterType, given, when, then, and, but, defineStep} = require('cypress-cucumber-preprocessor/lib/resolveStepDefinition');
   const Given = window.Given = window.given = given;
   const When = window.When = window.when = when;
   const Then = window.Then = window.then = then;
   const And = window.And = window.and = and;
   const But = window.But = window.but = but;
   window.defineParameterType = defineParameterType;
+  window.defineStep = defineStep;
   const { createTestsFromFeature } = require('cypress-cucumber-preprocessor/lib/createTestsFromFeature');
   ${eval(toRequire).join("\n")}
   const {Parser, Compiler} = require('gherkin');
diff --git a/lib/resolveStepDefinition.js b/lib/resolveStepDefinition.js
index fd7ff7d..93b7faa 100644
--- a/lib/resolveStepDefinition.js
+++ b/lib/resolveStepDefinition.js
@@ -138,5 +138,8 @@ module.exports = {
   but: (expression, implementation) => {
     stepDefinitionRegistry.runtime(expression, implementation);
   },
+  defineStep: (expression, implementation) => {
+    stepDefinitionRegistry.runtime(expression, implementation);
+  },
   defineParameterType: defineParameterType(stepDefinitionRegistry)
 };
diff --git a/steps.js b/steps.js
index 2e73e93..9676ce1 100644
--- a/steps.js
+++ b/steps.js
@@ -7,7 +7,8 @@ const {
   then,
   and,
   but,
-  defineParameterType
+  defineParameterType,
+  defineStep
 } = require("./lib/resolveStepDefinition");
 
 module.exports = {
@@ -21,5 +22,6 @@ module.exports = {
   Then: then,
   And: and,
   But: but,
-  defineParameterType
+  defineParameterType,
+  defineStep
 };


Test patch:
diff --git a/lib/createTestsFromFeature.js b/lib/createTestsFromFeature.js
index 9934773..914b3a0 100644
--- a/lib/createTestsFromFeature.js
+++ b/lib/createTestsFromFeature.js
@@ -20,7 +20,7 @@ const createTestsFromFeature = parsedFeature => {
       section =>
         section.tags &&
         section.tags.length &&
-        shouldProceedCurrentStep(section.tags)
+        shouldProceedCurrentStep(section.tags.concat(featureTags))
     );
   } else if (!sectionsWithTagsExist) {
     everythingShouldRun = true;
diff --git a/lib/resolveStepDefinition.test.js b/lib/resolveStepDefinition.test.js
index 777dc0d..381988a 100644
--- a/lib/resolveStepDefinition.test.js
+++ b/lib/resolveStepDefinition.test.js
@@ -5,6 +5,7 @@ const { Parser } = require("gherkin");
 const { createTestsFromFeature } = require("./createTestsFromFeature");
 const {
   defineParameterType,
+  defineStep,
   when,
   then,
   given,
@@ -18,6 +19,7 @@ window.then = then;
 window.given = given;
 window.and = and;
 window.but = but;
+window.defineStep = defineStep;
 window.cy = {
   log: jest.fn()
 };
@@ -97,6 +99,17 @@ describe("Tags implementation", () => {
   );
 });
 
+describe("Tags inheritance", () => {
+  window.Cypress = {
+    env: () => "@inherited-tag and @own-tag"
+  };
+  require("../cypress/support/step_definitions/tags_implementation_with_env_set");
+
+  createTestsFromFeature(
+    readAndParseFeatureFile("./cypress/integration/TagsInheritance.feature")
+  );
+});
+
 describe("Tags with env TAGS set", () => {
   window.Cypress = {
     env: () => "@test-tag and not @ignore-tag"
@@ -134,3 +147,11 @@ describe("And and But", () => {
     readAndParseFeatureFile("./cypress/integration/AndAndButSteps.feature")
   );
 });
+
+describe("defineStep", () => {
+  require("../cypress/support/step_definitions/usingDefineSteps");
+
+  createTestsFromFeature(
+    readAndParseFeatureFile("./cypress/integration/DefineStep.feature")
+  );
+});