Skip to content

Commit ec9da42

Browse files
committed
test: fix unit tests
1 parent cb631c9 commit ec9da42

File tree

9 files changed

+101
-111
lines changed

9 files changed

+101
-111
lines changed

.mocharc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"require": "ts-node/register,source-map-support/register",
33
"watch-extensions": "ts",
4+
"watch-files": ["src", "test"],
45
"recursive": true,
56
"reporter": "spec",
67
"timeout": 5000

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dependencies": {
99
"@oclif/core": "^1.7.0",
1010
"@salesforce/command": "^5.1.0",
11-
"@salesforce/core": "^3.18.2",
11+
"@salesforce/core": "^3.19.0",
1212
"@salesforce/kit": "^1.5.17",
1313
"open": "8.4.0",
1414
"tslib": "^2"
@@ -112,6 +112,7 @@
112112
"test:deprecation-policy": "./bin/dev snapshot:compare",
113113
"test:nuts": "nyc mocha \"**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel",
114114
"test:nuts:sandbox": "nyc mocha \"**/*.sandboxNut.ts\" --slow 450000 --timeout 7200000",
115+
"test:watch": "mocha --watch \"./test/**/*.test.ts\"",
115116
"version": "oclif readme"
116117
},
117118
"publishConfig": {

src/commands/force/org/status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class OrgStatusCommand extends SfdxCommand {
7272
});
7373
if (results.sandboxRes?.authUserName) {
7474
if (this.flags.setalias) {
75-
const info = await GlobalInfo.create();
75+
const info = await GlobalInfo.getInstance();
7676
info.aliases.update(this.flags.setalias, results.sandboxRes.authUserName);
7777
this.logger.debug('Set Alias: %s result: %s', this.flags.setalias, results.sandboxRes.authUserName);
7878
}

src/shared/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { GlobalInfo } from '@salesforce/core';
99
import * as open from 'open';
1010

1111
export const getAliasByUsername = async (username: string): Promise<string> => {
12-
const globalInfo = await GlobalInfo.create();
12+
const globalInfo = await GlobalInfo.getInstance();
1313
const keys = globalInfo.aliases.getAll(username);
1414
// use the most recently added alias for that username
1515
return keys?.length ? keys[keys.length - 1] : undefined;

test/commands/force/org/delete.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import { Messages, Org, SfdxProject } from '@salesforce/core';
7+
import { Messages, Org, SfProject } from '@salesforce/core';
88
import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon';
99
import * as sinon from 'sinon';
10-
import { expect, IConfig } from '@salesforce/command/lib/test';
10+
import { expect, Config as IConfig } from '@salesforce/command/lib/test';
1111
import { UX } from '@salesforce/command';
1212
import { Delete } from '../../../../src/commands/force/org/delete';
1313

@@ -18,7 +18,7 @@ describe('org:delete', () => {
1818
const sandbox = sinon.createSandbox();
1919
const username = '[email protected]';
2020
const orgId = '00D54000000KDltEAG';
21-
const oclifConfigStub = fromStub(stubInterface<IConfig.IConfig>(sandbox));
21+
const oclifConfigStub = fromStub(stubInterface<IConfig>(sandbox));
2222

2323
// stubs
2424
let resolveProjectConfigStub: sinon.SinonStub;
@@ -34,7 +34,7 @@ describe('org:delete', () => {
3434
public setOrg(org: Org) {
3535
this.org = org;
3636
}
37-
public setProject(project: SfdxProject) {
37+
public setProject(project: SfProject) {
3838
this.project = project;
3939
}
4040
}
@@ -46,7 +46,7 @@ describe('org:delete', () => {
4646
cmd = new TestDelete(params, oclifConfigStub);
4747
stubMethod(sandbox, cmd, 'assignProject').callsFake(() => {
4848
const sfdxProjectStub = fromStub(
49-
stubInterface<SfdxProject>(sandbox, {
49+
stubInterface<SfProject>(sandbox, {
5050
resolveProjectConfig: resolveProjectConfigStub,
5151
})
5252
);

test/commands/force/org/status.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77

8-
import { Org, Aliases, Config, ConfigAggregator, Lifecycle, SandboxEvents } from '@salesforce/core';
8+
import {
9+
Org,
10+
SfdxPropertyKeys,
11+
SfdxConfigAggregator,
12+
GlobalInfo,
13+
ConfigAggregator,
14+
Lifecycle,
15+
SandboxEvents,
16+
} from '@salesforce/core';
917
import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon';
1018
import * as sinon from 'sinon';
11-
import { expect, IConfig } from '@salesforce/command/lib/test';
19+
import { expect, Config as IConfig } from '@salesforce/command/lib/test';
1220
import { UX } from '@salesforce/command';
1321
import { OrgStatusCommand } from '../../../../src/commands/force/org/status';
1422

@@ -43,7 +51,7 @@ describe('org:status', () => {
4351
loginUrl: 'https://my-login.com',
4452
},
4553
};
46-
const oclifConfigStub = fromStub(stubInterface<IConfig.IConfig>(sandbox));
54+
const oclifConfigStub = fromStub(stubInterface<IConfig>(sandbox));
4755

4856
// stubs
4957
let uxTableStub: sinon.SinonStub;
@@ -62,7 +70,7 @@ describe('org:status', () => {
6270
public setOrg(org: Org) {
6371
this.org = org;
6472
}
65-
public setConfigAggregator(configAggregator: ConfigAggregator) {
73+
public setConfigAggregator(configAggregator: SfdxConfigAggregator) {
6674
this.configAggregator = configAggregator;
6775
}
6876
}
@@ -95,7 +103,12 @@ describe('org:status', () => {
95103
uxTableStub = stubMethod(sandbox, UX.prototype, 'table');
96104
stubMethod(sandbox, UX.prototype, 'log');
97105
stubMethod(sandbox, UX.prototype, 'styledHeader');
98-
updateValueStub = stubMethod(sandbox, Aliases.prototype, 'updateValue');
106+
updateValueStub = sinon.stub();
107+
stubMethod(sandbox, GlobalInfo, 'getInstance').returns({
108+
aliases: {
109+
update: updateValueStub,
110+
},
111+
});
99112
return cmd.runIt();
100113
};
101114

@@ -125,7 +138,7 @@ describe('org:status', () => {
125138

126139
it('will set default username', async () => {
127140
const res = await runStatusCommand(['--sandboxname', sanboxname, '--setdefaultusername']);
128-
expect(configSetStub.firstCall.args[0]).to.be.equal(Config.DEFAULT_USERNAME);
141+
expect(configSetStub.firstCall.args[0]).to.be.equal(SfdxPropertyKeys.DEFAULT_USERNAME);
129142
expect(configSetStub.firstCall.args[1]).to.be.equal(authUserName);
130143
expect(configWriteStub.calledOnce).to.be.true;
131144
expect(onStub.secondCall.firstArg).to.be.equal(SandboxEvents.EVENT_RESULT);

test/shared/orgListUtil.test.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7+
import * as fs from 'fs/promises';
78
import { expect } from '@salesforce/command/lib/test';
89
import * as sinon from 'sinon';
9-
import { AuthInfo, ConfigAggregator, fs, Aliases, Org } from '@salesforce/core';
10+
import { AuthInfo, ConfigAggregator, Org } from '@salesforce/core';
1011
import { stubMethod } from '@salesforce/ts-sinon';
1112
import { OrgListUtil } from '../../src/shared/orgListUtil';
1213
import * as utils from '../../src/shared/utils';
@@ -60,7 +61,6 @@ const fileNames = ['[email protected]', '[email protected]'];
6061

6162
describe('orgListUtil tests', () => {
6263
const spies = new Map();
63-
let aliasListStub: sinon.SinonStub;
6464
let determineConnectedStatusForNonScratchOrg: sinon.SinonStub;
6565
let retrieveScratchOrgInfoFromDevHubStub: sinon.SinonStub;
6666
let checkNonScratchOrgIsDevHub: sinon.SinonStub;
@@ -73,7 +73,6 @@ describe('orgListUtil tests', () => {
7373
sandbox.stub(AuthInfo, 'create');
7474

7575
stubMethod(sandbox, OrgListUtil, 'readAuthFiles').resolves([orgAuthConfig, expiredAuthConfig, devHubConfig]);
76-
aliasListStub = stubMethod(sandbox, Aliases, 'fetch').resolves();
7776
determineConnectedStatusForNonScratchOrg = stubMethod(
7877
sandbox,
7978
OrgListUtil,
@@ -96,7 +95,6 @@ describe('orgListUtil tests', () => {
9695
},
9796
});
9897

99-
sandbox.stub(fs, 'readFileSync');
10098
stubMethod(sandbox, fs, 'stat').resolves({ atime: 'test' });
10199

102100
sandbox.stub(utils, 'getAliasByUsername').withArgs('[email protected]').resolves('gaz');
@@ -117,8 +115,6 @@ describe('orgListUtil tests', () => {
117115
// devhub is updated to be true
118116
expect(checkNonScratchOrgIsDevHub.called).to.be.true;
119117
expect(orgs.nonScratchOrgs[0].isDevHub).to.be.true;
120-
121-
expect(aliasListStub.calledOnce).to.be.false;
122118
expect(determineConnectedStatusForNonScratchOrg.calledOnce).to.be.true;
123119
expect(retrieveScratchOrgInfoFromDevHubStub.calledOnce).to.be.true;
124120
});
@@ -133,8 +129,6 @@ describe('orgListUtil tests', () => {
133129

134130
expect(orgs.nonScratchOrgs.every((nonScratchOrg) => nonScratchOrg.connectedStatus === undefined)).to.be.true;
135131

136-
expect(aliasListStub.calledOnce).to.be.false;
137-
expect(aliasListStub.calledOnce).to.be.false;
138132
expect(determineConnectedStatusForNonScratchOrg.called).to.be.false;
139133
});
140134

@@ -245,9 +239,11 @@ describe('orgListUtil tests', () => {
245239
isOauth: () => false,
246240
getUsername: () => orgAuthConfigFields.username,
247241
});
248-
stubMethod(sandbox, fs, 'readJson').resolves({
249-
usernames: [orgAuthConfigFields.username, '[email protected]'],
250-
});
242+
stubMethod(sandbox, fs, 'readFile').resolves(
243+
JSON.stringify({
244+
usernames: [orgAuthConfigFields.username, '[email protected]'],
245+
})
246+
);
251247
const authFiles = await OrgListUtil.readAuthFiles([`${orgAuthConfigFields.username}.json`]);
252248
expect(authFiles.length).to.equal(1);
253249
expect(authFiles[0].getFields()).to.have.property('username').equals(orgAuthConfigFields.username);
@@ -261,9 +257,7 @@ describe('orgListUtil tests', () => {
261257
isOauth: () => false,
262258
getUsername: () => orgAuthConfigFields.username,
263259
});
264-
stubMethod(sandbox, fs, 'readJson').resolves({
265-
usernames: ['[email protected]'],
266-
});
260+
stubMethod(sandbox, fs, 'readFile').resolves('{"usernames":["[email protected]"]}');
267261
const authFiles = await OrgListUtil.readAuthFiles([`${orgAuthConfigFields.username}.json`]);
268262
expect(authFiles.length).to.equal(0);
269263
});
@@ -276,9 +270,11 @@ describe('orgListUtil tests', () => {
276270
isOauth: () => false,
277271
getUsername: () => orgAuthConfigFields.username,
278272
});
279-
stubMethod(sandbox, fs, 'readJson').resolves({
280-
usernames: ['[email protected]', orgAuthConfigFields.username],
281-
});
273+
stubMethod(sandbox, fs, 'readFile').resolves(
274+
JSON.stringify({
275+
usernames: ['[email protected]', orgAuthConfigFields.username],
276+
})
277+
);
282278
const authFiles = await OrgListUtil.readAuthFiles([`${orgAuthConfigFields.username}.json`]);
283279
expect(authFiles.length).to.equal(0);
284280
});

test/shared/utils.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
*/
77
import { expect } from '@salesforce/command/lib/test';
88
import * as sinon from 'sinon';
9-
import { Aliases } from '@salesforce/core';
9+
import { GlobalInfo } from '@salesforce/core';
1010
import { stubMethod } from '@salesforce/ts-sinon';
1111
import { getAliasByUsername } from '../../src/shared/utils';
1212

1313
describe('getAliasByUsername', () => {
1414
const sandbox = sinon.createSandbox();
1515
beforeEach(async () => {
16-
stubMethod(sandbox, Aliases, 'create').resolves(Aliases.prototype);
17-
stubMethod(sandbox, Aliases, 'getDefaultOptions').returns({});
18-
stubMethod(sandbox, Aliases.prototype, 'getKeysByValue')
19-
.withArgs('username1')
20-
.returns(['alias1'])
21-
.withArgs('username2')
22-
.returns(['alias2', 'alias2b']);
16+
const getAllStub = sandbox.stub();
17+
getAllStub.withArgs('username1').returns(['alias1']);
18+
getAllStub.withArgs('username2').returns(['alias2', 'alias2b']);
19+
20+
stubMethod(sandbox, GlobalInfo, 'getInstance').resolves({
21+
aliases: {
22+
getAll: getAllStub,
23+
},
24+
});
2325
});
2426
afterEach(() => {
2527
sandbox.restore();

0 commit comments

Comments
 (0)