@11joselu/cypress-to-playwright
Version:
Automatic migration from cypress to playwright
62 lines • 3.05 kB
JavaScript
import ts from 'typescript';
import { isHook } from '../is/is-hook.js';
export const handle = (expressionName, node, factory) => {
const call = node.expression;
if (isHook.it(expressionName)) {
return createHookWithTitle(expressionName, call, node, factory, "test" /* HOOKS.TEST */);
}
if (isHook.describe(expressionName)) {
return createHookWithTitle(expressionName, call, node, factory, "describe" /* HOOKS.DESCRIBE */);
}
if (isHook.beforeEach(expressionName)) {
return createHookWithoutTitle(factory, call, "beforeEach" /* HOOKS.BEFORE_EACH */);
}
if (isHook.afterEach(expressionName)) {
return createHookWithoutTitle(factory, call, "afterEach" /* HOOKS.AFTER_EACH */);
}
return node;
};
function createHookWithTitle(expressionName, call, node, factory, hook) {
if (isHook.it(expressionName)) {
let expression;
if (isHook.isItSkipOrOnly(expressionName)) {
if (!ts.isPropertyAccessExpression(call.expression))
return node;
expression = factory.propertyAccessExpression(hook, call.expression.name);
}
else {
expression = factory.identifier(hook);
}
const [title] = call.arguments;
return factory.callExpressionStatement(expression, factory.callExpression(call.expression, call.typeArguments, [
title,
factory.arrowFunction(getBodyOfCall(call, factory), [factory.destructuringParameter('page')], [factory.asyncToken()]),
]));
}
let expression = factory.propertyAccessExpression("test" /* HOOKS.TEST */, "describe" /* HOOKS.DESCRIBE */);
if (isHook.isDescribeSkipOrOnly(expressionName)) {
if (!ts.isPropertyAccessExpression(call.expression))
return node;
expression = factory.propertyAccessExpression(factory.propertyAccessExpression("test" /* HOOKS.TEST */, "describe" /* HOOKS.DESCRIBE */), call.expression.name);
}
const [title] = call.arguments;
return factory.callExpressionStatement(expression, factory.callExpression(call.expression, call.typeArguments, [
title,
factory.arrowFunction(getBodyOfCall(call, factory), [], []),
]));
}
function getBodyOfCall(callExpression, factory) {
const callbackArgument = callExpression.arguments.find((arg) => ts.isFunctionLike(arg));
const foundCallback = callbackArgument ? callbackArgument : undefined;
if (foundCallback === null || foundCallback === void 0 ? void 0 : foundCallback.body) {
return foundCallback.body;
}
return factory.emptyBlock();
}
function createHookWithoutTitle(factory, call, hook = "beforeEach" /* HOOKS.BEFORE_EACH */) {
const testCallExpression = factory.propertyAccessExpression("test" /* HOOKS.TEST */, hook);
return factory.callExpression(testCallExpression, call.typeArguments, [
factory.arrowFunction(getBodyOfCall(call, factory), [factory.destructuringParameter('page')], [factory.asyncToken()]),
]);
}
//# sourceMappingURL=hooks.js.map