@tsdi/unit
Version:
unit testing framework, base on AOP, Ioc container
105 lines (103 loc) • 3.3 kB
JavaScript
import { __awaiter, __decorate, __metadata } from "tslib";
import { Singleton, Inject, lang, INJECTOR, tokenId } from '@tsdi/ioc';
import { Reporter, RealtimeReporter } from './Reporter';
/**
* report token.
*/
export const ReportsToken = tokenId('unit-reports');
/**
* test report.
*
* @export
* @class TestReport
* @implements {ITestReport}
*/
let TestReport = class TestReport {
constructor() {
this.suites = new Map();
}
getReports() {
if (!this.resports || this.resports.length < 0) {
this.resports = this.injector.getServices(Reporter);
}
return this.resports || [];
}
track(error) {
this.resports.forEach(rep => {
rep.track(error);
});
}
addSuite(suit, describe) {
if (!this.suites.has(suit)) {
describe.start = new Date().getTime();
// init suite must has no completed cases.
if (describe.cases.length) {
describe = lang.omit(describe, 'cases');
}
describe.cases = [];
this.suites.set(suit, describe);
this.getReports().forEach((rep) => __awaiter(this, void 0, void 0, function* () {
if (rep instanceof RealtimeReporter) {
rep.renderSuite(describe);
}
}));
}
}
getSuite(suit) {
return this.suites.has(suit) ? this.suites.get(suit) : null;
}
setSuiteCompleted(suit) {
let suite = this.getSuite(suit);
if (suite) {
suite.end = new Date().getTime();
}
}
addCase(suit, testCase) {
if (this.suites.has(suit)) {
testCase.start = new Date().getTime();
this.suites.get(suit).cases.push(testCase);
}
}
getCase(suit, test) {
let suite = this.getSuite(suit);
if (suite) {
let tCase = suite.cases.find(c => c.key === test);
if (!tCase) {
tCase = suite.cases.find(c => c.title === test);
}
return tCase;
}
return null;
}
setCaseCompleted(testCase) {
testCase.end = new Date().getTime();
this.getReports().forEach((rep) => __awaiter(this, void 0, void 0, function* () {
if (rep instanceof RealtimeReporter) {
rep.renderCase(testCase);
}
}));
}
report() {
return __awaiter(this, void 0, void 0, function* () {
yield Promise.all(this.getReports().map(rep => {
if (rep) {
return rep.render(this.suites);
}
return null;
}));
});
}
static ρAnn() {
return { "name": "TestReport", "params": { "track": ["error"], "addSuite": ["suit", "describe"], "getSuite": ["suit"], "setSuiteCompleted": ["suit"], "addCase": ["suit", "testCase"], "getCase": ["suit", "test"], "setCaseCompleted": ["testCase"] } };
}
};
__decorate([
Inject(INJECTOR),
__metadata("design:type", Object)
], TestReport.prototype, "injector", void 0);
TestReport = __decorate([
Singleton(),
__metadata("design:paramtypes", [])
], TestReport);
export { TestReport };
//# sourceMappingURL=../sourcemaps/reports/TestReport.js.map