Repository: TheBrainFamily/cypress-cucumber-preprocessor
Pull request #358 (2020-04-07 13:49:42)

Pr/275

lgandecki: This is a PR based on @alOneh in #275 with tiny tweak

Patch:
diff --git a/lib/getStepDefinitionsPaths.js b/lib/getStepDefinitionsPaths.js
index 42ada5f..3744fd6 100644
--- a/lib/getStepDefinitionsPaths.js
+++ b/lib/getStepDefinitionsPaths.js
@@ -1,16 +1,23 @@
+const path = require("path");
 const glob = require("glob");
 const { getConfig } = require("./getConfig");
 const stepDefinitionPath = require("./stepDefinitionPath.js");
 const { getStepDefinitionPathsFrom } = require("./getStepDefinitionPathsFrom");
 
 const getStepDefinitionsPaths = filePath => {
+  const appRoot = process.cwd();
   let paths = [];
   const config = getConfig();
   if (config && config.nonGlobalStepDefinitions) {
     const nonGlobalPattern = `${getStepDefinitionPathsFrom(
       filePath
     )}/**/*.+(js|ts)`;
-    const commonPath = config.commonPath || `${stepDefinitionPath()}/common/`;
+
+    let commonPath = config.commonPath || `${stepDefinitionPath()}/common/`;
+
+    if (config.commonPath) {
+      commonPath = path.resolve(appRoot, commonPath);
+    }
     const commonDefinitionsPattern = `${commonPath}**/*.+(js|ts)`;
     paths = paths.concat(glob.sync(nonGlobalPattern));
     paths = paths.concat(glob.sync(commonDefinitionsPattern));


Test patch:
diff --git a/lib/getStepDefinitionsPaths.test.js b/lib/getStepDefinitionsPaths.test.js
index 34c3e70..212cb09 100644
--- a/lib/getStepDefinitionsPaths.test.js
+++ b/lib/getStepDefinitionsPaths.test.js
@@ -1,3 +1,4 @@
+/* eslint-disable global-require */
 jest.mock("./stepDefinitionPath.js", () => () => "stepDefinitionPath");
 jest.mock("glob", () => ({
   sync(pattern) {
@@ -5,17 +6,19 @@ jest.mock("glob", () => ({
   }
 }));
 
+let getConfig;
+
 describe("getStepDefinitionsPaths", () => {
-  it("should return the default common folder", () => {
+  beforeEach(() => {
     jest.resetModules();
-    jest.mock("cosmiconfig", () => () => ({
-      load: () => ({
-        config: {
-          nonGlobalStepDefinitions: true
-        }
-      })
-    }));
-    // eslint-disable-next-line global-require
+    ({ getConfig } = require("./getConfig"));
+    jest.mock("./getConfig");
+  });
+  it("should return the default common folder", () => {
+    getConfig.mockReturnValue({
+      nonGlobalStepDefinitions: true
+    });
+
     const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
 
     const actual = getStepDefinitionsPaths("/path");
@@ -23,22 +26,29 @@ describe("getStepDefinitionsPaths", () => {
 
     expect(actual).to.include(expected);
   });
-  it("should return the common folder defined by the developper", () => {
-    jest.resetModules();
-    jest.mock("cosmiconfig", () => () => ({
-      load: () => ({
-        config: {
-          nonGlobalStepDefinitions: true,
-          commonPath: "myPath/"
-        }
-      })
+
+  it("should return the common folder defined by the developer", () => {
+    jest.mock("path", () => ({
+      resolve(appRoot, commonPath) {
+        return `./${appRoot}/${commonPath}`;
+      },
+      extname() {
+        return ".js";
+      }
     }));
-    // eslint-disable-next-line global-require
+
+    jest.mock("fs");
+    jest.spyOn(process, "cwd").mockImplementation(() => "cwd");
+
+    getConfig.mockReturnValue({
+      nonGlobalStepDefinitions: true,
+      commonPath: "myPath/"
+    });
+
     const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
 
     const actual = getStepDefinitionsPaths("/path");
-    const expected = "myPath/**/*.+(js|ts)";
-
+    const expected = "./cwd/myPath/**/*.+(js|ts)";
     expect(actual).to.include(expected);
   });
 });