Skip to content

Instantly share code, notes, and snippets.

@chilldriven
Created December 2, 2019 09:56
Show Gist options
  • Save chilldriven/d3b3c1c7cb23c983478328028ac3933e to your computer and use it in GitHub Desktop.
Save chilldriven/d3b3c1c7cb23c983478328028ac3933e to your computer and use it in GitHub Desktop.
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