|
| 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 | +}; |
0 commit comments