Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webpack loader #115

Merged
merged 1 commit into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 2 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,11 @@
/* eslint-disable no-eval */
const fs = require("fs");
const through = require("through");
const path = require("path");
const browserify = require("@cypress/browserify-preprocessor");
const log = require("debug")("cypress:cucumber");
const glob = require("glob");
const cosmiconfig = require("cosmiconfig");
const chokidar = require("chokidar");

// This is the template for the file that we will send back to cypress instead of the text of a
// feature file
const createCucumber = (spec, toRequire) =>
`
const {resolveAndRunStepDefinition, defineParameterType, given, when, then} = require('cypress-cucumber-preprocessor/resolveStepDefinition');
const Given = window.Given = window.given = given;
const When = window.When = window.when = when;
const Then = window.Then = window.then = then;
window.defineParameterType = defineParameterType;
const { createTestsFromFeature } = require('cypress-cucumber-preprocessor/createTestsFromFeature');
${eval(toRequire).join("\n")}
const {Parser, Compiler} = require('gherkin');
const spec = \`${spec}\`
const gherkinAst = new Parser().parse(spec);

createTestsFromFeature(gherkinAst);
`;

const stepDefinitionPath = () => {
const appRoot = process.cwd();

const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
const loaded = explorer.load();
if (loaded && loaded.config && loaded.config.step_definitions) {
return path.resolve(appRoot, loaded.config.step_definitions);
}

// XXX Deprecated, left here for backward compability
const cypressOptions = JSON.parse(
fs.readFileSync(`${appRoot}/cypress.json`, "utf-8")
);
if (cypressOptions && cypressOptions.fileServerFolder) {
return `${cypressOptions.fileServerFolder}/support/step_definitions`;
}

return `${appRoot}/cypress/support/step_definitions`;
};
const createPattern = () => `${stepDefinitionPath()}/**/*.+(js|ts)`;

const pattern = createPattern();

const getStepDefinitionsPaths = () => [].concat(glob.sync(pattern));

const compile = spec => {
log("compiling", spec);
const stepDefinitionsToRequire = getStepDefinitionsPaths().map(
sdPath => `require('${sdPath}')`
);
return createCucumber(spec, stepDefinitionsToRequire);
};
const compile = require("./loader.js");
const stepDefinitionPath = require("./stepDefinitionPath.js");

const transform = file => {
let data = "";
Expand Down
36 changes: 36 additions & 0 deletions loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-eval */
const log = require("debug")("cypress:cucumber");
const glob = require("glob");
const stepDefinitionPath = require("./stepDefinitionPath.js");

// This is the template for the file that we will send back to cypress instead of the text of a
// feature file
const createCucumber = (spec, toRequire) =>
`
const {resolveAndRunStepDefinition, defineParameterType, given, when, then} = require('cypress-cucumber-preprocessor/resolveStepDefinition');
const Given = window.Given = window.given = given;
const When = window.When = window.when = when;
const Then = window.Then = window.then = then;
window.defineParameterType = defineParameterType;
const { createTestsFromFeature } = require('cypress-cucumber-preprocessor/createTestsFromFeature');
${eval(toRequire).join("\n")}
const {Parser, Compiler} = require('gherkin');
const spec = \`${spec}\`
const gherkinAst = new Parser().parse(spec);

createTestsFromFeature(gherkinAst);
`;

const createPattern = () => `${stepDefinitionPath()}/**/*.+(js|ts)`;

const pattern = createPattern();

const getStepDefinitionsPaths = () => [].concat(glob.sync(pattern));

module.exports = spec => {
log("compiling", spec);
const stepDefinitionsToRequire = getStepDefinitionsPaths().map(
sdPath => `require('${sdPath}')`
);
return createCucumber(spec, stepDefinitionsToRequire);
};
23 changes: 23 additions & 0 deletions stepDefinitionPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require("fs");
const path = require("path");
const cosmiconfig = require("cosmiconfig");

module.exports = () => {
const appRoot = process.cwd();

const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
const loaded = explorer.load();
if (loaded && loaded.config && loaded.config.step_definitions) {
return path.resolve(appRoot, loaded.config.step_definitions);
}

// XXX Deprecated, left here for backward compability
const cypressOptions = JSON.parse(
fs.readFileSync(`${appRoot}/cypress.json`, "utf-8")
);
if (cypressOptions && cypressOptions.fileServerFolder) {
return `${cypressOptions.fileServerFolder}/support/step_definitions`;
}

return `${appRoot}/cypress/support/step_definitions`;
};