UNPKG

jest-bdd-generator

Version:

Jest code generator empowered with Gherkin and BDD

228 lines (227 loc) 8.43 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var JestToGherkin_exports = {}; __export(JestToGherkin_exports, { JestToGherkin: () => JestToGherkin }); module.exports = __toCommonJS(JestToGherkin_exports); var import_typescript = __toESM(require("typescript")); var import_transpiler = require("../transpiler"); function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class JestToGherkin extends import_transpiler.Transpile { outputCode() { this.output = this.output.sort((a, b) => { const startA = a.pos.start; const startB = b.pos.start; if (startA.line < startB.line) { return -1; } else if (startA.line === startB.line) { return startA.character < startB.character ? -1 : 1; } else { return 1; } }); const fileHeadJest = []; this.output.reduce((prevstep, step, idx, steps) => { let displayKey = "", displayValue = ""; switch (step.key) { case "Feature": displayKey = "\nFeature: "; displayValue = step.value; break; case "Scenario": displayKey = ` Scenario `; if (step.examples) { displayKey += `Outline: `; } displayValue = step.value; break; case "Given": case "Then": case "When": displayKey = ` ${(prevstep === null || prevstep === void 0 ? void 0 : prevstep.key) === step.key ? "And" : step.key} `; displayValue = step.value; break; case "Examples": const MIN_WITH = 8; const examples = step.examples; const header = Object.keys(examples[0]); const widthsCols = new Array(header.length); widthsCols.fill(MIN_WITH); const getColMaxWidth = (val, i) => { widthsCols[i] = Math.max(widthsCols[i], val.length); widthsCols[i] = Math.ceil(widthsCols[i] / MIN_WITH) * MIN_WITH; return val; }; const padding = (str, i) => { const spacesEachSide = (widthsCols[i] - str.length) / 2; const paddingLefts = new Array(Math.ceil(spacesEachSide)).fill(" ").join(""); const paddingRights = new Array(Math.floor(spacesEachSide)).fill(" ").join(""); return `${paddingLefts}${str}${paddingRights} `; }; const table = [ header.map((title, i) => getColMaxWidth(title, i)), ...examples.map((val) => header.map((colname, i) => getColMaxWidth(val[colname].toString(), i))) ].map((row) => row.map(padding).join("|")).join("\n"); displayKey = `Examples: `; displayValue = table; break; default: } fileHeadJest.push(`${displayKey}${displayValue}`); return step; }, {}); return `${fileHeadJest.join("\n")}`; } constructor() { var _searchDescribeFunction_arguments__searchCallSchema; super(); _define_property(this, "fileName", ""); const searchTestFunction = { keyword: "test", chain: [ { keyword: "each", arguments: [ { callback: (nodeExamples, call, ctx) => { const examples = nodeExamples.elements.map((nodeObj) => { const ret = {}; if (import_typescript.default.isObjectLiteralExpression(nodeObj)) { nodeObj.properties.forEach((nodeProp) => { if (import_typescript.default.isPropertyAssignment(nodeProp)) { const rawLiteral = nodeProp.initializer.getText(); let keyName = nodeProp.name.getText(); if (import_typescript.default.isStringLiteral(nodeProp.name)) { keyName = keyName.substring(1, keyName.length - 1); } if (import_typescript.default.isNumericLiteral(nodeProp.initializer)) { ret[keyName] = Number(rawLiteral); } else if (import_typescript.default.isPrefixUnaryExpression(nodeProp.initializer)) { ret[keyName] = -Number(nodeProp.initializer.operand.getFullText()); } else if (import_typescript.default.isStringLiteral(nodeProp.initializer)) { ret[keyName] = rawLiteral.substring(1, rawLiteral.length - 1); } else { ret[keyName] = rawLiteral; } } }); } return ret; }); this.output.push({ key: "Examples", value: call.parent.arguments[0].text, examples, pos: { start: this.parsePosition(call.pos), end: this.parsePosition(call.end) } }); } } ] } ], arguments: [ { callback: this.callbackOnStringArgumentFactory("Scenario") }, { callback: this.callbackOnFnArgument, searchCallSchema: [ { keyword: "expect", arguments: [ { callback: (node, call, ctx) => { this.output.push({ key: "Then", value: call.parent.getText(), pos: { start: this.parsePosition(call.pos), end: this.parsePosition(call.end) } }); return node; } } ] } ] } ] }; const searchDescribeFunction = { keyword: "describe", arguments: [ { callback: this.callbackOnStringArgumentFactory("Feature") }, { searchCallSchema: [ { keyword: "test", chain: searchTestFunction.chain, arguments: searchTestFunction.arguments }, { keyword: "it", chain: searchTestFunction.chain, arguments: searchTestFunction.arguments } ] } ] }; (_searchDescribeFunction_arguments__searchCallSchema = searchDescribeFunction.arguments[1].searchCallSchema) === null || _searchDescribeFunction_arguments__searchCallSchema === void 0 ? void 0 : _searchDescribeFunction_arguments__searchCallSchema.push(searchDescribeFunction); this.searchSchema = [ searchDescribeFunction ]; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { JestToGherkin }); //# sourceMappingURL=JestToGherkin.js.map