Created
December 2, 2019 09:56
-
-
Save chilldriven/d3b3c1c7cb23c983478328028ac3933e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function transformer(file, api) { | |
const AST = api.jscodeshift(file.source); | |
const { | |
identifier, | |
callExpression, | |
memberExpression, | |
CallExpression, | |
ImportDeclaration, | |
importDeclaration, | |
importSpecifier, | |
literal | |
} = api.jscodeshift; | |
const imports = AST.find(ImportDeclaration, { | |
source: { value: "enzyme" } | |
}); | |
if (imports.length < 1) return AST.toSource(); | |
imports.replaceWith( | |
importDeclaration( | |
[importSpecifier(identifier("render"))], | |
literal("@testing-library/react") | |
) | |
); | |
function getProgram(p) { | |
if (p.name === "program") { | |
return true; | |
} | |
return getProgram(p.parent); | |
} | |
function replaceFunctionCall(name) { | |
AST.find(CallExpression, { callee: { name } }) | |
.filter(p => getProgram(p) === getProgram(imports.get())) | |
.replaceWith(p => | |
memberExpression( | |
callExpression(identifier("render"), p.node.arguments), | |
callExpression(identifier("asFragment"), []), | |
false | |
) | |
); | |
} | |
for (const id of ["shallow", "mount", "render"]) { | |
replaceFunctionCall(id); | |
} | |
return AST.toSource(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment