Skip to content

Commit ae53e7d

Browse files
test: add unit tests
1 parent f067ad3 commit ae53e7d

File tree

6 files changed

+615
-4
lines changed

6 files changed

+615
-4
lines changed

src/commands/apex/get/test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ export default class Test extends SfCommand<RunResult> {
6969
json: flags.json,
7070
detailedCoverage: flags['detailed-coverage'],
7171
concise: flags.concise,
72-
jsonEnabled: this.jsonEnabled(),
73-
isUnifiedLogic: false
72+
jsonEnabled: this.jsonEnabled()
7473
});
7574
}
7675
}

test/commands/apex/get/test.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ import {
1919
testRunSimpleResult,
2020
testRunWithFailuresResult,
2121
} from '../../../testData.js';
22+
import { TestReporter } from '../../../../src/reporters/testReporter.js';
2223

2324
config.truncateThreshold = 0;
2425

2526
let logStub: sinon.SinonStub;
2627
let styledJsonStub: sinon.SinonStub;
28+
let testReporterReportStub: sinon.SinonStub;
2729

2830
describe('apex:test:report', () => {
2931
let sandbox: sinon.SinonSandbox;
@@ -194,4 +196,17 @@ describe('apex:test:report', () => {
194196
expect(logStub.firstCall.args[0]).to.not.contain('Apex Code Coverage by Class');
195197
});
196198
});
199+
200+
describe('isUnifiedLogic parameter', () => {
201+
it('should NOT pass isUnifiedLogic parameter', async () => {
202+
testReporterReportStub = sandbox.stub(TestReporter.prototype, 'report');
203+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(testRunSimple);
204+
testReporterReportStub.resolves({ success: true });
205+
await Test.run(['--test-run-id', '7071w00003woTsc', '--target-org', '[email protected]']);
206+
207+
expect(testReporterReportStub.calledOnce).to.be.true;
208+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
209+
expect(testReporterReportStub.getCall(0).args[1].isUnifiedLogic).to.be.undefined;
210+
});
211+
});
197212
});
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright (c) 2020, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import fs from 'node:fs';
8+
import { Connection, Org } from '@salesforce/core';
9+
import sinon from 'sinon';
10+
import { Ux, stubSfCommandUx } from '@salesforce/sf-plugins-core';
11+
import { expect, config } from 'chai';
12+
import { TestService } from '@salesforce/apex-node';
13+
import LogicTest from '../../../../src/commands/logic/get/test.js';
14+
import {
15+
logicRunWithCoverage,
16+
logicRunWithFailures,
17+
logicTestRunSimple,
18+
logicTestRunSimpleResult,
19+
logicTestRunWithFailuresResult,
20+
testRunSimple,
21+
} from '../../../testData.js';
22+
import { TestReporter } from '../../../../src/reporters/testReporter.js';
23+
24+
config.truncateThreshold = 0;
25+
26+
let logStub: sinon.SinonStub;
27+
let styledJsonStub: sinon.SinonStub;
28+
let testReporterReportStub: sinon.SinonStub;
29+
30+
describe('logic:test:report', () => {
31+
let sandbox: sinon.SinonSandbox;
32+
let uxStub: ReturnType<typeof stubSfCommandUx>;
33+
34+
beforeEach(async () => {
35+
sandbox = sinon.createSandbox();
36+
uxStub = stubSfCommandUx(sandbox);
37+
logStub = sandbox.stub(Ux.prototype, 'log');
38+
styledJsonStub = sandbox.stub(Ux.prototype, 'styledJSON');
39+
sandbox.stub(Connection.prototype, 'getUsername').returns('[email protected]');
40+
41+
sandbox.stub(Org, 'create').resolves({ getConnection: () => ({ getUsername: () => '[email protected]' }) } as Org);
42+
});
43+
44+
afterEach(() => {
45+
sandbox.restore();
46+
47+
try {
48+
// the library writes to a directory, so we need to clean it up :(
49+
fs.rmSync('myDirectory', { recursive: true });
50+
} catch (e) {
51+
// do nothing
52+
}
53+
});
54+
55+
describe('test category', () => {
56+
describe('test failures', () => {
57+
it('should return a success human format message with async and include the test category', async () => {
58+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicRunWithFailures);
59+
60+
const result = await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--result-format', 'human']);
61+
62+
expect(result).to.deep.equal(logicTestRunWithFailuresResult);
63+
expect(logStub.firstCall.args[0]).to.include('TEST NAME CATEGORY');
64+
expect(logStub.firstCall.args[0]).to.include('ApexTests.testConfig Apex');
65+
expect(logStub.firstCall.args[0]).to.include('Fail');
66+
expect(uxStub.log.callCount).to.equal(0);
67+
});
68+
69+
it('should return a success json format message with async and include the test category', async () => {
70+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicRunWithFailures);
71+
const result = await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--result-format', 'json']);
72+
expect(result).to.deep.equal(logicTestRunWithFailuresResult);
73+
expect(styledJsonStub.firstCall.args[0]).to.deep.equal({ result: logicTestRunWithFailuresResult, status: 100 });
74+
});
75+
76+
it('should return a success --json format message with sync and include the test category', async () => {
77+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicRunWithFailures);
78+
sandbox.stub(Org.prototype, 'getUsername').returns('[email protected]');
79+
const result = await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--json']);
80+
expect(result).to.deep.equal(logicTestRunWithFailuresResult);
81+
expect(logStub.firstCall.args[0]).to.include('TEST NAME CATEGORY');
82+
expect(styledJsonStub.notCalled).to.be.true;
83+
});
84+
85+
it('should return a success human format with synchronous and include the test categorys', async () => {
86+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicRunWithFailures);
87+
await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--result-format', 'human']);
88+
expect(logStub.firstCall.args[0]).to.contain('Test Summary');
89+
expect(logStub.firstCall.args[0]).to.contain('Test Results');
90+
expect(logStub.firstCall.args[0]).to.not.contain('Apex Code Coverage by Class');
91+
expect(logStub.firstCall.args[0]).to.include('TEST NAME CATEGORY');
92+
});
93+
94+
it('should only display failed test with human format with concise flag and include the test categorys', async () => {
95+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicRunWithFailures);
96+
await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--result-format', 'human', '--concise']);
97+
expect(logStub.firstCall.args[0]).to.contain('Test Summary');
98+
expect(logStub.firstCall.args[0]).to.contain('Test Results');
99+
expect(logStub.firstCall.args[0]).to.contain('ApexTests.testConfig Apex');
100+
expect(logStub.firstCall.args[0]).to.not.contain('MyPassingTest');
101+
expect(logStub.firstCall.args[0]).to.not.contain('Apex Code Coverage by Class');
102+
expect(logStub.firstCall.args[0]).to.include('TEST NAME CATEGORY');
103+
});
104+
});
105+
106+
describe('test success', () => {
107+
it('should return a success human format message with async and include the test categorys', async () => {
108+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicTestRunSimple);
109+
110+
const result = await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--result-format', 'human']);
111+
112+
expect(result).to.deep.equal(logicTestRunSimpleResult);
113+
expect(logStub.firstCall.args[0]).to.include('=== Test Summary');
114+
expect(logStub.firstCall.args[0]).to.include('=== Test Results');
115+
expect(logStub.firstCall.args[0]).to.include('Test Run Id 707xx0000AUS2gH');
116+
expect(logStub.firstCall.args[0]).to.include('TEST NAME CATEGORY');
117+
expect(logStub.firstCall.args[0]).to.include('MyApexTests.testConfig Apex Pass 53');
118+
});
119+
120+
it('should return a success json format message with async and include the test categorys', async () => {
121+
process.exitCode = 0;
122+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicTestRunSimple);
123+
const result = await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--result-format', 'json']);
124+
expect(result).to.deep.equal(logicTestRunSimpleResult);
125+
expect(styledJsonStub.firstCall.args[0]).to.deep.equal({ result: logicTestRunSimpleResult, status: 0 });
126+
});
127+
128+
it('should return a success --json format message with sync and include the test categorys', async () => {
129+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicTestRunSimple);
130+
sandbox.stub(Org.prototype, 'getUsername').returns('[email protected]');
131+
const result = await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--json']);
132+
expect(result).to.deep.equal(logicTestRunSimpleResult);
133+
expect(styledJsonStub.notCalled).to.be.true;
134+
});
135+
136+
it('should return a success human format with synchronous and include the test categorys', async () => {
137+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicTestRunSimple);
138+
await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--result-format', 'human']);
139+
expect(logStub.firstCall.args[0]).to.contain('Test Summary');
140+
expect(logStub.firstCall.args[0]).to.contain('Test Results');
141+
expect(logStub.firstCall.args[0]).to.not.contain('Apex Code Coverage by Class');
142+
expect(logStub.firstCall.args[0]).to.include('TEST NAME CATEGORY');
143+
expect(logStub.firstCall.args[0]).to.include('MyApexTests.testConfig Apex Pass 53');
144+
});
145+
146+
it('should only display summary with human format and code coverage and concise parameters', async () => {
147+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(logicRunWithCoverage);
148+
await LogicTest.run(['--test-run-id', '707xxxxxxxxxxxx', '--result-format', 'human', '--code-coverage', '--concise']);
149+
expect(logStub.firstCall.args[0]).to.contain('Test Summary');
150+
expect(logStub.firstCall.args[0]).to.not.contain('Test Results');
151+
expect(logStub.firstCall.args[0]).to.not.contain('Apex Code Coverage by Class');
152+
expect(logStub.firstCall.args[0]).to.not.contain('CATEGORY');
153+
});
154+
});
155+
});
156+
157+
158+
describe('isUnifiedLogic parameter', () => {
159+
it('should pass isUnifiedLogic parameter', async () => {
160+
testReporterReportStub = sandbox.stub(TestReporter.prototype, 'report');
161+
sandbox.stub(TestService.prototype, 'reportAsyncResults').resolves(testRunSimple);
162+
testReporterReportStub.resolves({ success: true });
163+
await LogicTest.run(['--test-run-id', '7071w00003woTsc', '--target-org', '[email protected]']);
164+
165+
expect(testReporterReportStub.calledOnce).to.be.true;
166+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
167+
expect(testReporterReportStub.getCall(0).args[1].isUnifiedLogic).to.be.true;
168+
});
169+
});
170+
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import { expect } from 'chai';
8-
import { JsonReporter } from '../src/reporters/index.js';
8+
import { JsonReporter } from '../../src/reporters/index.js';
99
import {
1010
jsonResult,
1111
testRunSimple,
@@ -15,7 +15,7 @@ import {
1515
failureResult,
1616
runWithMixed,
1717
mixedResult,
18-
} from './testData.js';
18+
} from '../testData.js';
1919

2020
describe('JSON Test Reporter', () => {
2121
it('should report successful test results without code coverage', () => {

0 commit comments

Comments
 (0)