UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

154 lines 6.53 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jest_json_reporter_1 = require("@sprucelabs/jest-json-reporter"); const escapeRegExp_1 = __importDefault(require("lodash/escapeRegExp")); class JestJsonParser { testResults = { totalTestFiles: 0 }; buffer = ''; write(data) { let dataToProcess = this.buffer + data; let endDividerStartIdx = -1; do { endDividerStartIdx = dataToProcess.search((0, escapeRegExp_1.default)(jest_json_reporter_1.END_DIVIDER)); if (endDividerStartIdx > -1) { let startDividerIdx = Math.max(0, dataToProcess.search((0, escapeRegExp_1.default)(jest_json_reporter_1.START_DIVIDER))); let endDividerEndIdx = endDividerStartIdx + jest_json_reporter_1.END_DIVIDER.length; if (startDividerIdx > endDividerStartIdx) { startDividerIdx = -1; } const firstSegment = dataToProcess.substr(startDividerIdx, endDividerEndIdx - startDividerIdx); const cleanedSegment = firstSegment .replace(jest_json_reporter_1.START_DIVIDER, '') .replace(jest_json_reporter_1.END_DIVIDER, '') .trim(); const result = (0, jest_json_reporter_1.retrocycle)(JSON.parse(cleanedSegment)); this.ingestJestResult(result); dataToProcess = dataToProcess.substr(endDividerEndIdx); } } while (endDividerStartIdx > -1); this.buffer = dataToProcess; } ingestJestResult(result) { const testFiles = this.testResults.testFiles ?? []; switch (result.status) { case 'onRunStart': this.testResults = { totalTestFiles: result.results.numTotalTestSuites, }; break; case 'onTestCaseResult': { const relativePath = this.mapAbsoluteJsToRelativeTsPath(result.test.path); const idx = testFiles.findIndex((file) => file.path === relativePath); const test = this.testCaseResultToTest(result.testCaseResult); if (!testFiles[idx].tests) { testFiles[idx].tests = []; } testFiles[idx].tests?.push(test); break; } case 'onTestFileStart': testFiles.push({ path: this.pullPathFromTestResponse(result), status: this.pullTestFileStatusFromTestResponse(result), }); break; case 'onTestFileResult': { this.testResults.totalTestFilesComplete = this.pullTestFilesCompleteFromAggregatedResults(result.aggregatedResult); this.testResults.totalTestFiles = result.aggregatedResult.numTotalTestSuites; this.testResults.totalFailed = result.aggregatedResult.numFailedTests; this.testResults.totalPassed = result.aggregatedResult.numPassedTests; this.testResults.totalTests = result.aggregatedResult.numTotalTests; this.testResults.totalSkipped = result.aggregatedResult.numPendingTests; this.testResults.totalTodo = result.aggregatedResult.numTodoTests; for (const testResult of result.aggregatedResult.testResults) { const relativePath = this.mapAbsoluteJsToRelativeTsPath(testResult.testFilePath); const idx = testFiles.findIndex((file) => file.path === relativePath); const file = { ...(testFiles[idx] ?? {}), path: relativePath, status: this.pullTestFileResultStatus(testResult), tests: this.pullTestsFromTestFileResult(testResult), }; if (testResult.failureMessage) { file.errorMessage = testResult.failureMessage; } if (idx === -1) { testFiles.push(file); } else { testFiles[idx] = file; } } break; } } if (testFiles.length > 0) { this.testResults.testFiles = testFiles; } } pullTestFilesCompleteFromAggregatedResults(aggregatedResult) { const total = aggregatedResult.numFailedTestSuites + aggregatedResult.numPassedTestSuites; return total; } pullPathFromTestResponse(result) { let path = ''; switch (result.status) { case 'onTestFileResult': case 'onTestFileStart': path = result.test.path; break; } const tsFile = this.mapAbsoluteJsToRelativeTsPath(path); return tsFile; } mapAbsoluteJsToRelativeTsPath(path) { const partialPath = path.split('__tests__').pop(); if (!partialPath) { throw new Error('INVALID TEST FILE'); } const tsFile = partialPath.substr(1, partialPath.length - 3) + 'ts'; return tsFile; } pullTestFileStatusFromTestResponse(result) { switch (result.status) { case 'onTestFileResult': return this.pullTestFileResultStatus(result.testResult); default: return 'running'; } } pullTestFileResultStatus(testResult) { return testResult.failureMessage || testResult.numFailingTests > 0 ? 'failed' : 'passed'; } pullTestsFromTestFileResult(testResult) { return testResult.testResults.map((test) => //@ts-ignore - remove with next upgrade this.testCaseResultToTest(test)); } testCaseResultToTest(test) { return { name: test.title, status: test.status, errorMessages: test.failureMessages, duration: test.duration ?? 0, }; } getResults() { return this.testResults; } } exports.default = JestJsonParser; //# sourceMappingURL=JestJsonParser.js.map