86 lines
12 KiB
JavaScript
86 lines
12 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.executeAsyncJsExpression = exports.executeJsExpression = void 0;
|
||
|
const vm_1 = require("vm");
|
||
|
const runtime_1 = require("../../errors/runtime");
|
||
|
const test_run_1 = require("../../errors/test-run");
|
||
|
const execution_context_1 = require("../../api/test-controller/execution-context");
|
||
|
const constants_1 = require("./constants");
|
||
|
const test_run_tracker_1 = __importDefault(require("../../api/test-run-tracker"));
|
||
|
// NOTE: do not beautify this code since offsets for error lines and columns are coded here
|
||
|
function wrapInAsync(expression, testRunId) {
|
||
|
return `(async function ${test_run_tracker_1.default.getMarkedFnName(testRunId)} () {\n` +
|
||
|
expression + ';\n' +
|
||
|
'});';
|
||
|
}
|
||
|
function getErrorLineColumn(err) {
|
||
|
if (err.isTestCafeError) {
|
||
|
if (!err.callsite)
|
||
|
return {};
|
||
|
if (err.callsite.id)
|
||
|
return { line: 0, column: 0 };
|
||
|
const stackFrames = err.callsite.stackFrames || [];
|
||
|
const frameIndex = err.callsite.callsiteFrameIdx;
|
||
|
const stackFrame = stackFrames[frameIndex];
|
||
|
return stackFrame ? {
|
||
|
line: stackFrame.getLineNumber(),
|
||
|
column: stackFrame.getColumnNumber(),
|
||
|
} : {};
|
||
|
}
|
||
|
const result = err.stack && err.stack.match(constants_1.ERROR_LINE_COLUMN_REGEXP);
|
||
|
if (!result)
|
||
|
return {};
|
||
|
const line = result[1] ? parseInt(result[1], 10) : void 0;
|
||
|
const column = result[2] ? parseInt(result[2], 10) : void 0;
|
||
|
return { line, column };
|
||
|
}
|
||
|
function createErrorFormattingOptions() {
|
||
|
return {
|
||
|
filename: constants_1.ERROR_FILENAME,
|
||
|
lineOffset: constants_1.ERROR_LINE_OFFSET,
|
||
|
};
|
||
|
}
|
||
|
function getExecutionContext(testController, options = execution_context_1.DEFAULT_CONTEXT_OPTIONS) {
|
||
|
const context = testController.getExecutionContext();
|
||
|
// TODO: Find a way to avoid this assignment
|
||
|
(0, execution_context_1.setContextOptions)(context, options);
|
||
|
return context;
|
||
|
}
|
||
|
function isRuntimeError(err) {
|
||
|
return err instanceof runtime_1.GeneralError ||
|
||
|
err instanceof runtime_1.TestCompilationError ||
|
||
|
err instanceof runtime_1.APIError ||
|
||
|
err instanceof runtime_1.CompositeError;
|
||
|
}
|
||
|
function executeJsExpression(expression, testRun, options) {
|
||
|
const context = getExecutionContext(testRun.controller, options);
|
||
|
const errorOptions = createErrorFormattingOptions();
|
||
|
return (0, vm_1.runInContext)(expression, context, errorOptions);
|
||
|
}
|
||
|
exports.executeJsExpression = executeJsExpression;
|
||
|
async function executeAsyncJsExpression(expression, testRun, callsite, onBeforeRaisingError) {
|
||
|
if (!expression || !expression.length)
|
||
|
return Promise.resolve();
|
||
|
const context = getExecutionContext(testRun.controller);
|
||
|
const errorOptions = createErrorFormattingOptions(expression);
|
||
|
const wrappedExpression = wrapInAsync(expression, testRun.id);
|
||
|
try {
|
||
|
return await (0, vm_1.runInContext)(wrappedExpression, context, errorOptions)();
|
||
|
}
|
||
|
catch (err) {
|
||
|
const { line, column } = getErrorLineColumn(err);
|
||
|
let resultError = null;
|
||
|
if (err.isTestCafeError || isRuntimeError(err))
|
||
|
resultError = new test_run_1.UncaughtTestCafeErrorInCustomScript(err, expression, line, column, callsite);
|
||
|
else
|
||
|
resultError = new test_run_1.UncaughtErrorInCustomScript(err, expression, line, column, callsite);
|
||
|
if (onBeforeRaisingError)
|
||
|
await onBeforeRaisingError(resultError);
|
||
|
throw resultError;
|
||
|
}
|
||
|
}
|
||
|
exports.executeAsyncJsExpression = executeAsyncJsExpression;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyI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