Skip to content

Commit

Permalink
test: for scratchOrgCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
maggiben committed Dec 30, 2021
1 parent 2bfc06d commit 605642b
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 3 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
"args": ["--timeout", "999999", "dist/test/unit/**/*.js"],
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/dist/**/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "Debug Active Test",
"protocol": "inspector",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": ["--inspect", "--no-timeouts", "--colors", "${file}"],
"console": "integratedTerminal",
"sourceMaps": true,
"internalConsoleOptions": "openOnSessionStart",
"env": {
"NODE_ENV": "testing"
}
}
]
}
8 changes: 6 additions & 2 deletions src/scratchOrgCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ensureString, getString } from '@salesforce/ts-types';
// Local
import { Org } from './org';
import { Logger } from './logger';
import { AuthInfo } from './authInfo';
import { AuthInfo, AuthFields } from './authInfo';
import { Messages } from './messages';
import { SfdxError } from './sfdxError';
import { Connection } from './connection';
Expand All @@ -38,6 +38,7 @@ export interface ScratchOrgCreateResult {
username?: string;
scratchOrgInfo?: ScratchOrgInfo;
authInfo?: AuthInfo;
authFields?: AuthFields;
warnings: string[];
}

Expand Down Expand Up @@ -185,7 +186,9 @@ export const scratchOrgCreate = async (options: ScratchOrgCreateOptions): Promis
});

// we'll need this scratch org connection later;
const scratchOrg = await Org.create({ connection: await Connection.create({ authInfo: scratchOrgAuthInfo }) }); // scartchOrg should come from command
const connection = await Connection.create({ authInfo: scratchOrgAuthInfo });
const scratchOrg = await Org.create({ connection }); // scartchOrg should come from command

const username = scratchOrg.getUsername();

const configAggregator = new ConfigAggregator();
Expand All @@ -210,6 +213,7 @@ export const scratchOrgCreate = async (options: ScratchOrgCreateOptions): Promis
username,
scratchOrgInfo: scratchOrgInfoResult,
authInfo,
authFields: authInfo?.getFields(),
warnings,
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/scratchOrgInfoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ const getAuthInfo = async (options: {
throw error.lastError || error;
}
} else {
return await AuthInfo.create({
return AuthInfo.create({
username: options.username,
parentUsername: options.hubOrg.getUsername(),
oauth2Options: options.oauth2Options,
Expand Down
110 changes: 110 additions & 0 deletions test/unit/scratchOrgCreateTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { expect } from 'chai';
import * as sinon from 'sinon';
import { SObject } from 'jsforce'; // RecordResult
import { Org } from '../../src/org';
import { Connection } from '../../src/connection';
import { AuthInfo } from '../../src/authInfo';
import { scratchOrgCreate, ScratchOrgCreateOptions } from '../../src/scratchOrgCreate';
import { SfdxProjectJson } from '../../src/sfdxProject';
import { Messages } from '../../src/messages';

Messages.importMessagesDirectory(__dirname);
// const messages = Messages.loadMessages('@salesforce/core', 'scratchOrgCreate');

const packageId = '05iB0000000cWwnIAE';
const packageVersionSubscriberId = '04tB0000000cWwnIAE';
const clientId = 'MyClientId';

describe('scratchOrgCreate', () => {
const sandbox = sinon.createSandbox();
const hubOrgStub = sinon.createStubInstance(Org);
const authInfoStub = sinon.createStubInstance(AuthInfo);
const sfdxProjectJsonStub = sinon.createStubInstance(SfdxProjectJson);
const scratchOrgInfoId = '1234';
const username = 'PlatformCLI';
const retrieve = {
Status: 'Active',
};
beforeEach(() => {
sfdxProjectJsonStub.getPackageDirectories.resolves([
{ path: 'foo', package: 'fooPkgName', versionNumber: '4.7.0.NEXT', ancestorId: packageId },
]);
sandbox.stub(Connection, 'create').resolves();
sandbox.stub(Org, 'create').resolves(hubOrgStub);
sandbox.stub(AuthInfo, 'create').resolves(authInfoStub);
hubOrgStub.isDevHubOrg.returns(false);
hubOrgStub.determineIfDevHubOrg.withArgs(true).resolves();
hubOrgStub.getConnection.returns({
getAuthInfoFields: sandbox.stub().returns({
clientId,
}),
tooling: {
sobject: sandbox.stub().returns({
find: sandbox
.stub()
.withArgs({ RevisionCounter: { $gt: 0 } }, ['Id'])
.resolves([
{
Id: '1234',
},
]),
update: sandbox.spy(),
}),
},
sobject: sandbox
.stub()
.withArgs('ScratchOrgInfo')
.returns({
create: sinon.stub().resolves({
id: scratchOrgInfoId,
}),
retrieve: sinon.stub().withArgs(scratchOrgInfoId).resolves(retrieve),
} as unknown as SObject<unknown>),
singleRecordQuery: sandbox
.stub()
.withArgs(`SELECT Id FROM Package2Version WHERE SubscriberPackageVersionId = '${packageVersionSubscriberId}'`, {
tooling: true,
})
.resolves({ Id: packageId, IsReleased: true })
.withArgs(
`SELECT Id, IsReleased FROM Package2Version WHERE Package2Id = '${packageId}' AND MajorVersion = 4 AND MinorVersion = 0 AND PatchVersion = 0 and IsReleased = true`,
{
tooling: true,
}
)
.resolves({ Id: packageId, IsReleased: true }),
} as unknown as Connection);
hubOrgStub.getUsername.returns(username);
authInfoStub.getFields.returns({
instanceUrl: 'https://salesforce.com',
orgId: '12345',
});
});
afterEach(() => {
sandbox.restore();
});

it('scratchOrgCreate happy path', async () => {
const scratchOrgCreateOptions = {
hubOrg: hubOrgStub,
} as ScratchOrgCreateOptions;
const scratchOrgCreateResult = await scratchOrgCreate(scratchOrgCreateOptions);
expect(scratchOrgCreateResult).to.deep.equal({
authFields: {
instanceUrl: 'https://salesforce.com',
orgId: '12345',
},
authInfo: {},
scratchOrgInfo: retrieve,
username,
warnings: [],
});
expect(scratchOrgCreateResult).to.have.keys(['username', 'scratchOrgInfo', 'authInfo', 'authFields', 'warnings']);
});
});

0 comments on commit 605642b

Please sign in to comment.