Skip to content

Commit b2a3048

Browse files
committed
fix(.features): have the new load-all loader work with non-global step definitions
1 parent 9b2e907 commit b2a3048

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

lib/featuresLoader.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,32 @@ const log = require("debug")("cypress:cucumber");
77
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
88
const { cucumberTemplate } = require("./cucumberTemplate");
99
const { getCucumberJsonConfig } = require("./getCucumberJsonConfig");
10+
const {
11+
isNonGlobalStepDefinitionsMode
12+
} = require("./isNonGlobalStepDefinitionsMode");
1013

11-
// This is the template for the file that we will send back to cypress instead of the text of a
12-
// feature file
13-
const createCucumber = (specs, toRequire, cucumberJson) =>
14+
const createCucumber = (
15+
specs,
16+
globalToRequire,
17+
nonGlobalToRequire,
18+
cucumberJson
19+
) =>
1420
`
1521
${cucumberTemplate}
1622
1723
window.cucumberJson = ${JSON.stringify(cucumberJson)};
1824
19-
${toRequire.join("\n")}
25+
${globalToRequire.join("\n")}
2026
2127
${specs
2228
.map(
23-
({ spec, filePath, name }) =>
24-
`
29+
({ spec, filePath, name }) => `
2530
describe(\`${name}\`, function() {
31+
${nonGlobalToRequire &&
32+
nonGlobalToRequire
33+
.find(fileSteps => fileSteps[filePath])
34+
[filePath].join("\n")}
35+
2636
createTestsFromFeature('${filePath}', \`${spec}\`);
2737
})
2838
`
@@ -35,24 +45,35 @@ module.exports = function(_, filePath) {
3545

3646
const features = glob.sync(`${path.dirname(filePath)}/**/*.feature`);
3747

38-
const stepDefinitionsToRequire = [
39-
...new Set(
40-
features.reduce(
41-
requires =>
42-
requires.concat(
43-
getStepDefinitionsPaths(filePath).map(
44-
sdPath => `require('${sdPath}')`
45-
)
46-
),
47-
[]
48+
let globalStepDefinitionsToRequire = [];
49+
let nonGlobalStepDefinitionsToRequire;
50+
51+
if (isNonGlobalStepDefinitionsMode()) {
52+
nonGlobalStepDefinitionsToRequire = features.map(featurePath => ({
53+
[featurePath]: getStepDefinitionsPaths(featurePath).map(
54+
sdPath => `require('${sdPath}')`
4855
)
49-
)
50-
];
56+
}));
57+
} else {
58+
globalStepDefinitionsToRequire = [
59+
...new Set(
60+
features.reduce(
61+
requires =>
62+
requires.concat(
63+
getStepDefinitionsPaths(filePath).map(
64+
sdPath => `require('${sdPath}')`
65+
)
66+
),
67+
[]
68+
)
69+
)
70+
];
71+
}
5172

5273
const specs = features
5374
.map(featurePath => ({
5475
spec: fs.readFileSync(featurePath).toString(),
55-
filePath: path.basename(featurePath)
76+
filePath: featurePath
5677
}))
5778
.map(feature =>
5879
Object.assign({}, feature, {
@@ -62,7 +83,8 @@ module.exports = function(_, filePath) {
6283

6384
return createCucumber(
6485
specs,
65-
stepDefinitionsToRequire,
86+
globalStepDefinitionsToRequire,
87+
nonGlobalStepDefinitionsToRequire,
6688
getCucumberJsonConfig()
6789
);
6890
};

lib/getStepDefinitionsPaths.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const getStepDefinitionsPaths = filePath => {
1111
const nonGlobalPattern = `${getStepDefinitionPathsFrom(
1212
filePath
1313
)}/**/*.+(js|ts)`;
14-
14+
console.log("GOZDECKI nonGlobalPattern", nonGlobalPattern);
1515
const commonPath =
1616
loaded.config.commonPath || `${stepDefinitionPath()}/common/`;
1717
const commonDefinitionsPattern = `${commonPath}**/*.+(js|ts)`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const cosmiconfig = require("cosmiconfig");
2+
3+
exports.isNonGlobalStepDefinitionsMode = () => {
4+
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
5+
const loaded = explorer.load();
6+
return loaded && loaded.config && loaded.config.nonGlobalStepDefinitions;
7+
};

0 commit comments

Comments
 (0)