@badeball/cypress-cucumber-preprocessor
Version:
[](https://github.com/badeball/cypress-cucumber-preprocessor/actions/workflows/build.yml) [ • 4.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.compile = compile;
const path_1 = __importDefault(require("path"));
const util_1 = require("util");
const gherkin_1 = require("@cucumber/gherkin");
const messages_1 = require("@cucumber/messages");
const find_cypress_specs_1 = require("find-cypress-specs");
const common_ancestor_path_1 = __importDefault(require("common-ancestor-path"));
const assertions_1 = require("./helpers/assertions");
const preprocessor_configuration_1 = require("./preprocessor-configuration");
const step_definitions_1 = require("./step-definitions");
const type_guards_1 = require("./helpers/type-guards");
const paths_1 = require("./helpers/paths");
const add_cucumber_preprocessor_plugin_1 = require("./add-cucumber-preprocessor-plugin");
const debug_1 = __importDefault(require("./helpers/debug"));
const { stringify } = JSON;
async function compile(configuration, data, uri) {
configuration = (0, add_cucumber_preprocessor_plugin_1.rebuildOriginalConfigObject)(configuration);
const options = {
includeSource: false,
includeGherkinDocument: true,
includePickles: true,
newId: messages_1.IdGenerator.uuid(),
};
const relativeUri = path_1.default.relative(configuration.projectRoot, uri);
const envelopes = (0, gherkin_1.generateMessages)(data, relativeUri, messages_1.SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN, options);
if (envelopes[0].parseError) {
throw new Error((0, assertions_1.ensure)(envelopes[0].parseError.message, "Expected parse error to have a description"));
}
const gherkinDocument = (0, assertions_1.ensure)(envelopes.map((envelope) => envelope.gherkinDocument).find(type_guards_1.notNull), "Expected to find a gherkin document amongst the envelopes.");
const pickles = envelopes.map((envelope) => envelope.pickle).filter(type_guards_1.notNull);
const implicitIntegrationFolder = (0, assertions_1.ensure)((0, common_ancestor_path_1.default)(...(0, find_cypress_specs_1.getSpecs)(configuration, "foobar", true)
.map(path_1.default.dirname)
.map(path_1.default.normalize)), "Expected to find a common ancestor path");
const preprocessor = await (0, preprocessor_configuration_1.resolve)(configuration, configuration.env, implicitIntegrationFolder);
const { stepDefinitions } = preprocessor;
(0, debug_1.default)(`resolving step definitio|ns using template(s) ${(0, util_1.inspect)(stepDefinitions)}`);
const stepDefinitionPatterns = (0, step_definitions_1.getStepDefinitionPatterns)(preprocessor, uri).map((pattern) => (0, paths_1.ensureIsRelative)(configuration.projectRoot, pattern));
(0, debug_1.default)(`for ${(0, util_1.inspect)((0, paths_1.ensureIsRelative)(configuration.projectRoot, uri))} yielded patterns ${(0, util_1.inspect)(stepDefinitionPatterns)}`);
const stepDefinitionPaths = await (0, step_definitions_1.getStepDefinitionPaths)(configuration.projectRoot, stepDefinitionPatterns);
if (stepDefinitionPaths.length === 0) {
(0, debug_1.default)("found no step definitions");
}
else {
(0, debug_1.default)(`found step definitions ${(0, util_1.inspect)(stepDefinitionPaths.map((path) => (0, paths_1.ensureIsRelative)(configuration.projectRoot, path)))}`);
}
const prepareLibPath = (...parts) => stringify(path_1.default.join(__dirname, ...parts));
const createTestsPath = prepareLibPath("browser-runtime");
const prepareRegistryPath = prepareLibPath("helpers", "prepare-registry");
const dryRun = prepareLibPath("helpers", "dry-run");
const ensureRelativeToProjectRoot = (path) => (0, paths_1.ensureIsRelative)(configuration.projectRoot, path);
const createTestsOptions = [
new Date().getTime(),
data,
gherkinDocument,
pickles,
preprocessor.isTrackingState,
preprocessor.state.softErrors,
preprocessor.omitFiltered,
{
stepDefinitions,
stepDefinitionPatterns: stepDefinitionPatterns.map(ensureRelativeToProjectRoot),
stepDefinitionPaths: stepDefinitionPaths.map(ensureRelativeToProjectRoot),
},
preprocessor.dryRun,
];
return `
${preprocessor.dryRun ? `require(${dryRun})` : ""}
const { getAndFreeRegistry } = require(${prepareRegistryPath});
const { default: createTests } = require(${createTestsPath});
${stepDefinitionPaths
.map((stepDefinition) => `require(${stringify(stepDefinition)});`)
.join("\n ")}
const registry = getAndFreeRegistry();
createTests(registry, ...${stringify(createTestsOptions)});
`;
}