Skip to content

Commit

Permalink
test: remove stubMethod to drop ts-sinon devDep
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Nov 13, 2023
1 parent 627de3b commit 77da5aa
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 215 deletions.
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
"@oclif/core": "^2.15.0",
"@salesforce/core": "^5.3.14",
"@salesforce/core": "^5.3.20",
"@salesforce/kit": "^3.0.15",
"@salesforce/packaging": "^2.4.19",
"@salesforce/packaging": "^2.4.21",
"@salesforce/sf-plugins-core": "^3.1.25",
"chalk": "^4.1.2",
"tslib": "^2"
},
"devDependencies": {
"@oclif/plugin-command-snapshot": "^4.0.14",
"@salesforce/cli-plugins-testkit": "^4.4.12",
"@salesforce/dev-scripts": "^6.0.3",
"@salesforce/plugin-auth": "^2.8.25",
"@salesforce/dev-scripts": "^6.0.4",
"@salesforce/plugin-command-reference": "^3.0.46",
"@salesforce/ts-sinon": "^1.4.19",
"@salesforce/ts-types": "^2.0.6",
"@swc/core": "1.3.39",
"eslint-plugin-sf-plugin": "^1.16.10",
"eslint-plugin-sf-plugin": "^1.16.15",
"oclif": "^3.16.0",
"shx": "0.3.4",
"ts-node": "^10.9.1",
Expand Down
108 changes: 51 additions & 57 deletions test/commands/package/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { EOL } from 'node:os';
import { resolve } from 'node:path';
import { Connection, Lifecycle, SfProject, SfError, SfProjectJson } from '@salesforce/core';
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
import { stubMethod } from '@salesforce/ts-sinon';
import { Config } from '@oclif/core';
import { expect } from 'chai';
import { PackageEvents, PackagingSObjects, SubscriberPackageVersion } from '@salesforce/packaging';
Expand Down Expand Up @@ -43,7 +42,7 @@ const pkgInstallRequest = {
SkipHandlers: null,
Status: 'IN_PROGRESS',
Errors: null,
};
} satisfies PackagingSObjects.PackageInstallRequest;

const pkgInstallCreateRequest = {
SubscriberPackageVersionKey: myPackageVersion04t,
Expand Down Expand Up @@ -126,7 +125,7 @@ describe('package:install', () => {
getExternalSitesStub = $$.SANDBOX.stub();
installStub = $$.SANDBOX.stub();
installStatusStub = $$.SANDBOX.stub();
uxConfirmStub = stubMethod($$.SANDBOX, SfCommand.prototype, 'confirm');
uxConfirmStub = $$.SANDBOX.stub(SfCommand.prototype, 'confirm');

// The SubscriberPackageVersion class is tested in the packaging library, so
// we just stub the public APIs used by the command.
Expand Down Expand Up @@ -164,8 +163,8 @@ describe('package:install', () => {
});

it('should print IN_PROGRESS status correctly', async () => {
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);

const cmd = new Install(['-p', myPackageVersion04t, '-o', testOrg.username], config);
stubSpinner(cmd);
Expand All @@ -181,8 +180,8 @@ describe('package:install', () => {
it('should print IN_PROGRESS status when timed out', async () => {
const error = new SfError('polling timed out', 'PackageInstallTimeout');
error.setData(pkgInstallRequest);
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').throws(error);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').throws(error);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
const cmd = new Install(['-p', myPackageVersion04t, '-u', testOrg.username], config);
stubSpinner(cmd);
const result = await cmd.run();
Expand All @@ -199,8 +198,8 @@ describe('package:install', () => {
it('should return PackageInstallRequest when polling timed out with --json', async () => {
const error = new SfError('polling timed out', 'PackageInstallTimeout');
error.setData(pkgInstallRequest);
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').throws(error);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').throws(error);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
const cmd = new Install(['-p', myPackageVersion04t, '--json', '-o', testOrg.username], config);
stubSpinner(cmd);
const result = await cmd.run();
Expand All @@ -210,8 +209,8 @@ describe('package:install', () => {

it('should print SUCCESS status correctly', async () => {
const request = Object.assign({}, pkgInstallRequest, { Status: 'SUCCESS' });
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(request);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(request);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);

const cmd = new Install(['-p', myPackageVersion04t, '-o', testOrg.username], config);
stubSpinner(cmd);
Expand All @@ -226,8 +225,8 @@ describe('package:install', () => {

it('should throw error for ERROR status and no install errors', async () => {
const request = Object.assign({}, pkgInstallRequest, { Status: 'ERROR' });
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(request);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(request);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
try {
const cmd = new Install(['-p', myPackageVersion04t, '-o', testOrg.username], config);
stubSpinner(cmd);
Expand All @@ -245,8 +244,8 @@ describe('package:install', () => {
Status: 'ERROR',
Errors: { errors: [new Error('message 1'), new Error('message 2')] },
});
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(request);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(request);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
try {
const cmd = new Install(['-p', myPackageVersion04t, '-o', testOrg.username], config);
stubSpinner(cmd);
Expand Down Expand Up @@ -282,23 +281,26 @@ describe('package:install', () => {
it('should print SUCCESS status correctly for package alias', async () => {
// Stubs SfProject.getInstance, SfProject.getSfProjectJson, and SfProjectJson.getContents
// in a way that makes TS happy... all to test package aliases.
const getContentsStub = stubMethod($$.SANDBOX, SfProjectJson.prototype, 'getContents').returns({
const getContentsStub = $$.SANDBOX.stub(SfProjectJson.prototype, 'getContents').returns({
packageAliases: { ['my_package_alias']: myPackageVersion04t },
packageDirectories: [],
});
const getSfProjectJsonStub = stubMethod($$.SANDBOX, SfProject.prototype, 'getSfProjectJson').callsFake(() => ({
// @ts-expect-error stubbing only 1 method
const getSfProjectJsonStub = $$.SANDBOX.stub(SfProject.prototype, 'getSfProjectJson').callsFake(() => ({
getContents: getContentsStub,
}));
const getPackageIdFromAliasStub = stubMethod($$.SANDBOX, SfProject.prototype, 'getPackageIdFromAlias').returns(
const getPackageIdFromAliasStub = $$.SANDBOX.stub(SfProject.prototype, 'getPackageIdFromAlias').returns(
myPackageVersion04t
);
stubMethod($$.SANDBOX, SfProject, 'getInstance').callsFake(() => ({
// @ts-expect-error stubbing only a subset of methods
$$.SANDBOX.stub(SfProject, 'getInstance').callsFake(() => ({
getSfProjectJson: getSfProjectJsonStub,
getPackageIdFromAlias: getPackageIdFromAliasStub,
}));

const request = Object.assign({}, pkgInstallRequest, { Status: 'SUCCESS' });
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(request);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(request);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
const cmd = new Install(['-p', 'my_package_alias', '-o', testOrg.username], config);
stubSpinner(cmd);
const result = await cmd.run();
Expand All @@ -312,8 +314,8 @@ describe('package:install', () => {
it('should use installation key as password', async () => {
const installationkey = '1234abcd';
const expectedCreateRequest = Object.assign({}, pkgInstallCreateRequest, { Password: installationkey });
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
const cmd = new Install(['-p', myPackageVersion04t, '-k', installationkey, '-o', testOrg.username], config);
stubSpinner(cmd);
const result = await cmd.run();
Expand All @@ -329,8 +331,8 @@ describe('package:install', () => {
SkipHandlers: 'FeatureEnforcement',
};
const expectedCreateRequest = Object.assign({}, pkgInstallCreateRequest, overrides);
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);

const cmd = new Install(
[
Expand Down Expand Up @@ -358,11 +360,11 @@ describe('package:install', () => {

it('should listen for PackageInstallRequest:warning events and log warnings', async () => {
const warningMsg = 'test warning message';
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').callsFake(async () => {
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').callsFake(async () => {
await Lifecycle.getInstance().emit(PackageEvents.install.warning, warningMsg);
return pkgInstallRequest;
});
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);

const cmd = new Install(['-p', myPackageVersion04t, '-o', testOrg.username], config);
stubSpinner(cmd);
Expand All @@ -377,17 +379,13 @@ describe('package:install', () => {

it('should listen for Package/install-status polling events and log statuses', async () => {
const successRequest = Object.assign({}, pkgInstallRequest, { Status: 'SUCCESS' });
const waitForPublishStub = stubMethod(
$$.SANDBOX,
SubscriberPackageVersion.prototype,
'waitForPublish'
).resolves();
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').callsFake(async () => {
const waitForPublishStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'waitForPublish').resolves();
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').callsFake(async () => {
await Lifecycle.getInstance().emit(PackageEvents.install.status, pkgInstallRequest);
await Lifecycle.getInstance().emit(PackageEvents.install.status, successRequest);
return pkgInstallRequest;
});
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);

const cmd = new Install(['-p', myPackageVersion04t, '-w', '1', '-o', testOrg.username], config);
stubSpinner(cmd);
Expand All @@ -401,12 +399,8 @@ describe('package:install', () => {

it('should listen for Package/install-status and Package/install/subscriber-status polling events and log statuses', async () => {
const successRequest = Object.assign({}, pkgInstallRequest, { Status: 'SUCCESS' });
const waitForPublishStub = stubMethod(
$$.SANDBOX,
SubscriberPackageVersion.prototype,
'waitForPublish'
).resolves();
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').callsFake(async () => {
const waitForPublishStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'waitForPublish').resolves();
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').callsFake(async () => {
await Lifecycle.getInstance().emit(
PackageEvents.install['subscriber-status'],
'PACKAGE_UNAVAILABLE' as InstallValidationStatus
Expand All @@ -419,7 +413,7 @@ describe('package:install', () => {
await Lifecycle.getInstance().emit(PackageEvents.install.status, successRequest);
return pkgInstallRequest;
});
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves(subscriberPackageVersion);

const cmd = new Install(['-p', myPackageVersion04t, '-w', '1', '-b', '2', '-o', testOrg.username], config);
stubSpinner(cmd);
Expand All @@ -443,8 +437,8 @@ describe('package:install', () => {

describe('confirm upgrade type', () => {
it('should NOT confirm UpgradeType with --no-prompt flag', async () => {
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves({
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({
...subscriberPackageVersion,
Package2ContainerOptions: 'Unlocked',
});
Expand All @@ -461,8 +455,8 @@ describe('package:install', () => {
});

it('should confirm UpgradeType when NO --no-prompt flag', async () => {
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves({
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({
...subscriberPackageVersion,
Package2ContainerOptions: 'Unlocked',
});
Expand All @@ -477,8 +471,8 @@ describe('package:install', () => {
});

it('should confirm UpgradeType and throw with no consent', async () => {
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves({
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({
...subscriberPackageVersion,
Package2ContainerOptions: 'Unlocked',
});
Expand All @@ -495,8 +489,8 @@ describe('package:install', () => {
});

it('should NOT confirm UpgradeType with non-Unlocked packages', async () => {
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves({
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({
...subscriberPackageVersion,
Package2ContainerOptions: 'Managed',
});
Expand All @@ -517,10 +511,10 @@ describe('package:install', () => {

it('should NOT confirm external sites with --no-prompt flag', async () => {
const expectedCreateRequest = Object.assign({}, pkgInstallCreateRequest, { EnableRss: true });
getExternalSitesStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'getExternalSites').resolves(
getExternalSitesStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'getExternalSites').resolves(
extSites
);
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);

const cmd = new Install(['-p', myPackageVersion04t, '--no-prompt', '-o', testOrg.username], config);
stubSpinner(cmd);
Expand All @@ -538,8 +532,8 @@ describe('package:install', () => {
EnableRss: true,
Password: installationkey,
});
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves({
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({
...subscriberPackageVersion,
RemoteSiteSettings: { settings: ['url/for/site1'] },
CspTrustedSites: { settings: ['url/for/site2'] },
Expand All @@ -559,8 +553,8 @@ describe('package:install', () => {

it('should confirm external sites when NO --no-prompt flag (yes answer)', async () => {
const expectedCreateRequest = Object.assign({}, pkgInstallCreateRequest, { EnableRss: true });
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves({
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({
...subscriberPackageVersion,
RemoteSiteSettings: { settings: [{ url: 'url/for/site1' }] },
CspTrustedSites: { settings: [{ endpointUrl: 'url/for/site2' }] },
Expand All @@ -578,8 +572,8 @@ describe('package:install', () => {
});

it('should confirm external sites when NO --no-prompt flag (no answer)', async () => {
installStub = stubMethod($$.SANDBOX, SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves({
installStub = $$.SANDBOX.stub(SubscriberPackageVersion.prototype, 'install').resolves(pkgInstallRequest);
$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({
...subscriberPackageVersion,
RemoteSiteSettings: { settings: [{ url: 'url/for/site1' }] },
CspTrustedSites: { settings: [{ endpointUrl: 'url/for/site2' }] },
Expand Down
Loading

0 comments on commit 77da5aa

Please sign in to comment.