Skip to content

Commit

Permalink
fix: no dangling comma is included in result
Browse files Browse the repository at this point in the history
@W-15530915@
Fix the edge case to prevent dangling comma when tests are not run with coverage
  • Loading branch information
peternhale committed Apr 17, 2024
1 parent 83efe7b commit 824b6d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/streaming/testResultStringifyStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 35 additions & 6 deletions test/streaming/testResultStringifyStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,44 @@ 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 with tests and no coverage', (done) => {
let output = '';
const testsWithoutCoverage = structuredClone(tests);
const resultsWithTests = {
...testResult,
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();
});
Expand All @@ -155,11 +182,12 @@ describe('TestResultStringifyStream', () => {
});
it('should transform TestResult into a JSON string', (done) => {
let output = '';
tests[0].perClassCoverage = [perClassCoverageData[0]];
tests[1].perClassCoverage = perClassCoverageData;
const testsWithCoverage = structuredClone(tests);
testsWithCoverage[0].perClassCoverage = [perClassCoverageData[0]];
testsWithCoverage[1].perClassCoverage = perClassCoverageData;
const resultsWithTests = {
...testResult,
tests,
tests: testsWithCoverage,
codecoverage: coverageData
};
// Initialize the stream with the testResult
Expand All @@ -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();
Expand Down

0 comments on commit 824b6d5

Please sign in to comment.