UNPKG

@tsdi/unit

Version:

unit testing framework, base on AOP, Ioc container

125 lines (123 loc) 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TestReport = exports.ReportsToken = void 0; var tslib_1 = require("tslib"); var ioc_1 = require("@tsdi/ioc"); var Reporter_1 = require("./Reporter"); /** * report token. */ exports.ReportsToken = ioc_1.tokenId('unit-reports'); /** * test report. * * @export * @class TestReport * @implements {ITestReport} */ var TestReport = /** @class */ (function () { function TestReport() { this.suites = new Map(); } TestReport.prototype.getReports = function () { if (!this.resports || this.resports.length < 0) { this.resports = this.injector.getServices(Reporter_1.Reporter); } return this.resports || []; }; TestReport.prototype.track = function (error) { this.resports.forEach(function (rep) { rep.track(error); }); }; TestReport.prototype.addSuite = function (suit, describe) { var _this = this; if (!this.suites.has(suit)) { describe.start = new Date().getTime(); // init suite must has no completed cases. if (describe.cases.length) { describe = ioc_1.lang.omit(describe, 'cases'); } describe.cases = []; this.suites.set(suit, describe); this.getReports().forEach(function (rep) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { if (rep instanceof Reporter_1.RealtimeReporter) { rep.renderSuite(describe); } return [2 /*return*/]; }); }); }); } }; TestReport.prototype.getSuite = function (suit) { return this.suites.has(suit) ? this.suites.get(suit) : null; }; TestReport.prototype.setSuiteCompleted = function (suit) { var suite = this.getSuite(suit); if (suite) { suite.end = new Date().getTime(); } }; TestReport.prototype.addCase = function (suit, testCase) { if (this.suites.has(suit)) { testCase.start = new Date().getTime(); this.suites.get(suit).cases.push(testCase); } }; TestReport.prototype.getCase = function (suit, test) { var suite = this.getSuite(suit); if (suite) { var tCase = suite.cases.find(function (c) { return c.key === test; }); if (!tCase) { tCase = suite.cases.find(function (c) { return c.title === test; }); } return tCase; } return null; }; TestReport.prototype.setCaseCompleted = function (testCase) { var _this = this; testCase.end = new Date().getTime(); this.getReports().forEach(function (rep) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { if (rep instanceof Reporter_1.RealtimeReporter) { rep.renderCase(testCase); } return [2 /*return*/]; }); }); }); }; TestReport.prototype.report = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, Promise.all(this.getReports().map(function (rep) { if (rep) { return rep.render(_this.suites); } return null; }))]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; TestReportAnn = function () { return { "name": "TestReport", "params": { "track": ["error"], "addSuite": ["suit", "describe"], "getSuite": ["suit"], "setSuiteCompleted": ["suit"], "addCase": ["suit", "testCase"], "getCase": ["suit", "test"], "setCaseCompleted": ["testCase"] } }; }; tslib_1.__decorate([ ioc_1.Inject(ioc_1.INJECTOR), tslib_1.__metadata("design:type", Object) ], TestReport.prototype, "injector", void 0); TestReport = tslib_1.__decorate([ ioc_1.Singleton(), tslib_1.__metadata("design:paramtypes", []) ], TestReport); return TestReport; }()); exports.TestReport = TestReport; //# sourceMappingURL=../sourcemaps/reports/TestReport.js.map