UNPKG

@netatwork/mocha-utils

Version:

Utility package for mocha based test setup

143 lines 6.48 kB
"use strict"; try { var windw = (((window))); var _a = windw.Mocha.Runner.constants, EVENT_RUN_END_1 = _a.EVENT_RUN_END, EVENT_SUITE_BEGIN_1 = _a.EVENT_SUITE_BEGIN, EVENT_SUITE_END_1 = _a.EVENT_SUITE_END, EVENT_TEST_FAIL_1 = _a.EVENT_TEST_FAIL, EVENT_TEST_PASS_1 = _a.EVENT_TEST_PASS, EVENT_TEST_PENDING_1 = _a.EVENT_TEST_PENDING; var OriginalReporterFunction_1; var MochaReporter = (function () { function MochaReporter(runner) { var _this = this; this._originalReporter = new OriginalReporterFunction_1(runner); var container = this.createResultContainer(); if (container === null) { return; } var currentSuiteContainer = (this.specs); runner .on(EVENT_SUITE_BEGIN_1, function (suite) { if (suite.root) { return; } var item = _this.createListItem(suite.title); var list = document.createElement('ul'); item.appendChild(list); currentSuiteContainer.appendChild(item); currentSuiteContainer = list; }) .on(EVENT_SUITE_END_1, function () { currentSuiteContainer = (currentSuiteContainer.parentElement); }) .on(EVENT_TEST_PASS_1, function (test) { _this.append(test, currentSuiteContainer); }) .on(EVENT_TEST_PENDING_1, function (test) { _this.append(test, currentSuiteContainer); }) .on(EVENT_TEST_FAIL_1, function (test, error) { _this.append(test, currentSuiteContainer, error); }) .once(EVENT_RUN_END_1, function () { var _a, _b; var stats = (runner.stats); var span = document.createElement('span'); var numFailures = (_a = stats.failures) !== null && _a !== void 0 ? _a : 0; span.textContent = (numFailures > 0 ? numFailures + ' failures. ' : '') + stats.passes + ' of ' + stats.tests + ' tests passed.' + (((_b = stats.pending) !== null && _b !== void 0 ? _b : 0) > 0 ? ' ' + stats.pending + ' pending.' : ''); var time = document.createElement('span'); time.textContent = "Finished in ".concat(((stats === null || stats === void 0 ? void 0 : stats.duration) || 0) / 1000, " s"); var summary = (_this.summary); summary.append(span, time); summary.dataset[numFailures ? 'failure' : 'success'] = ''; var ulDiv = document.createElement('div'); ulDiv.classList.add('ul-container'); ulDiv.append((_this.specs)); container.append((summary), ulDiv); }); } MochaReporter.prototype.createResultContainer = function () { try { var container = document.createElement('div'); container.classList.add('kmhtml'); var dots = this.dots = document.createElement('div'); dots.classList.add('dots'); this.summary = document.createElement('div'); this.summary.classList.add('summary'); this.specs = document.createElement('ul'); container.appendChild(dots); document.body.appendChild(container); return container; } catch (e) { return null; } }; MochaReporter.prototype.append = function (test, suiteContainer, error) { if (error === void 0) { error = undefined; } var $class = ''; switch (true) { case test.isPassed(): $class = 'passed'; break; case test.isFailed(): $class = 'failed'; break; case test.isPending(): $class = 'pending'; break; } var title = test.fullTitle(); var span = document.createElement('span'); span.innerHTML = '•'; span.title = title; span.classList.add($class, 'dot'); span.style.color = $class; ((this.dots)).appendChild(span); suiteContainer.appendChild(this.createSpecResult(test, $class, error)); }; MochaReporter.prototype.createSpecResult = function (test, $class, error) { if ($class === void 0) { $class = ''; } if (error === void 0) { error = undefined; } var li = this.createListItem(test.title, test.fullTitle(), $class); if (test.isFailed()) { var details = document.createElement('details'); var summary = document.createElement('summary'); summary.textContent = 'Error(s)'; details.append(summary); var errDiv = document.createElement('pre'); var err = (error); errDiv.innerHTML = err.message + '\n' + err.stack; details.append(errDiv); li.append(details); } return li; }; MochaReporter.prototype.createListItem = function (title, fullTitle, $class) { if (fullTitle === void 0) { fullTitle = title; } if ($class === void 0) { $class = ''; } var li = document.createElement('li'); if ($class) { li.classList.add($class); } var anchor = document.createElement('a'); anchor.innerText = title; var loc = new URL(location.href); loc.pathname = 'debug.html'; loc.searchParams.set('grep', fullTitle); anchor.href = loc.href; anchor.target = '_blank'; li.appendChild(anchor); return li; }; return MochaReporter; }()); var mocha_1 = windw.mocha; OriginalReporterFunction_1 = (mocha_1['_reporter']); mocha_1['_reporter'] = MochaReporter; } catch (e) { console.log('Not running the kmhtml reporter. Note that this is a dev-only reporter, meant to be used in browser.'); } //# sourceMappingURL=reporter.js.map