Skip to content

Commit

Permalink
test: fix UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Nov 1, 2023
1 parent 002af63 commit 6bd3c84
Show file tree
Hide file tree
Showing 39 changed files with 394 additions and 368 deletions.
48 changes: 27 additions & 21 deletions schemas/force-source-status.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,7 @@
"type": "object",
"properties": {
"state": {
"type": "string",
"enum": [
"Local Deleted",
"Local Add",
"Local Changed",
"Local Unchanged",
"Remote Deleted",
"Remote Add",
"Remote Changed",
"Remote Unchanged",
"Local Deleted (Conflict)",
"Local Add (Conflict)",
"Local Changed (Conflict)",
"Local Unchanged (Conflict)",
"Remote Deleted (Conflict)",
"Remote Add (Conflict)",
"Remote Changed (Conflict)",
"Remote Unchanged (Conflict)"
]
"$ref": "#/definitions/StatusStateString"
},
"fullName": {
"type": "string"
Expand All @@ -52,12 +34,36 @@
"enum": ["Deleted", "Add", "Changed", "Unchanged"]
},
"origin": {
"type": "string",
"enum": ["Local", "Remote"]
"$ref": "#/definitions/StatusOrigin"
}
},
"required": ["state", "fullName", "type", "origin"],
"additionalProperties": false
},
"StatusStateString": {
"type": "string",
"enum": [
"Local Deleted",
"Local Add",
"Local Changed",
"Local Unchanged",
"Remote Deleted",
"Remote Add",
"Remote Changed",
"Remote Unchanged",
"Local Deleted (Conflict)",
"Local Add (Conflict)",
"Local Changed (Conflict)",
"Local Unchanged (Conflict)",
"Remote Deleted (Conflict)",
"Remote Add (Conflict)",
"Remote Changed (Conflict)",
"Remote Unchanged (Conflict)"
]
},
"StatusOrigin": {
"type": "string",
"enum": ["Local", "Remote"]
}
}
}
7 changes: 3 additions & 4 deletions src/hooks/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,15 @@ const apiVersionTest = async (doctor: SfDoctor): Promise<void> => {
};

// check sfdx-project.json for sourceApiVersion
const getSourceApiVersion = async (): Promise<string> => {
const getSourceApiVersion = async (): Promise<string | undefined> => {
try {
const project = SfProject.getInstance();
const projectJson = await project.resolveProjectConfig();
return projectJson.sourceApiVersion as string;
return projectJson.sourceApiVersion as string | undefined;
} catch (error) {
const errMsg = (error as Error).message;
getLogger().debug(`Cannot determine sourceApiVersion due to: ${errMsg}`);
}
return '';
};

// check max API version for default orgs
Expand All @@ -139,7 +138,7 @@ const getMaxApiVersion = async (aggregator: ConfigAggregator, aliasOrUsername: s
// Comparing undefined with undefined would return false.
// Comparing 55.0 with 55.0 would return false.
// Comparing 55.0 with 56.0 would return true.
const diff = (version1: string, version2: string): boolean => {
const diff = (version1: string | undefined, version2: string | undefined): boolean => {
getLogger().debug(`Comparing API versions: [${version1},${version2}]`);
return (version1?.length && version2?.length && version1 !== version2) as boolean;
};
5 changes: 2 additions & 3 deletions test/commands/source/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ describe('force:source:deploy', () => {
const runDeployCmd = async (params: string[], options?: { sourceApiVersion?: string }) => {
const cmd = new TestDeploy(params, oclifConfigStub);
cmd.project = SfProject.getInstance();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
cmd.configAggregator = await (await ConfigAggregator.create({ customConfigMeta: ConfigMeta })).reload();

cmd.configAggregator = await (await ConfigAggregator.create({ customConfigMeta: ConfigMeta.default })).reload();
sandbox.stub(cmd.project, 'getDefaultPackage').returns({ name: '', path: '', fullPath: defaultDir });
sandbox.stub(cmd.project, 'getUniquePackageDirectories').returns([{ fullPath: defaultDir, path: '', name: '' }]);
sandbox.stub(cmd.project, 'getPackageDirectories').returns([{ fullPath: defaultDir, path: '', name: '' }]);
Expand Down
6 changes: 3 additions & 3 deletions test/coverageUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { expect } from 'chai';
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup.js';
import { AuthInfo, Connection } from '@salesforce/core';
import { createSandbox, SinonSandbox } from 'sinon';
import sinon from 'sinon';
import chalk from 'chalk';
import {
prepCoverageForDisplay,
Expand Down Expand Up @@ -298,10 +298,10 @@ describe('transform md RunTestResult', () => {
const testData = new MockTestOrgData();
let sampleTestResult = getSampleTestResult();

let sandboxStub: SinonSandbox;
let sandboxStub: sinon.SinonSandbox;
beforeEach(async () => {
sampleTestResult = getSampleTestResult();
sandboxStub = createSandbox();
sandboxStub = sinon.createSandbox();

$$.setConfigStubContents('StateAggregator', {
contents: {
Expand Down
29 changes: 16 additions & 13 deletions test/formatters/mdDeployResultFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('mdDeployResultFormatter', () => {
const deployResultTestSuccess = getDeployResult('passedTest');
const deployResultTestSuccessAndFailure = getDeployResult('passedAndFailedTest');

let ux;
let ux: Ux;

let logStub: sinon.SinonStub;
let styledHeaderStub: sinon.SinonStub;
Expand All @@ -32,6 +32,9 @@ describe('mdDeployResultFormatter', () => {
logStub = sandbox.stub();
styledHeaderStub = sandbox.stub();
tableStub = sandbox.stub();
// ux is a stubbed Ux
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
ux = stubInterface<Ux>(sandbox, {
log: logStub,
styledHeader: styledHeaderStub,
Expand All @@ -49,7 +52,7 @@ describe('mdDeployResultFormatter', () => {
it('should return expected json for a success', () => {
process.exitCode = 0;
const expectedSuccessResults = deployResultSuccess.response;
const formatter = new MdDeployResultFormatter(ux as Ux, {}, deployResultSuccess);
const formatter = new MdDeployResultFormatter(ux, {}, deployResultSuccess);
const json = formatter.getJson();

expect(json).to.deep.equal(expectedSuccessResults);
Expand All @@ -59,22 +62,22 @@ describe('mdDeployResultFormatter', () => {
process.exitCode = 1;

const expectedFailureResults = deployResultFailure.response;
const formatter = new MdDeployResultFormatter(ux as Ux, {}, deployResultFailure);
const formatter = new MdDeployResultFormatter(ux, {}, deployResultFailure);
expect(formatter.getJson()).to.deep.equal(expectedFailureResults);
});

it('should return expected json for a partial success', () => {
process.exitCode = 69;
const expectedPartialSuccessResponse = deployResultPartialSuccess.response;
const formatter = new MdDeployResultFormatter(ux as Ux, {}, deployResultPartialSuccess);
const formatter = new MdDeployResultFormatter(ux, {}, deployResultPartialSuccess);
expect(formatter.getJson()).to.deep.equal(expectedPartialSuccessResponse);
});

it('should omit successes when used with concise', () => {
process.exitCode = 0;
const expectedSuccessResults = deployResultSuccess.response;

const formatter = new MdDeployResultFormatter(ux as Ux, { concise: true }, deployResultSuccess);
const formatter = new MdDeployResultFormatter(ux, { concise: true }, deployResultSuccess);
const json = formatter.getJson();

// a few checks that it's the rest of the json
Expand All @@ -88,7 +91,7 @@ describe('mdDeployResultFormatter', () => {
describe('display', () => {
it('should output as expected for a success (no table)', () => {
process.exitCode = 0;
const formatter = new MdDeployResultFormatter(ux as Ux, {}, deployResultSuccess);
const formatter = new MdDeployResultFormatter(ux, {}, deployResultSuccess);
formatter.display();
expect(logStub.callCount).to.equal(0);
expect(tableStub.callCount).to.equal(0);
Expand All @@ -97,7 +100,7 @@ describe('mdDeployResultFormatter', () => {

it('should output as expected for a verbose success (has table)', () => {
process.exitCode = 0;
const formatter = new MdDeployResultFormatter(ux as Ux, { verbose: true }, deployResultSuccess);
const formatter = new MdDeployResultFormatter(ux, { verbose: true }, deployResultSuccess);
formatter.display();
expect(styledHeaderStub.callCount).to.equal(1);
expect(logStub.callCount).to.equal(1);
Expand All @@ -108,7 +111,7 @@ describe('mdDeployResultFormatter', () => {

it('should output as expected for a failure and exclude duplicate information', () => {
process.exitCode = 1;
const formatter = new MdDeployResultFormatter(ux as Ux, {}, deployResultFailure);
const formatter = new MdDeployResultFormatter(ux, {}, deployResultFailure);

try {
formatter.display();
Expand All @@ -128,7 +131,7 @@ describe('mdDeployResultFormatter', () => {
deployFailure.response.details.componentFailures = [];
deployFailure.response.details.componentSuccesses = [];
delete deployFailure.response.details.runTestResult;
const formatter = new MdDeployResultFormatter(ux as Ux, {}, deployFailure);
const formatter = new MdDeployResultFormatter(ux, {}, deployFailure);
sandbox.stub(formatter, 'isSuccess').returns(false);

try {
Expand All @@ -144,7 +147,7 @@ describe('mdDeployResultFormatter', () => {

it('should output as expected for a test failure with verbose', () => {
process.exitCode = 1;
const formatter = new MdDeployResultFormatter(ux as Ux, { verbose: true }, deployResultTestFailure);
const formatter = new MdDeployResultFormatter(ux, { verbose: true }, deployResultTestFailure);
try {
formatter.display();
throw new Error('should have thrown');
Expand All @@ -161,7 +164,7 @@ describe('mdDeployResultFormatter', () => {

it('should output as expected for passing tests with verbose', () => {
process.exitCode = 0;
const formatter = new MdDeployResultFormatter(ux as Ux, { verbose: true }, deployResultTestSuccess);
const formatter = new MdDeployResultFormatter(ux, { verbose: true }, deployResultTestSuccess);
formatter.display();
expect(styledHeaderStub.args[0][0]).to.include('Deployed Source');
expect(styledHeaderStub.args[1][0]).to.include('Component Failures [1]');
Expand All @@ -171,7 +174,7 @@ describe('mdDeployResultFormatter', () => {

it('should output as expected for passing and failing tests with verbose', () => {
process.exitCode = 1;
const formatter = new MdDeployResultFormatter(ux as Ux, { verbose: true }, deployResultTestSuccessAndFailure);
const formatter = new MdDeployResultFormatter(ux, { verbose: true }, deployResultTestSuccessAndFailure);
try {
formatter.display();
throw new Error('should have thrown');
Expand All @@ -188,7 +191,7 @@ describe('mdDeployResultFormatter', () => {

it('shows success AND failures for partialSucceeded', () => {
process.exitCode = 69;
const formatter = new MdDeployResultFormatter(ux as Ux, { verbose: true }, deployResultPartialSuccess);
const formatter = new MdDeployResultFormatter(ux, { verbose: true }, deployResultPartialSuccess);
formatter.display();
expect(styledHeaderStub.callCount, 'styledHeaderStub.callCount').to.equal(2);
expect(logStub.callCount, 'logStub.callCount').to.equal(3);
Expand Down
20 changes: 11 additions & 9 deletions test/formatters/pullFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('PullFormatter', () => {
const retrieveResultEmpty = getRetrieveResult('empty');
const retrieveResultWarnings = getRetrieveResult('warnings');

let ux;
let ux: Ux;
let logStub: sinon.SinonStub;
let styledHeaderStub: sinon.SinonStub;
let tableStub: sinon.SinonStub;
Expand All @@ -42,6 +42,8 @@ describe('PullFormatter', () => {
logStub = sandbox.stub();
styledHeaderStub = sandbox.stub();
tableStub = sandbox.stub();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
ux = stubInterface<Ux>(sandbox, {
log: logStub,
styledHeader: styledHeaderStub,
Expand All @@ -58,14 +60,14 @@ describe('PullFormatter', () => {
it('should return expected json for a success', () => {
process.exitCode = 0;
const expectedSuccessResults: PullResponse['pulledSource'] = retrieveResultSuccess.getFileResponses();
const formatter = new PullResultFormatter(ux as Ux, {}, retrieveResultSuccess);
const formatter = new PullResultFormatter(ux, {}, retrieveResultSuccess);
expect(formatter.getJson().pulledSource).to.deep.equal(expectedSuccessResults);
});

it('should return expected json for a failure', () => {
process.exitCode = 1;
const expectedFailureResults: PullResponse['pulledSource'] = retrieveResultFailure.getFileResponses();
const formatter = new PullResultFormatter(ux as Ux, {}, retrieveResultFailure);
const formatter = new PullResultFormatter(ux, {}, retrieveResultFailure);
try {
formatter.getJson().pulledSource;
throw new Error('should have thrown');
Expand All @@ -79,14 +81,14 @@ describe('PullFormatter', () => {

it('should return expected json for an InProgress', () => {
const expectedInProgressResults: PullResponse['pulledSource'] = retrieveResultInProgress.getFileResponses();
const formatter = new PullResultFormatter(ux as Ux, {}, retrieveResultInProgress);
const formatter = new PullResultFormatter(ux, {}, retrieveResultInProgress);
expect(formatter.getJson().pulledSource).to.deep.equal(expectedInProgressResults);
});

describe('display', () => {
it('should output as expected for a success', () => {
process.exitCode = 0;
const formatter = new PullResultFormatter(ux as Ux, {}, retrieveResultSuccess);
const formatter = new PullResultFormatter(ux, {}, retrieveResultSuccess);
formatter.display();
expect(styledHeaderStub.called).to.equal(true);
expect(logStub.called).to.equal(false);
Expand All @@ -100,7 +102,7 @@ describe('PullFormatter', () => {
it('should output as expected for an InProgress', () => {
process.exitCode = 68;
const options = { waitTime: 33 };
const formatter = new PullResultFormatter(ux as Ux, options, retrieveResultInProgress);
const formatter = new PullResultFormatter(ux, options, retrieveResultInProgress);
formatter.display();
expect(styledHeaderStub.called).to.equal(false);
expect(logStub.called).to.equal(true);
Expand All @@ -112,7 +114,7 @@ describe('PullFormatter', () => {

it('should output as expected for a Failure', () => {
process.exitCode = 1;
const formatter = new PullResultFormatter(ux as Ux, {}, retrieveResultFailure);
const formatter = new PullResultFormatter(ux, {}, retrieveResultFailure);
sandbox.stub(formatter, 'isSuccess').returns(false);

formatter.display();
Expand All @@ -123,7 +125,7 @@ describe('PullFormatter', () => {

it('should output as expected for warnings', () => {
process.exitCode = 0;
const formatter = new PullResultFormatter(ux as Ux, {}, retrieveResultWarnings);
const formatter = new PullResultFormatter(ux, {}, retrieveResultWarnings);
formatter.display();
// Should call styledHeader for warnings and the standard "Retrieved Source" header
expect(styledHeaderStub.calledTwice).to.equal(true);
Expand All @@ -137,7 +139,7 @@ describe('PullFormatter', () => {

it('should output a message when no results were returned', () => {
process.exitCode = 0;
const formatter = new PullResultFormatter(ux as Ux, {}, retrieveResultEmpty);
const formatter = new PullResultFormatter(ux, {}, retrieveResultEmpty);
formatter.display();
expect(styledHeaderStub.called).to.equal(true);
expect(logStub.called).to.equal(true);
Expand Down
Loading

0 comments on commit 6bd3c84

Please sign in to comment.