Skip to content

Commit 1a7b57e

Browse files
committed
feat(getstepdefinitionspaths): configurable nonGlobalStepDefinitions base directory
Adds a new config option nonGlobalStepBaseDir which sets where the feature scoped step defintions are resolved from
1 parent 01e7622 commit 1a7b57e

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ Please make use of [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) t
8484

8585
Option | Default value | Description
8686
------------ | ------------- | -------------
87-
commonPath | `cypress/integration/common` when `nonGlobalStepDefinitions` is true <br> `cypress/support/step_definitions` when `nonGlobalStepDefinitions` is false | Define the path to a folder containing all common step definitions of your tests
88-
nonGlobalStepDefinitions | false | If true use the Cypress Cucumber Preprocessor Style pattern for placing step definitions files. If false, we will use the "oldschool" (everything is global) Cucumber style
87+
commonPath | `cypress/integration/common` when `nonGlobalStepDefinitions` is true <br> `cypress/support/step_definitions` when `nonGlobalStepDefinitions` is false <br> `${nonGlobalStepBaseDir}/common` when `nonGlobalStepBaseDir` is defined | Define the path to a folder containing all common step definitions of your tests. When `nonGlobalStepBaseDir` is defined this path is defined from that base location. e.g `${nonGlobalStepBaseDir}/${commonPath}`.
88+
nonGlobalStepDefinitions | false | If true use the Cypress Cucumber Preprocessor Style pattern for placing step definitions files. If false, we will use the "oldschool" (everything is global) Cucumber style.
89+
nonGlobalStepBaseDir| undefined | If defined and `nonGlobalStepDefinitions` is also true then step definition searches for folders with the features name will start from the directory provided here. The cwd is already taken into account. e.g `test/step_definitions`.
8990
stepDefinitions | `cypress/integration` when `nonGlobalStepDefinitions` is true <br> `cypress/support/step_definitions` when `nonGlobalStepDefinitions` is false | Path to the folder containing our step definitions.
9091

9192
## How to organize the tests

lib/getStepDefinitionsPaths.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
const glob = require("glob");
22
const cosmiconfig = require("cosmiconfig");
3+
const process = require("process");
34
const stepDefinitionPath = require("./stepDefinitionPath.js");
45
const { getStepDefinitionPathsFrom } = require("./getStepDefinitionPathsFrom");
56

67
const getStepDefinitionsPaths = filePath => {
78
let paths = [];
9+
const featureBase = stepDefinitionPath();
10+
const appRoot = process.cwd();
811
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
912
const loaded = explorer.load();
1013
if (loaded && loaded.config && loaded.config.nonGlobalStepDefinitions) {
11-
const nonGlobalPattern = `${getStepDefinitionPathsFrom(
12-
filePath
13-
)}/**/*.+(js|ts)`;
14-
const commonPath =
15-
loaded.config.commonPath || `${stepDefinitionPath()}/common/`;
14+
let nonGlobalPath = getStepDefinitionPathsFrom(filePath);
15+
let commonPath = loaded.config.commonPath || `${featureBase}/common/`;
16+
17+
if (loaded.config.nonGlobalStepBaseDir) {
18+
const stepBase = `${appRoot}/${loaded.config.nonGlobalStepBaseDir}`;
19+
nonGlobalPath = nonGlobalPath.replace(featureBase, stepBase);
20+
commonPath = `${nonGlobalPath}/${loaded.config.commonPath ||
21+
`${stepBase}/common/`}`;
22+
}
23+
24+
const nonGlobalPattern = `${nonGlobalPath}/**/*.+(js|ts)`;
1625
const commonDefinitionsPattern = `${commonPath}**/*.+(js|ts)`;
17-
paths = paths.concat(glob.sync(nonGlobalPattern));
18-
paths = paths.concat(glob.sync(commonDefinitionsPattern));
26+
27+
paths = paths.concat(
28+
glob.sync(nonGlobalPattern),
29+
glob.sync(commonDefinitionsPattern)
30+
);
1931
} else {
20-
const pattern = `${stepDefinitionPath()}/**/*.+(js|ts)`;
32+
const pattern = `${featureBase}/**/*.+(js|ts)`;
2133
paths = paths.concat(glob.sync(pattern));
2234
}
2335
return paths;

lib/getStepDefinitionsPaths.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ jest.mock("glob", () => ({
55
}
66
}));
77

8+
jest.mock("process", () => ({
9+
cwd: () => "cwd"
10+
}));
11+
812
describe("getStepDefinitionsPaths", () => {
913
it("should return the default common folder", () => {
1014
jest.resetModules();
@@ -41,4 +45,42 @@ describe("getStepDefinitionsPaths", () => {
4145

4246
expect(actual).to.include(expected);
4347
});
48+
49+
it("should return the default non global step definition pattern", () => {
50+
jest.resetModules();
51+
jest.mock("cosmiconfig", () => () => ({
52+
load: () => ({
53+
config: {
54+
nonGlobalStepDefinitions: true
55+
}
56+
})
57+
}));
58+
// eslint-disable-next-line global-require
59+
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
60+
const path = "stepDefinitionPath/test.feature";
61+
const actual = getStepDefinitionsPaths(path);
62+
const expected = "stepDefinitionPath/test/**/*.+(js|ts)";
63+
64+
expect(actual).to.include(expected);
65+
});
66+
67+
it("should return the overriden non global step definition pattern if nonGlobalStepBaseDir is defined", () => {
68+
jest.resetModules();
69+
jest.mock("cosmiconfig", () => () => ({
70+
load: () => ({
71+
config: {
72+
nonGlobalStepDefinitions: true,
73+
nonGlobalStepBaseDir: "nonGlobalStepBaseDir"
74+
}
75+
})
76+
}));
77+
// eslint-disable-next-line global-require
78+
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
79+
const path = "stepDefinitionPath/test.feature";
80+
const actual = getStepDefinitionsPaths(path);
81+
const expected = "cwd/nonGlobalStepBaseDir/test/**/*.+(js|ts)";
82+
83+
expect(actual).to.include(expected);
84+
expect(actual).to.not.include("stepDefinitionPath/test/**/*.+(js|ts)");
85+
});
4486
});

0 commit comments

Comments
 (0)