Skip to content

Commit 3bd07a5

Browse files
darrinholstlgandecki
authored andcommitted
Add ability to combine multiple fetaure files into one spec
1 parent fc14b44 commit 3bd07a5

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/featuresLoader.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* eslint-disable no-eval */
2+
const glob = require("glob");
3+
const path = require("path");
4+
const fs = require("fs");
5+
const log = require("debug")("cypress:cucumber");
6+
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
7+
8+
// This is the template for the file that we will send back to cypress instead of the text of a
9+
// feature file
10+
const createCucumber = (specs, toRequire) =>
11+
`
12+
const {resolveAndRunStepDefinition, defineParameterType, given, when, then, and, but, defineStep} = require('cypress-cucumber-preprocessor/lib/resolveStepDefinition');
13+
const Given = window.Given = window.given = given;
14+
const When = window.When = window.when = when;
15+
const Then = window.Then = window.then = then;
16+
const And = window.And = window.and = and;
17+
const But = window.But = window.but = but;
18+
window.defineParameterType = defineParameterType;
19+
window.defineStep = defineStep;
20+
21+
const { createTestsFromFeature } = require('cypress-cucumber-preprocessor/lib/createTestsFromFeature');
22+
${eval(toRequire).join("\n")}
23+
const {Parser, Compiler} = require('gherkin');
24+
25+
${specs
26+
.map(spec => `createTestsFromFeature(new Parser().parse(\`${spec}\`))`)
27+
.join("\n")}
28+
`;
29+
30+
module.exports = function(_, filePath) {
31+
log("compiling", filePath);
32+
33+
const features = glob.sync(`${path.dirname(filePath)}/**/*.feature`);
34+
35+
const stepDefinitionsToRequire = [
36+
...new Set(
37+
features.reduce(
38+
requires =>
39+
requires.concat(
40+
getStepDefinitionsPaths(filePath).map(
41+
sdPath => `require('${sdPath}')`
42+
)
43+
),
44+
[]
45+
)
46+
)
47+
];
48+
49+
const specs = features.map(featurePath =>
50+
fs.readFileSync(featurePath).toString()
51+
);
52+
53+
return createCucumber(specs, stepDefinitionsToRequire);
54+
};

lib/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const browserify = require("@cypress/browserify-preprocessor");
55
const log = require("debug")("cypress:cucumber");
66
const chokidar = require("chokidar");
77
const compile = require("./loader.js");
8+
const compileFeatures = require("./featuresLoader.js");
89
const stepDefinitionPath = require("./stepDefinitionPath.js");
910

1011
const transform = file => {
@@ -15,7 +16,10 @@ const transform = file => {
1516
}
1617

1718
function end() {
18-
if (file.match(".feature$")) {
19+
if (file.match(".features$")) {
20+
log("compiling features ", file);
21+
this.queue(compileFeatures(data, file));
22+
} else if (file.match(".feature$")) {
1923
log("compiling feature ", file);
2024
this.queue(compile(data, file));
2125
} else {

0 commit comments

Comments
 (0)