UNPKG

@salesforce/apex-node

Version:

Salesforce JS library for Apex

141 lines 6.16 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TestResultStringifyStream = void 0; /* * Copyright (c) 2024, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ const core_1 = require("@salesforce/core"); const node_stream_1 = require("node:stream"); const utils_1 = require("../utils"); class TestResultStringifyStream extends node_stream_1.Readable { testResult; logger; buffer; bufferSize; constructor(testResult, options) { super({ ...options, objectMode: true }); this.testResult = testResult; this.testResult = testResult; this.logger = core_1.Logger.childFromRoot('TestResultStringifyStream'); this.buffer = ''; this.bufferSize = options?.bufferSize || 256; // Default buffer size is 256 } pushToBuffer(data) { this.buffer += data; if (this.buffer.length >= this.bufferSize) { this.push(this.buffer); this.buffer = ''; } } _read() { this.logger.trace('starting format'); this.format(); if (this.buffer.length > 0) { this.push(this.buffer); this.buffer = ''; } this.push(null); // Signal the end of the stream this.logger.trace('finishing format'); } format() { const { summary } = this.testResult; // strip out vars not included in the summary data reported to the user // outer curly this.pushToBuffer('{'); // summary this.pushToBuffer(`"summary":${JSON.stringify(summary)},`); this.buildTests(); this.buildCodeCoverage(); // closing outer curly this.pushToBuffer(`}`); } buildTests() { this.pushToBuffer('"tests":['); const numberOfTests = this.testResult.tests.length - 1; this.testResult.tests.forEach((test, index) => { const { perClassCoverage, ...testRest } = test; this.pushToBuffer(`${JSON.stringify(testRest).slice(0, -1)}`); if (perClassCoverage) { const numberOfPerClassCoverage = perClassCoverage.length - 1; this.pushToBuffer(',"perClassCoverage":['); perClassCoverage.forEach((pcc, index) => { const { coverage, ...coverageRest } = pcc; this.pushToBuffer(`${JSON.stringify(coverageRest).slice(0, -1)}`); this.pushToBuffer(`,"coverage":${JSON.stringify(coverage)}}`); if (numberOfPerClassCoverage !== index) { this.pushToBuffer(','); } }); this.pushToBuffer(']'); // this call to setImmediate will schedule the closure on the event loop // this action causing the current code to yield to the event loop // allowing other processes to get time on the event loop setImmediate(() => { }); } // close the tests this.pushToBuffer('}'); if (numberOfTests !== index) { this.pushToBuffer(','); } }); this.pushToBuffer(']'); } buildCodeCoverage() { if (this.testResult.codecoverage) { this.pushToBuffer(',"codecoverage":['); const numberOfCodeCoverage = this.testResult.codecoverage.length - 1; this.testResult.codecoverage.forEach((coverage, index) => { const { coveredLines, uncoveredLines, ...theRest } = coverage; this.pushToBuffer(`${JSON.stringify(theRest).slice(0, -1)}`); this.pushToBuffer(',"coveredLines":['); this.pushArrayToBuffer(coveredLines); this.pushToBuffer('],"uncoveredLines":['); this.pushArrayToBuffer(uncoveredLines); this.pushToBuffer(']}'); if (numberOfCodeCoverage !== index) { this.pushToBuffer(','); } // this call to setImmediate will schedule the closure on the event loop // this action causing the current code to yield to the event loop // allowing other processes to get time on the event loop setImmediate(() => { }); }); this.pushToBuffer(']'); } } static fromTestResult(testResult, options) { return new TestResultStringifyStream(testResult, options); } pushArrayToBuffer(array) { const chunkSize = 1000; for (let i = 0; i < array.length; i += chunkSize) { const chunk = array.slice(i, i + chunkSize); let jsonString = JSON.stringify(chunk); jsonString = jsonString.slice(1, -1); // remove '[' and ']' this.pushToBuffer(jsonString); if (i + chunkSize < array.length) { this.pushToBuffer(','); // add comma for all but the last chunk } } } } exports.TestResultStringifyStream = TestResultStringifyStream; __decorate([ (0, utils_1.elapsedTime)() ], TestResultStringifyStream.prototype, "format", null); __decorate([ (0, utils_1.elapsedTime)() ], TestResultStringifyStream.prototype, "buildTests", null); __decorate([ (0, utils_1.elapsedTime)() ], TestResultStringifyStream.prototype, "buildCodeCoverage", null); //# sourceMappingURL=testResultStringifyStream.js.map