UNPKG

@badeball/cypress-cucumber-preprocessor

Version:

[![Build status](https://github.com/badeball/cypress-cucumber-preprocessor/actions/workflows/build.yml/badge.svg)](https://github.com/badeball/cypress-cucumber-preprocessor/actions/workflows/build.yml) [![Npm package weekly downloads](https://badgen.net/n

326 lines (325 loc) 17.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolve = exports.combineIntoConfiguration = exports.COMPILED_REPORTER_ENTRYPOINT = void 0; const cosmiconfig_1 = require("cosmiconfig"); const util_1 = __importDefault(require("util")); const debug_1 = __importDefault(require("./helpers/debug")); const paths_1 = require("./helpers/paths"); const type_guards_1 = require("./helpers/type-guards"); function hasOwnProperty(value, property) { return Object.prototype.hasOwnProperty.call(value, property); } function isPlainObject(value) { return (value === null || value === void 0 ? void 0 : value.constructor) === Object; } function validateUserConfigurationEntry(key, value) { switch (key) { case "stepDefinitions": if (!(0, type_guards_1.isStringOrStringArray)(value)) { throw new Error(`Expected a string or array of strings (stepDefinitions), but got ${util_1.default.inspect(value)}`); } return { [key]: value }; case "messages": { if (typeof value !== "object" || value == null) { throw new Error(`Expected an object (messages), but got ${util_1.default.inspect(value)}`); } if (!hasOwnProperty(value, "enabled") || typeof value.enabled !== "boolean") { throw new Error(`Expected a boolean (messages.enabled), but got ${util_1.default.inspect(value.enabled)}`); } let output; if (hasOwnProperty(value, "output")) { if ((0, type_guards_1.isString)(value.output)) { output = value.output; } else { throw new Error(`Expected a string (messages.output), but got ${util_1.default.inspect(value.output)}`); } } const messagesConfig = { enabled: value.enabled, output, }; return { [key]: messagesConfig }; } case "json": { if (typeof value !== "object" || value == null) { throw new Error(`Expected an object (json), but got ${util_1.default.inspect(value)}`); } if (!hasOwnProperty(value, "enabled") || typeof value.enabled !== "boolean") { throw new Error(`Expected a boolean (json.enabled), but got ${util_1.default.inspect(value.enabled)}`); } let output; if (hasOwnProperty(value, "output")) { if ((0, type_guards_1.isString)(value.output)) { output = value.output; } else { throw new Error(`Expected a string (json.output), but got ${util_1.default.inspect(value.output)}`); } } const messagesConfig = { enabled: value.enabled, output, }; return { [key]: messagesConfig }; } case "html": { if (typeof value !== "object" || value == null) { throw new Error(`Expected an object (html), but got ${util_1.default.inspect(value)}`); } if (!hasOwnProperty(value, "enabled") || typeof value.enabled !== "boolean") { throw new Error(`Expected a boolean (html.enabled), but got ${util_1.default.inspect(value.enabled)}`); } let output; if (hasOwnProperty(value, "output")) { if ((0, type_guards_1.isString)(value.output)) { output = value.output; } else { throw new Error(`Expected a string (html.output), but got ${util_1.default.inspect(value.output)}`); } } const messagesConfig = { enabled: value.enabled, output, }; return { [key]: messagesConfig }; } case "pretty": { if (typeof value !== "object" || value == null) { throw new Error(`Expected an object (pretty), but got ${util_1.default.inspect(value)}`); } if (!hasOwnProperty(value, "enabled") || typeof value.enabled !== "boolean") { throw new Error(`Expected a boolean (pretty.enabled), but got ${util_1.default.inspect(value.enabled)}`); } const prettyConfig = { enabled: value.enabled, }; return { [key]: prettyConfig }; } case "filterSpecs": { if (!(0, type_guards_1.isBoolean)(value)) { throw new Error(`Expected a boolean (filterSpecs), but got ${util_1.default.inspect(value)}`); } return { [key]: value }; } case "omitFiltered": { if (!(0, type_guards_1.isBoolean)(value)) { throw new Error(`Expected a boolean (omitFiltered), but got ${util_1.default.inspect(value)}`); } return { [key]: value }; } case "e2e": return { [key]: validateUserConfiguration(value) }; case "component": return { [key]: validateUserConfiguration(value) }; default: return {}; } } function validateUserConfiguration(configuration) { if (!isPlainObject(configuration)) { throw new Error(`Malformed configuration, expected an object, but got ${util_1.default.inspect(configuration)}`); } return Object.assign({}, ...Object.entries(configuration).map((entry) => validateUserConfigurationEntry(...entry))); } function validateEnvironmentOverrides(environment) { const overrides = {}; if (hasOwnProperty(environment, "stepDefinitions")) { const { stepDefinitions } = environment; if ((0, type_guards_1.isStringOrStringArray)(stepDefinitions)) { overrides.stepDefinitions = stepDefinitions; } else { throw new Error(`Expected a string or array of strings (stepDefinitions), but got ${util_1.default.inspect(stepDefinitions)}`); } } if (hasOwnProperty(environment, "messagesEnabled")) { const { messagesEnabled } = environment; if ((0, type_guards_1.isBoolean)(messagesEnabled)) { overrides.messagesEnabled = messagesEnabled; } else if ((0, type_guards_1.isString)(messagesEnabled)) { overrides.messagesEnabled = stringToMaybeBoolean(messagesEnabled); } else { throw new Error(`Expected a boolean (messagesEnabled), but got ${util_1.default.inspect(messagesEnabled)}`); } } if (hasOwnProperty(environment, "messagesOutput")) { const { messagesOutput } = environment; if ((0, type_guards_1.isString)(messagesOutput)) { overrides.messagesOutput = messagesOutput; } else { throw new Error(`Expected a string (messagesOutput), but got ${util_1.default.inspect(messagesOutput)}`); } } if (hasOwnProperty(environment, "jsonEnabled")) { const { jsonEnabled } = environment; if ((0, type_guards_1.isBoolean)(jsonEnabled)) { overrides.jsonEnabled = jsonEnabled; } else if ((0, type_guards_1.isString)(jsonEnabled)) { overrides.jsonEnabled = stringToMaybeBoolean(jsonEnabled); } else { throw new Error(`Expected a boolean (jsonEnabled), but got ${util_1.default.inspect(jsonEnabled)}`); } } if (hasOwnProperty(environment, "jsonOutput")) { const { jsonOutput } = environment; if ((0, type_guards_1.isString)(jsonOutput)) { overrides.jsonOutput = jsonOutput; } else { throw new Error(`Expected a string (jsonOutput), but got ${util_1.default.inspect(jsonOutput)}`); } } if (hasOwnProperty(environment, "htmlEnabled")) { const { htmlEnabled } = environment; if ((0, type_guards_1.isBoolean)(htmlEnabled)) { overrides.htmlEnabled = htmlEnabled; } else if ((0, type_guards_1.isString)(htmlEnabled)) { overrides.htmlEnabled = stringToMaybeBoolean(htmlEnabled); } else { throw new Error(`Expected a boolean (htmlEnabled), but got ${util_1.default.inspect(htmlEnabled)}`); } } if (hasOwnProperty(environment, "htmlOutput")) { const { htmlOutput } = environment; if ((0, type_guards_1.isString)(htmlOutput)) { overrides.htmlOutput = htmlOutput; } else { throw new Error(`Expected a string (htmlOutput), but got ${util_1.default.inspect(htmlOutput)}`); } } if (hasOwnProperty(environment, "prettyEnabled")) { const { prettyEnabled } = environment; if ((0, type_guards_1.isBoolean)(prettyEnabled)) { overrides.prettyEnabled = prettyEnabled; } else if ((0, type_guards_1.isString)(prettyEnabled)) { overrides.prettyEnabled = stringToMaybeBoolean(prettyEnabled); } else { throw new Error(`Expected a boolean (prettyEnabled), but got ${util_1.default.inspect(prettyEnabled)}`); } } if (hasOwnProperty(environment, "filterSpecs")) { const { filterSpecs } = environment; if ((0, type_guards_1.isBoolean)(filterSpecs)) { overrides.filterSpecs = filterSpecs; } else if ((0, type_guards_1.isString)(filterSpecs)) { overrides.filterSpecs = stringToMaybeBoolean(filterSpecs); } else { throw new Error(`Expected a boolean (filterSpecs), but got ${util_1.default.inspect(filterSpecs)}`); } } if (hasOwnProperty(environment, "omitFiltered")) { const { omitFiltered } = environment; if ((0, type_guards_1.isBoolean)(omitFiltered)) { overrides.omitFiltered = omitFiltered; } else if ((0, type_guards_1.isString)(omitFiltered)) { overrides.omitFiltered = stringToMaybeBoolean(omitFiltered); } else { throw new Error(`Expected a boolean (omitFiltered), but got ${util_1.default.inspect(omitFiltered)}`); } } return overrides; } function stringToMaybeBoolean(value) { if (value === "") { return; } const falsyValues = ["0", "false"]; if (falsyValues.includes(value)) { return false; } else { return true; } } const DEFAULT_STEP_DEFINITIONS = [ "[integration-directory]/[filepath]/**/*.{js,mjs,ts,tsx}", "[integration-directory]/[filepath].{js,mjs,ts,tsx}", "cypress/support/step_definitions/**/*.{js,mjs,ts,tsx}", ]; exports.COMPILED_REPORTER_ENTRYPOINT = "dist/subpath-entrypoints/pretty-reporter.js"; function combineIntoConfiguration(configuration, overrides, cypress, implicitIntegrationFolder) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19; const defaultStepDefinitions = DEFAULT_STEP_DEFINITIONS.map((pattern) => pattern.replace("[integration-directory]", (0, paths_1.ensureIsRelative)(cypress.projectRoot, implicitIntegrationFolder))); const specific = configuration[cypress.testingType]; const unspecific = configuration; const stepDefinitions = (_c = (_b = (_a = overrides.stepDefinitions) !== null && _a !== void 0 ? _a : specific === null || specific === void 0 ? void 0 : specific.stepDefinitions) !== null && _b !== void 0 ? _b : unspecific.stepDefinitions) !== null && _c !== void 0 ? _c : defaultStepDefinitions; const json = { enabled: (_h = (_f = (_d = overrides.jsonEnabled) !== null && _d !== void 0 ? _d : (_e = specific === null || specific === void 0 ? void 0 : specific.json) === null || _e === void 0 ? void 0 : _e.enabled) !== null && _f !== void 0 ? _f : (_g = unspecific.json) === null || _g === void 0 ? void 0 : _g.enabled) !== null && _h !== void 0 ? _h : false, output: (_o = (_l = (_j = overrides.jsonOutput) !== null && _j !== void 0 ? _j : (_k = specific === null || specific === void 0 ? void 0 : specific.json) === null || _k === void 0 ? void 0 : _k.output) !== null && _l !== void 0 ? _l : (_m = unspecific.json) === null || _m === void 0 ? void 0 : _m.output) !== null && _o !== void 0 ? _o : "cucumber-report.json", }; const html = { enabled: (_t = (_r = (_p = overrides.htmlEnabled) !== null && _p !== void 0 ? _p : (_q = specific === null || specific === void 0 ? void 0 : specific.html) === null || _q === void 0 ? void 0 : _q.enabled) !== null && _r !== void 0 ? _r : (_s = unspecific.html) === null || _s === void 0 ? void 0 : _s.enabled) !== null && _t !== void 0 ? _t : false, output: (_y = (_w = (_u = overrides.htmlOutput) !== null && _u !== void 0 ? _u : (_v = specific === null || specific === void 0 ? void 0 : specific.html) === null || _v === void 0 ? void 0 : _v.output) !== null && _w !== void 0 ? _w : (_x = unspecific.html) === null || _x === void 0 ? void 0 : _x.output) !== null && _y !== void 0 ? _y : "cucumber-report.html", }; const messages = { enabled: json.enabled || html.enabled || ((_3 = (_1 = (_z = overrides.messagesEnabled) !== null && _z !== void 0 ? _z : (_0 = specific === null || specific === void 0 ? void 0 : specific.messages) === null || _0 === void 0 ? void 0 : _0.enabled) !== null && _1 !== void 0 ? _1 : (_2 = unspecific.messages) === null || _2 === void 0 ? void 0 : _2.enabled) !== null && _3 !== void 0 ? _3 : false), output: (_8 = (_6 = (_4 = overrides.messagesOutput) !== null && _4 !== void 0 ? _4 : (_5 = specific === null || specific === void 0 ? void 0 : specific.messages) === null || _5 === void 0 ? void 0 : _5.output) !== null && _6 !== void 0 ? _6 : (_7 = unspecific.messages) === null || _7 === void 0 ? void 0 : _7.output) !== null && _8 !== void 0 ? _8 : "cucumber-messages.ndjson", }; const pretty = { enabled: (_13 = (_11 = (_9 = overrides.prettyEnabled) !== null && _9 !== void 0 ? _9 : (_10 = specific === null || specific === void 0 ? void 0 : specific.pretty) === null || _10 === void 0 ? void 0 : _10.enabled) !== null && _11 !== void 0 ? _11 : (_12 = unspecific.pretty) === null || _12 === void 0 ? void 0 : _12.enabled) !== null && _13 !== void 0 ? _13 : false, }; const filterSpecs = (_16 = (_15 = (_14 = overrides.filterSpecs) !== null && _14 !== void 0 ? _14 : specific === null || specific === void 0 ? void 0 : specific.filterSpecs) !== null && _15 !== void 0 ? _15 : unspecific.filterSpecs) !== null && _16 !== void 0 ? _16 : false; const omitFiltered = (_19 = (_18 = (_17 = overrides.omitFiltered) !== null && _17 !== void 0 ? _17 : specific === null || specific === void 0 ? void 0 : specific.omitFiltered) !== null && _18 !== void 0 ? _18 : unspecific.omitFiltered) !== null && _19 !== void 0 ? _19 : false; return { stepDefinitions, messages, json, html, pretty, filterSpecs, omitFiltered, implicitIntegrationFolder, }; } exports.combineIntoConfiguration = combineIntoConfiguration; async function cosmiconfigResolver(projectRoot) { const result = await (0, cosmiconfig_1.cosmiconfig)("cypress-cucumber-preprocessor").search(projectRoot); return result === null || result === void 0 ? void 0 : result.config; } async function resolve(cypressConfig, environment, implicitIntegrationFolder, configurationFileResolver = cosmiconfigResolver) { const result = await configurationFileResolver(cypressConfig.projectRoot); const environmentOverrides = validateEnvironmentOverrides(environment); (0, debug_1.default)(`resolved environment overrides ${util_1.default.inspect(environmentOverrides)}`); let explicitConfiguration; if (result) { explicitConfiguration = validateUserConfiguration(result); (0, debug_1.default)(`resolved explicit user configuration ${util_1.default.inspect(explicitConfiguration)}`); } else { explicitConfiguration = {}; (0, debug_1.default)("resolved no explicit user configuration"); } const configuration = combineIntoConfiguration(explicitConfiguration, environmentOverrides, cypressConfig, implicitIntegrationFolder); if (cypressConfig.reporter.endsWith(exports.COMPILED_REPORTER_ENTRYPOINT)) { (0, debug_1.default)("detected use of @badeball/cypress-cucumber-preprocessor/pretty-reporter, enabling pretty output"); configuration.pretty.enabled = true; } (0, debug_1.default)(`resolved configuration ${util_1.default.inspect(configuration)}`); return configuration; } exports.resolve = resolve;