|
| 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 | +}); |
0 commit comments