Skip to content

Commit

Permalink
Merge branch 'main' into cristi/w-15279718/test-setup-time
Browse files Browse the repository at this point in the history
  • Loading branch information
CristiCanizales authored Apr 22, 2024
2 parents 9125f53 + 95fb994 commit a1001fc
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ jspm_packages/

# parcel-bundler cache (https://parceljs.org/)
.cache
.idea/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/apex-node",
"description": "Salesforce JS library for Apex",
"version": "4.0.2",
"version": "4.0.6",
"author": "Salesforce",
"bugs": "https://github.com/forcedotcom/salesforcedx-apex/issues",
"main": "lib/src/index.js",
Expand Down
6 changes: 3 additions & 3 deletions src/streaming/codeCoverageStringifyStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export class CodeCoverageStringifyStream extends Transform {
const { coverage, ...theRest } = perClassCoverage;
// stringify all properties except coverage and strip off the closing '}'
this.push(JSON.stringify(theRest).slice(0, -1));
this.push(',"coverage": {');
this.push('"coveredLines": [');
this.push(',"coverage":{');
this.push('"coveredLines":[');
pushArrayToStream(coverage.coveredLines ?? [], this);
this.push('],"uncoveredLines": [');
this.push('],"uncoveredLines":[');
pushArrayToStream(coverage.uncoveredLines ?? [], this);
this.push(']}}');

Expand Down
20 changes: 10 additions & 10 deletions src/streaming/testResultStringifyStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class TestResultStringifyStream extends Readable {
// outer curly
this.push('{');
// summary
this.push(`"summary": ${JSON.stringify(summary)},`);
this.push(`"summary":${JSON.stringify(summary)},`);

this.buildTests();
this.buildCodeCoverage();
Expand All @@ -47,19 +47,19 @@ export class TestResultStringifyStream extends Readable {
const numberOfTests = this.testResult.tests.length - 1;
this.testResult.tests.forEach((test, index) => {
const { perClassCoverage, ...testRest } = test;
this.push(`${JSON.stringify(testRest).slice(0, -1)},`);
this.push(`${JSON.stringify(testRest).slice(0, -1)}`);
if (perClassCoverage) {
const numberOfPerClassCoverage = perClassCoverage.length - 1;
this.push('"perClassCoverage": [');
this.push(',"perClassCoverage":[');
perClassCoverage.forEach((pcc, index) => {
const { coverage, ...coverageRest } = pcc;
this.push(`${JSON.stringify(coverageRest).slice(0, -1)},`);
this.push(`"coverage": ${JSON.stringify(coverage)}}`);
this.push(`${JSON.stringify(coverageRest).slice(0, -1)}`);
this.push(`,"coverage":${JSON.stringify(coverage)}}`);
if (numberOfPerClassCoverage !== index) {
this.push(',');
}
});
this.push('],');
this.push(']');
}
// close the tests
this.push('}');
Expand All @@ -68,18 +68,18 @@ 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;
this.push(`${JSON.stringify(theRest).slice(0, -1)},`);
this.push('"coveredLines":[');
this.push(`${JSON.stringify(theRest).slice(0, -1)}`);
this.push(',"coveredLines":[');
pushArrayToStream(coveredLines, this);
this.push('],"uncoveredLines":[');
pushArrayToStream(uncoveredLines, this);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/testService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { isTestResult, isValidApexClassID } from '../narrowing';

export class TestService {
private readonly connection: Connection;
private readonly asyncService: AsyncTests;
public readonly asyncService: AsyncTests;
private readonly syncService: SyncTests;

constructor(connection: Connection) {
Expand Down
82 changes: 82 additions & 0 deletions test/streaming/codeCoverageStringifyStream.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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
*/
import { expect } from 'chai';
import { CodeCoverageStringifyStream } from '../../src/streaming/codeCoverageStringifyStream';
import { PerClassCoverage } from '../../src/tests';

describe('CodeCoverageStringifyStream', () => {
let stream: CodeCoverageStringifyStream;
let data: PerClassCoverage[];

beforeEach(() => {
data = [
{
apexClassOrTriggerName: 'TestClass3',
apexClassOrTriggerId: '01p3h00000KoP4UAAV',
apexTestClassId: '01p3h00000KoP4VAAV',
apexTestMethodName: 'testMethod3',
numLinesCovered: 12,
numLinesUncovered: 3,
percentage: '80.00',
coverage: {
coveredLines: [26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37],
uncoveredLines: [38, 39, 40]
}
},
{
apexClassOrTriggerName: 'TestClass4',
apexClassOrTriggerId: '01p3h00000KoP4WAAV',
apexTestClassId: '01p3h00000KoP4XAAV',
apexTestMethodName: 'testMethod4',
numLinesCovered: 15,
numLinesUncovered: 0,
percentage: '100.00',
coverage: {
coveredLines: [
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55
],
uncoveredLines: []
}
}
];
stream = new CodeCoverageStringifyStream();
});

it('should transform data correctly', (done) => {
const expectedOutput = JSON.stringify([data]);

let output = '';
stream.on('data', (chunk: string) => {
output += chunk;
});

stream.on('end', () => {
expect(output).to.equal(expectedOutput);
done();
});

stream.write(data);
stream.end();
});

it('should handle empty data', (done) => {
const expectedOutput = '[[]]';

let output = '';
stream.on('data', (chunk: string) => {
output += chunk;
});

stream.on('end', () => {
expect(output).to.equal(expectedOutput);
done();
});

stream.write([]);
stream.end();
});
});
16 changes: 13 additions & 3 deletions test/streaming/jsonStringifyStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(json));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -43,15 +44,18 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(json));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});

it('should handle complex objects ending with a key/array', (done) => {
it('should handle complex objects and arrays without dangling commas', (done) => {
const json = {
key1: 'value1',
key2: { key3: 'value3' },
key4: ['value4', 'value5']
key2: { key3: 'value3', key5: ['value6', ['value7', 'value8', null]] },
key4: ['value4', 'value5', null],
// @ts-ignore
key6: { key7: 'value7', key8: null }
};
const stream = JSONStringifyStream.from(json);

Expand All @@ -62,6 +66,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(json));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -81,6 +86,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(json));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -96,6 +102,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(value));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -111,6 +118,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(value));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -126,6 +134,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(value));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -141,6 +150,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(value));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand Down
Loading

0 comments on commit a1001fc

Please sign in to comment.