Skip to content

Commit 19d2083

Browse files
authored
Merge pull request #236 from liamchilds/configurable-loc-non-global-steps
feat(getstepdefinitionspaths): configurable nonGlobalStepDefinitions base directory
2 parents 8a5f032 + ddf5883 commit 19d2083

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

README.md

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

8686
Option | Default value | Description
8787
------------ | ------------- | -------------
88-
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
89-
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
88+
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}`.
89+
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.
90+
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`.
9091
stepDefinitions | `cypress/integration` when `nonGlobalStepDefinitions` is true <br> `cypress/support/step_definitions` when `nonGlobalStepDefinitions` is false | Path to the folder containing our step definitions.
9192

9293
## How to organize the tests

lib/getStepDefinitionsPaths.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require("path");
22
const glob = require("glob");
3+
const process = require("process");
34
const { getConfig } = require("./getConfig");
45
const stepDefinitionPath = require("./stepDefinitionPath.js");
56
const { getStepDefinitionPathsFrom } = require("./getStepDefinitionPathsFrom");
@@ -9,18 +10,27 @@ const getStepDefinitionsPaths = filePath => {
910
let paths = [];
1011
const config = getConfig();
1112
if (config && config.nonGlobalStepDefinitions) {
12-
const nonGlobalPattern = `${getStepDefinitionPathsFrom(
13-
filePath
14-
)}/**/*.+(js|ts)`;
15-
13+
let nonGlobalPath = getStepDefinitionPathsFrom(filePath);
1614
let commonPath = config.commonPath || `${stepDefinitionPath()}/common/`;
1715

1816
if (config.commonPath) {
1917
commonPath = path.resolve(appRoot, commonPath);
2018
}
19+
20+
if (config.nonGlobalStepBaseDir) {
21+
const stepBase = `${appRoot}/${config.nonGlobalStepBaseDir}`;
22+
nonGlobalPath = nonGlobalPath.replace(stepDefinitionPath(), stepBase);
23+
commonPath = `${nonGlobalPath}/${config.commonPath ||
24+
`${stepBase}/common/`}`;
25+
}
26+
27+
const nonGlobalPattern = `${nonGlobalPath}/**/*.+(js|ts)`;
28+
2129
const commonDefinitionsPattern = `${commonPath}**/*.+(js|ts)`;
22-
paths = paths.concat(glob.sync(nonGlobalPattern));
23-
paths = paths.concat(glob.sync(commonDefinitionsPattern));
30+
paths = paths.concat(
31+
glob.sync(nonGlobalPattern),
32+
glob.sync(commonDefinitionsPattern)
33+
);
2434
} else {
2535
const pattern = `${stepDefinitionPath()}/**/*.+(js|ts)`;
2636
paths = paths.concat(glob.sync(pattern));

lib/getStepDefinitionsPaths.test.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe("getStepDefinitionsPaths", () => {
1212
beforeEach(() => {
1313
jest.resetModules();
1414
({ getConfig } = require("./getConfig"));
15+
jest.unmock("path");
1516
jest.mock("./getConfig");
1617
});
1718
it("should return the default common folder", () => {
@@ -23,7 +24,6 @@ describe("getStepDefinitionsPaths", () => {
2324

2425
const actual = getStepDefinitionsPaths("/path");
2526
const expected = "stepDefinitionPath/common/**/*.+(js|ts)";
26-
2727
expect(actual).to.include(expected);
2828
});
2929

@@ -37,7 +37,6 @@ describe("getStepDefinitionsPaths", () => {
3737
}
3838
}));
3939

40-
jest.mock("fs");
4140
jest.spyOn(process, "cwd").mockImplementation(() => "cwd");
4241

4342
getConfig.mockReturnValue({
@@ -51,4 +50,32 @@ describe("getStepDefinitionsPaths", () => {
5150
const expected = "./cwd/myPath/**/*.+(js|ts)";
5251
expect(actual).to.include(expected);
5352
});
53+
it("should return the default non global step definition pattern", () => {
54+
getConfig.mockReturnValue({
55+
nonGlobalStepDefinitions: true
56+
});
57+
// eslint-disable-next-line global-require
58+
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
59+
const path = "stepDefinitionPath/test.feature";
60+
const actual = getStepDefinitionsPaths(path);
61+
const expected = "stepDefinitionPath/test/**/*.+(js|ts)";
62+
63+
expect(actual).to.include(expected);
64+
});
65+
66+
it("should return the overriden non global step definition pattern if nonGlobalStepBaseDir is defined", () => {
67+
jest.spyOn(process, "cwd").mockImplementation(() => "cwd");
68+
getConfig.mockReturnValue({
69+
nonGlobalStepDefinitions: true,
70+
nonGlobalStepBaseDir: "nonGlobalStepBaseDir"
71+
});
72+
// eslint-disable-next-line global-require
73+
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
74+
const path = "stepDefinitionPath/test.feature";
75+
const actual = getStepDefinitionsPaths(path);
76+
const expected = "cwd/nonGlobalStepBaseDir/test/**/*.+(js|ts)";
77+
78+
expect(actual).to.include(expected);
79+
expect(actual).to.not.include("stepDefinitionPath/test/**/*.+(js|ts)");
80+
});
5481
});

0 commit comments

Comments
 (0)