89 lines
12 KiB
JavaScript
89 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 });
|
|
const events_1 = require("events");
|
|
const delay_1 = __importDefault(require("../utils/delay"));
|
|
const thennable_1 = require("../utils/thennable");
|
|
const test_run_1 = require("../errors/test-run");
|
|
const re_executable_promise_1 = __importDefault(require("../utils/re-executable-promise"));
|
|
const get_fn_1 = __importDefault(require("./get-fn"));
|
|
const marker_1 = require("../services/serialization/replicator/transforms/function-marker-transform/marker");
|
|
const marker_2 = require("../services/serialization/replicator/transforms/promise-marker-transform/marker");
|
|
const ASSERTION_DELAY = 200;
|
|
class AssertionExecutor extends events_1.EventEmitter {
|
|
constructor(command, timeout, callsite) {
|
|
super();
|
|
this.command = command;
|
|
this.timeout = timeout;
|
|
this.callsite = callsite;
|
|
this.startTime = null;
|
|
this.passed = false;
|
|
this.inRetry = false;
|
|
const fn = (0, get_fn_1.default)(this.command);
|
|
const actualCommand = this.command.actual;
|
|
if (actualCommand instanceof re_executable_promise_1.default)
|
|
this.fn = this._wrapFunction(fn);
|
|
else if (!this.command.options.allowUnawaitedPromise && this._isPromise(actualCommand))
|
|
throw new test_run_1.AssertionUnawaitedPromiseError(this.callsite);
|
|
else
|
|
this.fn = fn;
|
|
}
|
|
_isPromise(val) {
|
|
return (0, thennable_1.isThennable)(val) ||
|
|
val === Symbol.for(marker_2.PROMISE_MARKER_DESCRIPTION);
|
|
}
|
|
_getTimeLeft() {
|
|
const executionTime = new Date().getTime() - this.startTime; // eslint-disable-line @typescript-eslint/no-extra-parens
|
|
return this.timeout - executionTime;
|
|
}
|
|
_onExecutionFinished() {
|
|
if (this.inRetry)
|
|
this.emit('end-assertion-retries', this.passed);
|
|
}
|
|
_wrapFunction(fn) {
|
|
return async () => {
|
|
const resultPromise = this.command.actual;
|
|
while (!this.passed) {
|
|
this.command.actual = await resultPromise._reExecute();
|
|
try {
|
|
fn();
|
|
this.passed = true;
|
|
this._onExecutionFinished();
|
|
}
|
|
catch (err) {
|
|
if (this._getTimeLeft() <= 0) {
|
|
this._onExecutionFinished();
|
|
throw err;
|
|
}
|
|
await (0, delay_1.default)(ASSERTION_DELAY);
|
|
this.inRetry = true;
|
|
this.emit('start-assertion-retries', this._getTimeLeft());
|
|
}
|
|
}
|
|
};
|
|
}
|
|
_onBeforeRun() {
|
|
if (this.command.actual !== Symbol.for(marker_1.FUNCTION_MARKER_DESCRIPTION))
|
|
return;
|
|
this.emit('non-serializable-actual-value', this);
|
|
}
|
|
async run() {
|
|
this._onBeforeRun();
|
|
this.startTime = new Date().getTime();
|
|
try {
|
|
await this.fn();
|
|
}
|
|
catch (err) {
|
|
if (err.name === 'AssertionError' || err.constructor.name === 'AssertionError')
|
|
throw new test_run_1.ExternalAssertionLibraryError(err, this.callsite);
|
|
if (err.isTestCafeError)
|
|
err.callsite = this.callsite;
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
exports.default = AssertionExecutor;
|
|
module.exports = exports.default;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXhlY3V0b3IuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvYXNzZXJ0aW9ucy9leGVjdXRvci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLG1DQUFzQztBQUN0QywyREFBbUM7QUFDbkMsa0RBQWlEO0FBQ2pELGlEQUFtRztBQUNuRywyRkFBaUU7QUFDakUsc0RBQTZCO0FBRzdCLDZHQUErSDtBQUMvSCw0R0FBNkg7QUFFN0gsTUFBTSxlQUFlLEdBQUcsR0FBRyxDQUFDO0FBRTVCLE1BQXFCLGlCQUFrQixTQUFRLHFCQUFZO0lBU3ZELFlBQW9CLE9BQXlCLEVBQUUsT0FBZSxFQUFFLFFBQXdCO1FBQ3BGLEt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dhaXQgcmVzdWx0UHJvbWlzZS5fcmVFeGVjdXRlKCk7XG5cbiAgICAgICAgICAgICAgICB0cnkge1xuICAgICAgICAgICAgICAgICAgICBmbigpO1xuICAgICAgICAgICAgICAgICAgICB0aGlzLnBhc3NlZCA9IHRydWU7XG4gICAgICAgICAgICAgICAgICAgIHRoaXMuX29uRXhlY3V0aW9uRmluaXNoZWQoKTtcbiAgICAgICAgICAgICAgICB9XG5cbiAgICAgICAgICAgICAgICBjYXRjaCAoZXJyKSB7XG4gICAgICAgICAgICAgICAgICAgIGlmICh0aGlzLl9nZXRUaW1lTGVmdCgpIDw9IDApIHtcbiAgICAgICAgICAgICAgICAgICAgICAgIHRoaXMuX29uRXhlY3V0aW9uRmluaXNoZWQoKTtcbiAgICAgICAgICAgICAgICAgICAgICAgIHRocm93IGVycjtcbiAgICAgICAgICAgICAgICAgICAgfVxuXG4gICAgICAgICAgICAgICAgICAgIGF3YWl0IGRlbGF5KEFTU0VSVElPTl9ERUxBWSk7XG5cbiAgICAgICAgICAgICAgICAgICAgdGhpcy5pblJldHJ5ID0gdHJ1ZTtcbiAgICAgICAgICAgICAgICAgICAgdGhpcy5lbWl0KCdzdGFydC1hc3NlcnRpb24tcmV0cmllcycsIHRoaXMuX2dldFRpbWVMZWZ0KCkpO1xuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH1cbiAgICAgICAgfTtcbiAgICB9XG5cbiAgICBwcml2YXRlIF9vbkJlZm9yZVJ1biAoKTogdm9pZCB7XG4gICAgICAgIGlmICh0aGlzLmNvbW1hbmQuYWN0dWFsICE9PSBTeW1ib2wuZm9yKEZVTkNUSU9OX01BUktFUl9ERVNDUklQVElPTikpXG4gICAgICAgICAgICByZXR1cm47XG5cbiAgICAgICAgdGhpcy5lbWl0KCdub24tc2VyaWFsaXphYmxlLWFjdHVhbC12YWx1ZScsIHRoaXMpO1xuICAgIH1cblxuICAgIHB1YmxpYyBhc3luYyBydW4gKCk6IFByb21pc2U8dm9pZD4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