From 962af402952b03705ff313fe06cc424e6d7d8564 Mon Sep 17 00:00:00 2001 From: peternhale Date: Thu, 18 Apr 2024 06:17:41 -0600 Subject: [PATCH 1/2] fix: no dangling comma is included in result (#366) * fix: no dangling comma is included in result @W-15530915@ Fix the edge case to prevent dangling comma when tests are not run with coverage * Update test/streaming/testResultStringifyStream.test.ts Co-authored-by: Daphne Yang <139700604+daphne-sfdc@users.noreply.github.com> --------- Co-authored-by: Daphne Yang <139700604+daphne-sfdc@users.noreply.github.com> --- src/streaming/testResultStringifyStream.ts | 4 +- .../testResultStringifyStream.test.ts | 43 ++++++++++++++++--- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/streaming/testResultStringifyStream.ts b/src/streaming/testResultStringifyStream.ts index 2b74a9df..0f05e67d 100644 --- a/src/streaming/testResultStringifyStream.ts +++ b/src/streaming/testResultStringifyStream.ts @@ -68,13 +68,13 @@ export class TestResultStringifyStream extends Readable { } }); - this.push('],'); + this.push(']'); } @elapsedTime() buildCodeCoverage(): void { if (this.testResult.codecoverage) { - this.push('"codecoverage":['); + this.push(',"codecoverage":['); const numberOfCodeCoverage = this.testResult.codecoverage.length - 1; this.testResult.codecoverage.forEach((coverage, index) => { const { coveredLines, uncoveredLines, ...theRest } = coverage; diff --git a/test/streaming/testResultStringifyStream.test.ts b/test/streaming/testResultStringifyStream.test.ts index 64829caa..e5bd1416 100644 --- a/test/streaming/testResultStringifyStream.test.ts +++ b/test/streaming/testResultStringifyStream.test.ts @@ -136,30 +136,58 @@ describe('TestResultStringifyStream', () => { }; }); - it('should transform TestResult into a JSON string with no tests and no coverage', (done) => { + it('should transform TestResult into a JSON string with empty tests and no coverage', (done) => { let output = ''; + const emptyTestsNoCoverage = structuredClone(testResult); + delete emptyTestsNoCoverage.codecoverage; // Initialize the stream with the testResult - stream = new TestResultStringifyStream(testResult); + stream = new TestResultStringifyStream(emptyTestsNoCoverage); stream.on('data', (chunk: string) => { output += chunk; }); stream.on('end', () => { - const expectedOutput = JSON.stringify(testResult); + expect(() => JSON.parse(output)).to.not.throw(); + const expectedOutput = JSON.stringify(emptyTestsNoCoverage); expect(output).to.equal(expectedOutput); done(); }); stream._read(); }); - it('should transform TestResult into a JSON string', (done) => { + it('should transform TestResult into a JSON string with tests and no coverage', (done) => { let output = ''; - tests[0].perClassCoverage = [perClassCoverageData[0]]; - tests[1].perClassCoverage = perClassCoverageData; + const testsWithoutCoverage = structuredClone(tests); const resultsWithTests = { ...testResult, - tests, + tests: testsWithoutCoverage + }; + delete resultsWithTests.codecoverage; + // Initialize the stream with the testResult + stream = new TestResultStringifyStream(resultsWithTests); + + stream.on('data', (chunk: string) => { + output += chunk; + }); + + stream.on('end', () => { + expect(() => JSON.parse(output)).to.not.throw(); + const expectedOutput = JSON.stringify(resultsWithTests); + expect(output).to.equal(expectedOutput); + done(); + }); + + stream._read(); + }); + it('should transform TestResult into a JSON string with tests and coverage both present', (done) => { + let output = ''; + const testsWithCoverage = structuredClone(tests); + testsWithCoverage[0].perClassCoverage = [perClassCoverageData[0]]; + testsWithCoverage[1].perClassCoverage = perClassCoverageData; + const resultsWithTests = { + ...testResult, + tests: testsWithCoverage, codecoverage: coverageData }; // Initialize the stream with the testResult @@ -170,6 +198,7 @@ describe('TestResultStringifyStream', () => { }); stream.on('end', () => { + expect(() => JSON.parse(output)).to.not.throw(); const expectedOutput = JSON.stringify(resultsWithTests); expect(output).to.equal(expectedOutput); done(); From 95fb9947d2b0f3e3219a2d51a762c1abb3b1366d Mon Sep 17 00:00:00 2001 From: svc-cli-bot Date: Thu, 18 Apr 2024 12:17:56 +0000 Subject: [PATCH 2/2] chore(release): 4.0.6 [skip ci] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bc829b0e..307bf5b2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@salesforce/apex-node", "description": "Salesforce JS library for Apex", - "version": "4.0.5", + "version": "4.0.6", "author": "Salesforce", "bugs": "https://github.com/forcedotcom/salesforcedx-apex/issues", "main": "lib/src/index.js",