import generateMessages from '@cucumber/gherkin/dist/src/stream/generateMessages'; import { uuid } from '@cucumber/messages/dist/src/IdGenerator'; import fs from 'fs'; import path from 'path'; import bindGherkinSteps from './bindGherkinSteps'; import generateExampleTableSteps from './generateExampleTableSteps'; import parseGherkinExampleTables from './parseGherkinExampleTables'; import parseFeature from './parseFeature'; export default function parseGherkinSuites(cwd, feature: string, extensions: string[], cucumberSupportCode: any) { const featurePath = parseFeature(cwd, feature, extensions); const source = fs.readFileSync(featurePath, 'utf8'); const events = generateMessages(source, path.relative(cwd, featurePath), { includeSource: false, includeGherkinDocument: true, includePickles: true, newId: uuid() }); const document = events[0].gherkinDocument.feature; const hasBackground = !!document.children[0].background; const specs = hasBackground ? document.children.slice(1) : document.children; const scenarios = specs.reduce((acc, spec) => { const examples = parseGherkinExampleTables(spec.scenario.examples); return [ ...acc, ...examples.length ? generateExampleTableSteps(examples, spec.scenario) : [ { ...spec.scenario, steps: spec.scenario.steps } ] ]; }, []); const suites = scenarios.map((scenario) => ({ ...scenario, path: featurePath, steps: [ ...hasBackground ? document.children[0].background.steps : [], ...scenario.steps ] })); return { document, afterEach: cucumberSupportCode.afterTestCaseHookDefinitions, afterAll: cucumberSupportCode.afterTestRunHookDefinitions, beforeEach: cucumberSupportCode.beforeTestCaseHookDefinitions, beforeAll: cucumberSupportCode.beforeTestRunHookDefinitions, suites: suites.map((suite) => ({ ...suite, steps: bindGherkinSteps( suite.steps, cucumberSupportCode.stepDefinitions ) })) }; }