testcafe-legacy-api
Version:
Legacy API support for TestCafe
66 lines (65 loc) • 2.89 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const highlight_es_1 = __importDefault(require("highlight-es"));
const parse5_1 = __importDefault(require("parse5"));
const templates_1 = __importDefault(require("./templates"));
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
var renderer = ['string', 'punctuator', 'keyword', 'number', 'regex', 'comment', 'invalid'].reduce((syntaxRenderer, tokenType) => {
syntaxRenderer[tokenType] = str => `<span class="syntax-${tokenType}">${(0, lodash_1.escape)(str)}</span>`;
return syntaxRenderer;
}, {});
class TestRunErrorFormattableAdapter {
constructor(err, metaInfo) {
this.TEMPLATES = templates_1.default;
this.userAgent = metaInfo.userAgent;
this.screenshotPath = metaInfo.screenshotPath;
this.testRunState = metaInfo.testRunState;
(0, lodash_1.assignIn)(this, err);
this.callsite = this.callsite || metaInfo.callsite;
}
static _getSelector(node) {
var classAttr = (0, lodash_1.find)(node.attrs, { name: 'class' });
var cls = classAttr && classAttr.value;
return cls ? `${node.tagName} ${cls}` : node.tagName;
}
static _decorateHtml(node, decorator) {
var msg = '';
if (node.nodeName === '#text')
msg = node.value;
else {
if (node.childNodes.length) {
msg += node.childNodes
.map(childNode => TestRunErrorFormattableAdapter._decorateHtml(childNode, decorator))
.join('');
}
if (node.nodeName !== '#document-fragment') {
var selector = TestRunErrorFormattableAdapter._getSelector(node);
msg = decorator[selector](msg, node.attrs);
}
}
return msg;
}
getErrorMarkup(viewportWidth) {
return this.TEMPLATES[this.type](this, viewportWidth);
}
getCallsiteMarkup() {
if (!this.callsite)
return null;
var code = (0, highlight_es_1.default)(this.callsite, renderer);
var lines = code.split(NEWLINE);
var lastLine = lines.pop();
lastLine = `<div class="code-line-last">${lastLine}</div>`;
lines = lines.map(line => `<div class="code-line"><div class="code-line-src">${line}</div></div>`);
return `<div class="code-frame">${lines.join('')}${lastLine}</div>`;
}
formatMessage(decorator, viewportWidth) {
var msgHtml = this.getErrorMarkup(viewportWidth);
var fragment = parse5_1.default.parseFragment(msgHtml);
return TestRunErrorFormattableAdapter._decorateHtml(fragment, decorator);
}
}
exports.default = TestRunErrorFormattableAdapter;