Skip to content

Commit 2e2d64a

Browse files
mkusherlgandecki
authored andcommitted
feat: Add webpack loader #114 (#115)
1 parent 99fb342 commit 2e2d64a

File tree

3 files changed

+61
-54
lines changed

3 files changed

+61
-54
lines changed

index.js

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,11 @@
11
/* eslint-disable no-eval */
22
const fs = require("fs");
33
const through = require("through");
4-
const path = require("path");
54
const browserify = require("@cypress/browserify-preprocessor");
65
const log = require("debug")("cypress:cucumber");
7-
const glob = require("glob");
8-
const cosmiconfig = require("cosmiconfig");
96
const chokidar = require("chokidar");
10-
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 = (spec, toRequire) =>
14-
`
15-
const {resolveAndRunStepDefinition, defineParameterType, given, when, then} = require('cypress-cucumber-preprocessor/resolveStepDefinition');
16-
const Given = window.Given = window.given = given;
17-
const When = window.When = window.when = when;
18-
const Then = window.Then = window.then = then;
19-
window.defineParameterType = defineParameterType;
20-
const { createTestsFromFeature } = require('cypress-cucumber-preprocessor/createTestsFromFeature');
21-
${eval(toRequire).join("\n")}
22-
const {Parser, Compiler} = require('gherkin');
23-
const spec = \`${spec}\`
24-
const gherkinAst = new Parser().parse(spec);
25-
26-
createTestsFromFeature(gherkinAst);
27-
`;
28-
29-
const stepDefinitionPath = () => {
30-
const appRoot = process.cwd();
31-
32-
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
33-
const loaded = explorer.load();
34-
if (loaded && loaded.config && loaded.config.step_definitions) {
35-
return path.resolve(appRoot, loaded.config.step_definitions);
36-
}
37-
38-
// XXX Deprecated, left here for backward compability
39-
const cypressOptions = JSON.parse(
40-
fs.readFileSync(`${appRoot}/cypress.json`, "utf-8")
41-
);
42-
if (cypressOptions && cypressOptions.fileServerFolder) {
43-
return `${cypressOptions.fileServerFolder}/support/step_definitions`;
44-
}
45-
46-
return `${appRoot}/cypress/support/step_definitions`;
47-
};
48-
const createPattern = () => `${stepDefinitionPath()}/**/*.+(js|ts)`;
49-
50-
const pattern = createPattern();
51-
52-
const getStepDefinitionsPaths = () => [].concat(glob.sync(pattern));
53-
54-
const compile = spec => {
55-
log("compiling", spec);
56-
const stepDefinitionsToRequire = getStepDefinitionsPaths().map(
57-
sdPath => `require('${sdPath}')`
58-
);
59-
return createCucumber(spec, stepDefinitionsToRequire);
60-
};
7+
const compile = require("./loader.js");
8+
const stepDefinitionPath = require("./stepDefinitionPath.js");
619

6210
const transform = file => {
6311
let data = "";

loader.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable no-eval */
2+
const log = require("debug")("cypress:cucumber");
3+
const glob = require("glob");
4+
const stepDefinitionPath = require("./stepDefinitionPath.js");
5+
6+
// This is the template for the file that we will send back to cypress instead of the text of a
7+
// feature file
8+
const createCucumber = (spec, toRequire) =>
9+
`
10+
const {resolveAndRunStepDefinition, defineParameterType, given, when, then} = require('cypress-cucumber-preprocessor/resolveStepDefinition');
11+
const Given = window.Given = window.given = given;
12+
const When = window.When = window.when = when;
13+
const Then = window.Then = window.then = then;
14+
window.defineParameterType = defineParameterType;
15+
const { createTestsFromFeature } = require('cypress-cucumber-preprocessor/createTestsFromFeature');
16+
${eval(toRequire).join("\n")}
17+
const {Parser, Compiler} = require('gherkin');
18+
const spec = \`${spec}\`
19+
const gherkinAst = new Parser().parse(spec);
20+
21+
createTestsFromFeature(gherkinAst);
22+
`;
23+
24+
const createPattern = () => `${stepDefinitionPath()}/**/*.+(js|ts)`;
25+
26+
const pattern = createPattern();
27+
28+
const getStepDefinitionsPaths = () => [].concat(glob.sync(pattern));
29+
30+
module.exports = spec => {
31+
log("compiling", spec);
32+
const stepDefinitionsToRequire = getStepDefinitionsPaths().map(
33+
sdPath => `require('${sdPath}')`
34+
);
35+
return createCucumber(spec, stepDefinitionsToRequire);
36+
};

stepDefinitionPath.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const cosmiconfig = require("cosmiconfig");
4+
5+
module.exports = () => {
6+
const appRoot = process.cwd();
7+
8+
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
9+
const loaded = explorer.load();
10+
if (loaded && loaded.config && loaded.config.step_definitions) {
11+
return path.resolve(appRoot, loaded.config.step_definitions);
12+
}
13+
14+
// XXX Deprecated, left here for backward compability
15+
const cypressOptions = JSON.parse(
16+
fs.readFileSync(`${appRoot}/cypress.json`, "utf-8")
17+
);
18+
if (cypressOptions && cypressOptions.fileServerFolder) {
19+
return `${cypressOptions.fileServerFolder}/support/step_definitions`;
20+
}
21+
22+
return `${appRoot}/cypress/support/step_definitions`;
23+
};

0 commit comments

Comments
 (0)