UNPKG

jest-codemods

Version:

Codemods for migrating test files to Jest

66 lines (65 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isExpectSinonCall = isExpectSinonCall; exports.isExpectSinonObject = isExpectSinonObject; exports.getExpectArg = getExpectArg; exports.modifyVariableDeclaration = modifyVariableDeclaration; exports.expressionContainsProperty = expressionContainsProperty; var recast_helpers_1 = require("./recast-helpers"); function isExpectSinonCall(obj, sinonMethods) { if (obj.type === 'CallExpression' && obj.callee.name === 'expect') { var args = obj.arguments; if (args.length) { return (args[0].type === 'CallExpression' && args[0].callee.type === 'MemberExpression' && sinonMethods.includes(args[0].callee.property.name)); } return false; } else if (obj.type === 'MemberExpression') { return isExpectSinonCall(obj.object, sinonMethods); } } function isExpectSinonObject(obj, sinonMethods) { if (obj.type === 'CallExpression' && obj.callee.name === 'expect') { var args = obj.arguments; if (args.length) { return (args[0].type === 'MemberExpression' && sinonMethods.includes(args[0].property.name)); } return false; } else if (obj.type === 'MemberExpression') { return isExpectSinonObject(obj.object, sinonMethods); } } function getExpectArg(obj) { if (obj.type === 'MemberExpression') { return getExpectArg(obj.object); } else { return obj.arguments[0]; } } function modifyVariableDeclaration(nodePath, newNodePath) { var _a, _b, _c; var varDec = (0, recast_helpers_1.findParentOfType)(nodePath, 'VariableDeclaration'); if (!varDec) return; (_c = (_b = (_a = varDec.parentPath) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.forEach) === null || _c === void 0 ? void 0 : _c.call(_b, function (n, i) { if (varDec.value === n) { varDec.parentPath.value[i] = newNodePath; } }); } function expressionContainsProperty(node, memberName) { var _a; var current = node; while (current) { if (((_a = current.property) === null || _a === void 0 ? void 0 : _a.name) === memberName) { return true; } current = current.object; } return false; }