UNPKG

vitest-teamcity-reporter

Version:
73 lines (72 loc) 3.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Printer = void 0; const suite_message_1 = require("./messages/suite-message"); const escape_1 = require("./escape"); const test_message_1 = require("./messages/test-message"); const missing_result_error_1 = __importDefault(require("./error/missing-result.error")); class Printer { constructor(logger) { this.logger = logger; this.fileMessageMap = new Map(); this.testConsoleMap = new Map(); this.addFile = (file) => { const suitMessage = new suite_message_1.SuitMessage(file.id, (0, escape_1.escape)(file.name)); const messages = [suitMessage.started(), ...file.tasks.flatMap(this.handleTask), suitMessage.finished()]; this.fileMessageMap.set(file.id, messages); }; this.handeUpdate = ([id, result]) => { const messages = this.fileMessageMap.get(id); if (messages != null && result != null && result.state !== 'run') { messages .flatMap((message) => (typeof message === 'string' ? message : message())) .forEach((message) => { this.logger.console.info(message); }); this.fileMessageMap.delete(id); } }; this.handleTask = (task) => { if (task.type === 'test') { return this.handleTest(task); } if (task.type === 'suite' && task.mode === 'run') { return this.handleSuite(task); } return []; }; this.handleSuite = (suite) => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const suitMessage = new suite_message_1.SuitMessage(suite.file.id, (0, escape_1.escape)(suite.name)); return [suitMessage.started(), ...suite.tasks.flatMap(this.handleTask), suitMessage.finished()]; }; this.handleTest = (test) => { const testMessage = new test_message_1.TestMessage(test); if (test.mode === 'skip' || test.mode === 'todo') { return testMessage.ignored(); } return () => { var _a, _b, _c; const fail = test.result == null || test.result.state === 'fail'; const logs = (_a = this.testConsoleMap.get(test.id)) !== null && _a !== void 0 ? _a : []; const logsMessages = logs.map((log) => testMessage.log(log.type, log.content)); const filedMessages = fail ? this.getTestErrors(test).map(testMessage.fail) : []; return [testMessage.started(), ...logsMessages, ...filedMessages, testMessage.finished((_c = (_b = test.result) === null || _b === void 0 ? void 0 : _b.duration) !== null && _c !== void 0 ? _c : 0)].filter(Boolean); }; }; this.getTestErrors = (test) => { var _a, _b, _c, _d, _e, _f, _g, _h; return (_h = (_e = (_b = (_a = test.result) === null || _a === void 0 ? void 0 : _a.errors) !== null && _b !== void 0 ? _b : (_d = (_c = test.suite) === null || _c === void 0 ? void 0 : _c.result) === null || _d === void 0 ? void 0 : _d.errors) !== null && _e !== void 0 ? _e : (_g = (_f = test.file) === null || _f === void 0 ? void 0 : _f.result) === null || _g === void 0 ? void 0 : _g.errors) !== null && _h !== void 0 ? _h : [new missing_result_error_1.default(test)]; }; } addTestConsoleLog(id, log) { const messages = this.testConsoleMap.get(id); if (messages != null) { messages.push(log); } else { this.testConsoleMap.set(id, [log]); } } } exports.Printer = Printer;