UNPKG

jest-test-each

Version:

run parametrised tests easily [typesafe] without text tables or arrays of arrays.

115 lines (114 loc) 4.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createTree = exports.treeWalk = void 0; const name_1 = require("./utils/name"); const createNode = (obj, maxTestNameLength, parent) => { const fullData = Object.assign(Object.assign({}, parent === null || parent === void 0 ? void 0 : parent.fullData), obj); const currentData = Object.assign({}, obj); const previousData = Object.assign({}, parent === null || parent === void 0 ? void 0 : parent.fullData); const name = name_1.getName([obj], maxTestNameLength); const node = { name: name.name, parent, fullData, currentData, previousData, tests: [], children: [], }; return node; }; const mergeNodes = (node, child, maxTestNameLength) => { const fullData = Object.assign(Object.assign({}, node.fullData), child.currentData); const currentData = Object.assign(Object.assign({}, node.currentData), child.currentData); const previousData = Object.assign({}, node.previousData); const name = name_1.getName([node.currentData, child.currentData], maxTestNameLength); return { name: name.name, parent: node.parent, fullData, currentData, previousData, tests: [...node.tests, ...child.tests], children: [...child.children], }; }; const mergeNodeAndTest = (node, test, maxTestNameLength) => { const fullData = Object.assign(Object.assign({}, node.fullData), test.partialData); const partialData = Object.assign(Object.assign({}, node.currentData), test.partialData); const name = name_1.getName([node.currentData, test.partialData], maxTestNameLength); return { name, data: fullData, partialData, }; }; const createTest = (obj, parent, maxTestNameLength) => { const name = name_1.getName([obj], maxTestNameLength); return { name, data: Object.assign(Object.assign({}, parent.fullData), obj), partialData: obj, }; }; // todo any const createTree = (levels, maxTestNameLength, errors, onEachTest) => { const root = createNode({}, maxTestNameLength); let levels2 = [...levels]; const populateNode = (parent, nextLevel = 0) => { const levelNum = nextLevel; const cases = levels2.length > 0 ? levels2[levelNum] : []; const previousData = Object.assign({}, parent.fullData); const normalized = typeof cases === 'function' ? cases(previousData) : cases; if (normalized.length === 0 && !previousData.isEmpty) { errors.push({ msg: 'every .each should have non empty cases.\nIf it is expected mark cases with "isEmpty:true"', test: typeof cases === 'function' ? `${cases}` : `${JSON.stringify(cases)}`, details: `${typeof cases === 'function' ? `each '${cases}' evaluated to an empty array.\n\tArgs {${JSON.stringify(previousData)}}` : ''}`, }); } if (levelNum === levels2.length - 1) { normalized.forEach((p) => { const test_ = createTest(p, parent, maxTestNameLength); const test = onEachTest ? onEachTest(test_) : test_; parent.tests.push(test); }); return; } normalized.forEach((p) => { var _a; const node = createNode(p, maxTestNameLength, parent); populateNode(node, levelNum + 1); if (node.children.length === 1 && node.tests.length === 0) { const newNode = mergeNodes(node, node.children[0], maxTestNameLength); node.tests = newNode.tests; node.children = newNode.children; node.previousData = newNode.previousData; node.fullData = newNode.fullData; node.currentData = newNode.currentData; node.name = newNode.name; } if (node.tests.length === 1 && node.children.length === 0) { const test = mergeNodeAndTest(node, node.tests[0], maxTestNameLength); ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.tests.push(test)) || parent.tests.push(test); return; } if (node.tests.length === 0 && node.children.length === 0) { return; } parent.children.push(node); }); }; populateNode(root); return root; }; exports.createTree = createTree; const treeWalk = (node, onEachNode, onEachTest) => { node.children.forEach((c, i) => onEachNode(c, i, () => treeWalk(c, onEachNode, onEachTest))); node.tests.forEach((t, i) => { onEachTest(t, i); }); }; exports.treeWalk = treeWalk;