Innovenergy_trunk/frontend/node_modules/testcafe/lib/api/test-run-tracker.js

100 lines
14 KiB
JavaScript
Raw Normal View History

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const callsite_1 = __importDefault(require("callsite"));
const events_1 = require("events");
const TRACKING_MARK_RE = /^\$\$testcafe_test_run\$\$(\S+)\$\$$/;
const STACK_CAPACITY = 5000;
class TestRunTracker extends events_1.EventEmitter {
constructor() {
super();
this.enabled = false;
this.activeTestRuns = {};
}
_createContextSwitchingFunctionHook(ctxSwitchingFn, patchedArgsCount) {
const tracker = this;
return function () {
const testRunId = tracker.getContextTestRunId();
if (testRunId) {
for (let i = 0; i < patchedArgsCount; i++) {
if (typeof arguments[i] === 'function')
arguments[i] = tracker.addTrackingMarkerToFunction(testRunId, arguments[i]);
}
}
// @ts-ignore
return ctxSwitchingFn.apply(this, arguments);
};
}
_getStackFrames() {
// NOTE: increase stack capacity to seek deep stack entries
const savedLimit = Error.stackTraceLimit;
Error.stackTraceLimit = STACK_CAPACITY;
const frames = (0, callsite_1.default)();
Error.stackTraceLimit = savedLimit;
return frames;
}
getMarkedFnName(testRunId) {
return `$$testcafe_test_run$$${testRunId}$$`;
}
ensureEnabled() {
if (!this.enabled) {
global.setTimeout = this._createContextSwitchingFunctionHook(global.setTimeout, 1);
global.setInterval = this._createContextSwitchingFunctionHook(global.setInterval, 1);
global.setImmediate = this._createContextSwitchingFunctionHook(global.setImmediate, 1);
process.nextTick = this._createContextSwitchingFunctionHook(process.nextTick, 1);
global.Promise.prototype.then = this._createContextSwitchingFunctionHook(global.Promise.prototype.then, 2);
global.Promise.prototype.catch = this._createContextSwitchingFunctionHook(global.Promise.prototype.catch, 1);
this.enabled = true;
}
}
addTrackingMarkerToFunction(testRunId, fn, context) {
const markerFactoryBody = `
return function ${this.getMarkedFnName(testRunId)} () {
context = context || this;
switch (arguments.length) {
case 0: return fn.call(context);
case 1: return fn.call(context, arguments[0]);
case 2: return fn.call(context, arguments[0], arguments[1]);
case 3: return fn.call(context, arguments[0], arguments[1], arguments[2]);
case 4: return fn.call(context, arguments[0], arguments[1], arguments[2], arguments[3]);
default: return fn.apply(context, arguments);
}
};
`;
return new Function('fn', 'context', markerFactoryBody)(fn, context);
}
getContextTestRunId() {
const frames = this._getStackFrames();
// OPTIMIZATION: we start traversing from the bottom of the stack,
// because we'll more likely encounter a marker there.
// Async/await and Promise machinery executes lots of intrinsics
// on timers (where we have a marker). And, since a timer initiates a new
// stack, the marker will be at the very bottom of it.
for (let i = frames.length - 1; i >= 0; i--) {
const fnName = frames[i].getFunctionName();
const match = fnName && fnName.match(TRACKING_MARK_RE);
if (match)
return match[1];
}
return null;
}
resolveContextTestRun() {
const testRunId = this.getContextTestRunId();
if (testRunId)
return this.activeTestRuns[testRunId];
return null;
}
addActiveTestRun(testRun) {
this.activeTestRuns[testRun.id] = testRun;
testRun.onAny((eventName, eventData) => this.emit(eventName, { testRun, data: eventData }));
}
removeActiveTestRun(id) {
delete this.activeTestRuns[id];
}
}
// Tracker
exports.default = new TestRunTracker();
module.exports = exports.default;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC1ydW4tdHJhY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9hcGkvdGVzdC1ydW4tdHJhY2tlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHdEQUFzQztBQUN0QyxtQ0FBc0M7QUFJdEMsTUFBTSxnQkFBZ0IsR0FBRyxzQ0FBc0MsQ0FBQztBQUNoRSxNQUFNLGNBQWMsR0FBSyxJQUFJLENBQUM7QUFFOUIsTUFBTSxjQUFlLFNBQVEscUJBQVk7SUFJckM7UUFDSSxLQUFLLEVBQUUsQ0FBQztRQUVSLElBQUksQ0FBQyxPQUFPLEdBQUcsS0FBSyxDQUFD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