@11joselu/cypress-to-playwright
Version:
Automatic migration from cypress to playwright
181 lines • 7.86 kB
JavaScript
import ts from 'typescript';
import { nodeFactory } from './node-factory.js';
import { PLAYWRIGHT_PAGE_NAME } from '../playwright.js';
import { isCy } from '../is/is-cy.js';
import { isHook } from '../is/is-hook.js';
import * as hook from './hooks.js';
import * as actions from './actions.js';
import * as validations from './validations.js';
import * as customCommand from './custom-command.js';
export function transform(sourceFile, customCommandsTracker) {
return (context) => {
const factory = nodeFactory(context.factory);
return (rootNode) => {
function visit(node) {
var _a;
node = ts.visitEachChild(node, visit, context);
if (isFunction(node)) {
return convertFunctionNode(node, sourceFile, factory);
}
if (!(ts.isExpressionStatement(node) && ts.isCallExpression(node.expression))) {
return node;
}
const call = node.expression;
const expressionName = getExpressionName(call);
if (isRunnerHook(expressionName)) {
return hook.handle(expressionName, node, factory);
}
if (isCy.customCommand(expressionName)) {
const newNode = customCommand.handle(node, factory);
customCommandsTracker.track(((_a = newNode.name) === null || _a === void 0 ? void 0 : _a.escapedText) || '');
return newNode;
}
const foundFunctionDeclaration = sourceFile.statements.find((st) => {
var _a;
if (isFunction(st)) {
if (ts.isVariableStatement(st) && st.declarationList.declarations[0]) {
return st.declarationList.declarations[0].name.getText() === call.expression.getText();
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return ((_a = st.name) === null || _a === void 0 ? void 0 : _a.getText()) === call.expression.getText();
}
return false;
});
if (foundFunctionDeclaration) {
return injectPageArgumentIntoCallFunction(foundFunctionDeclaration, sourceFile, factory, call, node);
}
if (!isCyPropertyCall(expressionName, call))
return node;
if (isAction(expressionName)) {
return actions.handle(expressionName, call.expression, factory);
}
if (isValidation(expressionName)) {
return validations.handle(call, factory) || node;
}
if (isCommand(expressionName)) {
return commands(expressionName, factory, call) || node;
}
if (customCommandsTracker.exists(expressionName)) {
if (ts.isCallExpression(node.expression)) {
const expression = call.expression;
return factory.callExpression(expression.name, undefined, [factory.identifier(PLAYWRIGHT_PAGE_NAME)]);
}
}
return node;
}
return ts.visitNode(rootNode, visit);
};
};
}
function getExpressionName(expressions) {
return getListOfExpressionName(expressions).reverse().join('.');
}
function createGoTo(factory, call) {
return factory.awaitCallExpression(factory.playwrightCommand("goto" /* COMMANDS.GOTO */), call.typeArguments, call.arguments);
}
function createWait(factory, call) {
return factory.awaitCallExpression(factory.playwrightCommand("waitForTimeout" /* COMMANDS.WAIT */), call.typeArguments, call.arguments);
}
function createClearCookies(factory, call) {
const expression = factory.propertyAccessExpression(factory.callExpression(factory.playwrightCommand("context" /* COMMANDS.CONTEXT */), call.typeArguments, call.arguments), "clearCookies" /* COMMANDS.CLEAR_COOKIES */);
return factory.awaitCallExpression(expression, undefined, []);
}
function getListOfExpressionName(expression) {
const result = [];
if ('name' in expression) {
result.push(expression.name.escapedText.toString());
}
if ('escapedText' in expression) {
result.push(expression.escapedText);
}
if ('expression' in expression) {
result.push(...getListOfExpressionName(expression.expression));
}
return result;
}
function isRunnerHook(expressionName) {
return (isHook.beforeEach(expressionName) ||
isHook.it(expressionName) ||
isHook.afterEach(expressionName) ||
isHook.describe(expressionName));
}
function isCyPropertyCall(expressionName, call) {
return isCy.startWithCy(expressionName) && ts.isPropertyAccessExpression(call.expression);
}
function isAction(expressionName) {
return (isCy.click(expressionName) ||
isCy.type(expressionName) ||
isCy.check(expressionName) ||
isCy.uncheck(expressionName) ||
isCy.select(expressionName) ||
isCy.scrollTo(expressionName) ||
isCy.scrollIntoView(expressionName) ||
isCy.dblclick(expressionName) ||
isCy.clear(expressionName) ||
isCy.focus(expressionName) ||
isCy.blur(expressionName));
}
function isValidation(expressionName) {
return isCy.should(expressionName);
}
function isCommand(expressionName) {
return (isCy.visit(expressionName) ||
isCy.intercept(expressionName) ||
isCy.wait(expressionName) ||
isCy.clearCookies(expressionName));
}
function commands(expressionName, factory, call) {
if (isCy.visit(expressionName)) {
return createGoTo(factory, call);
}
else if (isCy.intercept(expressionName)) {
return factory.playwrightIntercept(call);
}
else if (isCy.wait(expressionName)) {
return createWait(factory, call);
}
else if (isCy.clearCookies(expressionName)) {
return createClearCookies(factory, call);
}
return null;
}
function includesCyCodeInFnCode(fnBodyContent) {
return fnBodyContent.includes('cy.');
}
function isFunction(node) {
const isArrowFunctionDeclaration = (node) => ts.isVariableDeclaration(node) &&
node.initializer &&
ts.isArrowFunction(node.initializer);
return (ts.isFunctionDeclaration(node) ||
isArrowFunctionDeclaration(node) ||
(ts.isVariableStatement(node) && isArrowFunctionDeclaration(node.declarationList.declarations[0])));
}
function convertFunctionNode(node, sourceFile, factory) {
if (ts.isFunctionDeclaration(node)) {
const fnBodyContent = node.getFullText(sourceFile);
if (includesCyCodeInFnCode(fnBodyContent)) {
return factory.functionWithPageParameter(node);
}
}
if (ts.isVariableDeclaration(node) &&
ts.isArrowFunction(node.initializer)) {
const arrowBodyContent = node.getFullText(sourceFile);
if (includesCyCodeInFnCode(arrowBodyContent)) {
return factory.functionWithPageParameter(node);
}
}
return node;
}
function injectPageArgumentIntoCallFunction(foundFunctionDeclaration, sourceFile, factory, call, node) {
if (isFunction(foundFunctionDeclaration)) {
const fnBodyContent = foundFunctionDeclaration.getFullText(sourceFile);
if (includesCyCodeInFnCode(fnBodyContent)) {
return factory.await(factory.callExpression(factory.identifier(call.expression.getText()), call.typeArguments, [
factory.identifier(PLAYWRIGHT_PAGE_NAME),
]));
}
}
return node;
}
//# sourceMappingURL=transform.js.map