UNPKG

@90poe/jest-performant-warnings-stats-reporter

Version:

A flexible jest reporter that provides ability to show statistics of warnings and group them

128 lines (127 loc) 5.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chalk_1 = require("chalk"); const table_1 = require("table"); const constants_1 = require("./constants"); const recursiveLogger_1 = require("./recursiveLogger"); const utils_1 = require("./utils"); class CustomReporter { constructor(globalConfiguration, options) { var _a, _b, _c, _d, _e, _f; this.globalConfiguration = globalConfiguration; this.options = options; this.globalConfig = globalConfiguration; this.showWarningSummary = (_a = options.showWarningSummary) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_SHOW_WARNING_SUMMARY; this.warningsConfig = (_c = (_b = options.warningsConfig) === null || _b === void 0 ? void 0 : _b.map(utils_1.addFilesSetToWarningConfig)) !== null && _c !== void 0 ? _c : constants_1.DEFAULT_WARNINGS_CONFIG; this.maxSlowTests = (_d = options.maxSlowTests) !== null && _d !== void 0 ? _d : constants_1.DEFAULT_MAX_SLOW_TESTS; this.logs = {}; this.showSlowest = (_e = options.showSlowest) !== null && _e !== void 0 ? _e : constants_1.DEFAULT_SHOW_SLOWEST; this.slowTests = []; this.warnOnSlowerThan = (_f = options.warnOnSlowerThan) !== null && _f !== void 0 ? _f : constants_1.DEFAULT_WARN_ON_SLOWER_THAN; this.finalTable = (0, utils_1.omitWarningsData)(globalConfiguration) ? [constants_1.TABLE_HEADER_WITHOUT_WARNINGS_DATA] : [constants_1.TABLE_HEADER_WITH_WARNINGS_DATA]; } onRunStart({ numTotalTestSuites }) { const greetingMessage = (0, utils_1.prepareGreetingMessage)(numTotalTestSuites); console.log(greetingMessage); } onTestResult(_, testResult) { const fileName = (0, utils_1.getFileName)(testResult.testFilePath); const warningsPerTest = {}; const tableRow = (0, utils_1.prepareTableRow)(testResult, this.warnOnSlowerThan, this.globalConfig); this.logs[fileName] = { filePath: testResult.testFilePath, warnings: [], }; this.finalTable.push(tableRow); if (testResult.console) { testResult.console.forEach((value) => { let isOther = true; this.warningsConfig.forEach(({ regex, name }) => { if (regex.test(value.message)) { warningsPerTest[name] = warningsPerTest[name] + 1 || 1; isOther = false; } }); if (isOther) { warningsPerTest.Other = warningsPerTest.Other + 1 || 1; } this.logs[fileName].warnings.push(value); }); } if (this.showSlowest) { this.collectSlowTests(testResult); } if (this.showWarningSummary && !this.globalConfig.verbose) { const warningsInfo = (0, utils_1.prepareWarnings)(warningsPerTest); this.finalTable.push(...warningsInfo); } } onRunComplete(_, { numFailedTests, numPassedTests, testResults }) { testResults.forEach(({ testFilePath, testResults: innerTestResults, failureMessage }) => { (0, recursiveLogger_1.recursiveLogger)(testFilePath, innerTestResults, 0, 0); if (failureMessage) { console.log(failureMessage); } }); if (numPassedTests !== 0) { console.log((0, chalk_1.green)(`${numPassedTests} passed`)); } if (numFailedTests !== 0) { console.log((0, chalk_1.bold)((0, chalk_1.red)(`${numFailedTests} failed`))); } if (this.globalConfig.verbose || this.globalConfig.watch) { Object.entries(this.logs).forEach(([filename, data]) => { const { warnings } = data; if (warnings.length) { console.log((0, chalk_1.bold)(filename)); } warnings.forEach(utils_1.log); }); } const shouldShowTable = this.finalTable.length > 1; if (shouldShowTable) { console.log((0, table_1.table)(this.finalTable)); } if (this.showSlowest && !this.globalConfig.watch && !this.globalConfig.watchAll) { this.consoleSlowTests(); } } // Idea taken from [jest-slow-test-reporter](https://github.com/jodonnell/jest-slow-test-reporter) collectSlowTests(result) { result.testResults.forEach((test) => { var _a; this.slowTests.push({ duration: (_a = test.duration) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_TEST_DURATION, fullName: test.fullName, filePath: result.testFilePath, }); }); } consoleSlowTests() { this.slowTests.sort(function sort(a, b) { return b.duration - a.duration; }); const rootPathRegex = new RegExp(`^${process.cwd()}`); const slowestTests = this.slowTests.slice(0, this.maxSlowTests); const slowTestTime = this.calcTestTime(slowestTests); const allTestTime = this.calcTestTime(this.slowTests); const percentTime = (slowTestTime / allTestTime) * 100; console.log(`Top ${slowestTests.length} slowest examples (${slowTestTime / 1000} seconds, ${percentTime.toFixed(1)}% of total time):`); slowestTests.forEach(({ duration, fullName, filePath }) => { const path = filePath.replace(rootPathRegex, '.'); console.log(` ${fullName}`); console.log(` ${duration / 1000} seconds ${path}`); }); } calcTestTime(slowestTests) { return slowestTests.reduce((acc, curr) => acc + curr.duration, 0); } } exports.default = CustomReporter;