@salesforce/core
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -0,1 +1,8 @@ | ||
# [2.3.0](https://github.com/forcedotcom/sfdx-core/compare/v2.2.0...v2.3.0) (2020-03-20) | ||
### Features | ||
* allow stubbing on single tests ([37cef1b](https://github.com/forcedotcom/sfdx-core/commit/37cef1bbf8f4d1c4909f13c1fac7757f1430e40b)) | ||
# [2.2.0](https://github.com/forcedotcom/sfdx-core/compare/v2.1.6...v2.2.0) (2020-02-11) | ||
@@ -2,0 +9,0 @@ |
@@ -68,5 +68,5 @@ import { AsyncCreatable } from '@salesforce/kit'; | ||
* | ||
* @param devHubUsername The username of the dev hub org. | ||
* @param devHubUsernameOrAlias The username or alias of the dev hub org. | ||
*/ | ||
checkScratchOrg(devHubUsername?: string): Promise<Partial<AuthFields>>; | ||
checkScratchOrg(devHubUsernameOrAlias?: string): Promise<Partial<AuthFields>>; | ||
/** | ||
@@ -73,0 +73,0 @@ * Returns the Org object or null if this org is not affiliated with a Dev Hub (according to the local config). |
@@ -118,12 +118,10 @@ "use strict"; | ||
* | ||
* @param devHubUsername The username of the dev hub org. | ||
* @param devHubUsernameOrAlias The username or alias of the dev hub org. | ||
*/ | ||
async checkScratchOrg(devHubUsername) { | ||
let targetDevHub = devHubUsername; | ||
if (!targetDevHub) { | ||
targetDevHub = ts_types_1.asString(this.configAggregator.getPropertyValue(config_1.Config.DEFAULT_DEV_HUB_USERNAME)); | ||
async checkScratchOrg(devHubUsernameOrAlias) { | ||
let aliasOrUsername = devHubUsernameOrAlias; | ||
if (!aliasOrUsername) { | ||
aliasOrUsername = ts_types_1.asString(this.configAggregator.getPropertyValue(config_1.Config.DEFAULT_DEV_HUB_USERNAME)); | ||
} | ||
const devHubConnection = await connection_1.Connection.create({ | ||
authInfo: await authInfo_1.AuthInfo.create({ username: targetDevHub }) | ||
}); | ||
const devHubConnection = (await Org.create({ aliasOrUsername })).getConnection(); | ||
const thisOrgAuthConfig = this.getConnection().getAuthInfoFields(); | ||
@@ -130,0 +128,0 @@ const trimmedId = sfdc_1.sfdc.trimTo15(thisOrgAuthConfig.orgId); |
@@ -124,2 +124,69 @@ import * as sinonType from 'sinon'; | ||
/** | ||
* Instantiate a @salesforce/core test context. This is automatically created by `const $$ = testSetup()` | ||
* but is useful if you don't want to have a global stub of @salesforce/core and you want to isolate it to | ||
* a single describe. | ||
* | ||
* **Note:** Call `stubContext` in your beforeEach to have clean stubs of @salesforce/core every test run. | ||
* | ||
* @example | ||
* ``` | ||
* const $$ = instantiateContext(); | ||
* | ||
* beforeEach(() => { | ||
* stubContext($$); | ||
* }); | ||
* | ||
* afterEach(() => { | ||
* restoreContext($$); | ||
* }); | ||
* ``` | ||
* @param sinon | ||
*/ | ||
export declare const instantiateContext: (sinon?: any) => TestContext; | ||
/** | ||
* Stub a @salesforce/core test context. This will mock out logging to a file, config file reading and writing, | ||
* local and global path resolution, and http request using connection (soon)*. | ||
* | ||
* This is automatically stubbed in the global beforeEach created by | ||
* `const $$ = testSetup()` but is useful if you don't want to have a global stub of @salesforce/core and you | ||
* want to isolate it to a single describe. | ||
* | ||
* **Note:** Always call `restoreContext` in your afterEach. | ||
* | ||
* @example | ||
* ``` | ||
* const $$ = instantiateContext(); | ||
* | ||
* beforeEach(() => { | ||
* stubContext($$); | ||
* }); | ||
* | ||
* afterEach(() => { | ||
* restoreContext($$); | ||
* }); | ||
* ``` | ||
* @param testContext | ||
*/ | ||
export declare const stubContext: (testContext: TestContext) => void; | ||
/** | ||
* Restore a @salesforce/core test context. This is automatically stubbed in the global beforeEach created by | ||
* `const $$ = testSetup()` but is useful if you don't want to have a global stub of @salesforce/core and you | ||
* want to isolate it to a single describe. | ||
* | ||
* @example | ||
* ``` | ||
* const $$ = instantiateContext(); | ||
* | ||
* beforeEach(() => { | ||
* stubContext($$); | ||
* }); | ||
* | ||
* afterEach(() => { | ||
* restoreContext($$); | ||
* }); | ||
* ``` | ||
* @param testContext | ||
*/ | ||
export declare const restoreContext: (testContext: TestContext) => void; | ||
/** | ||
* Use to mock out different pieces of sfdx-core to make testing easier. This will mock out | ||
@@ -129,2 +196,4 @@ * logging to a file, config file reading and writing, local and global path resolution, and | ||
* | ||
* **Note:** The testSetup should be outside of the describe. If you need to stub per test, use | ||
* `instantiateContext`, `stubContext`, and `restoreContext`. | ||
* ``` | ||
@@ -131,0 +200,0 @@ * // In a mocha tests |
@@ -38,4 +38,25 @@ "use strict"; | ||
} | ||
// tslint:disable-next-line:no-any | ||
const _testSetup = (sinon) => { | ||
/** | ||
* Instantiate a @salesforce/core test context. This is automatically created by `const $$ = testSetup()` | ||
* but is useful if you don't want to have a global stub of @salesforce/core and you want to isolate it to | ||
* a single describe. | ||
* | ||
* **Note:** Call `stubContext` in your beforeEach to have clean stubs of @salesforce/core every test run. | ||
* | ||
* @example | ||
* ``` | ||
* const $$ = instantiateContext(); | ||
* | ||
* beforeEach(() => { | ||
* stubContext($$); | ||
* }); | ||
* | ||
* afterEach(() => { | ||
* restoreContext($$); | ||
* }); | ||
* ``` | ||
* @param sinon | ||
*/ | ||
// tslint:disable-next-line: no-any | ||
exports.instantiateContext = (sinon) => { | ||
if (!sinon) { | ||
@@ -90,53 +111,107 @@ try { | ||
}; | ||
return testContext; | ||
}; | ||
/** | ||
* Stub a @salesforce/core test context. This will mock out logging to a file, config file reading and writing, | ||
* local and global path resolution, and http request using connection (soon)*. | ||
* | ||
* This is automatically stubbed in the global beforeEach created by | ||
* `const $$ = testSetup()` but is useful if you don't want to have a global stub of @salesforce/core and you | ||
* want to isolate it to a single describe. | ||
* | ||
* **Note:** Always call `restoreContext` in your afterEach. | ||
* | ||
* @example | ||
* ``` | ||
* const $$ = instantiateContext(); | ||
* | ||
* beforeEach(() => { | ||
* stubContext($$); | ||
* }); | ||
* | ||
* afterEach(() => { | ||
* restoreContext($$); | ||
* }); | ||
* ``` | ||
* @param testContext | ||
*/ | ||
exports.stubContext = (testContext) => { | ||
// Most core files create a child logger so stub this to return our test logger. | ||
ts_sinon_1.stubMethod(testContext.SANDBOX, logger_1.Logger, 'child').returns(Promise.resolve(testContext.TEST_LOGGER)); | ||
testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile, 'resolveRootFolder').callsFake((isGlobal) => testContext.rootPathRetriever(isGlobal, testContext.id)); | ||
// Mock out all config file IO for all tests. They can restore individually if they need original functionality. | ||
testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile.prototype, 'read').callsFake(async function () { | ||
const stub = testContext.configStubs[this.constructor.name] || {}; | ||
if (stub.readFn) { | ||
return await stub.readFn.call(this); | ||
} | ||
let contents = stub.contents || {}; | ||
if (stub.retrieveContents) { | ||
contents = await stub.retrieveContents.call(this); | ||
} | ||
this.setContentsFromObject(contents); | ||
return Promise.resolve(this.getContents()); | ||
}); | ||
testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile.prototype, 'write').callsFake(async function (newContents) { | ||
if (!testContext.configStubs[this.constructor.name]) { | ||
testContext.configStubs[this.constructor.name] = {}; | ||
} | ||
const stub = testContext.configStubs[this.constructor.name]; | ||
if (!stub) | ||
return; | ||
if (stub.writeFn) { | ||
return await stub.writeFn.call(this, newContents); | ||
} | ||
let contents = newContents || this.getContents(); | ||
if (stub.updateContents) { | ||
contents = await stub.updateContents.call(this); | ||
} | ||
this.setContents(contents); | ||
stub.contents = this.toObject(); | ||
}); | ||
testContext.SANDBOXES.CRYPTO.stub(crypto_2.Crypto.prototype, 'getKeyChain').callsFake(() => Promise.resolve({ | ||
setPassword: () => Promise.resolve(), | ||
getPassword: (data, cb) => cb(undefined, '12345678901234567890123456789012') | ||
})); | ||
testContext.SANDBOXES.CONNECTION.stub(connection_1.Connection.prototype, 'request').callsFake(function (request, options) { | ||
if (request === `${this.instanceUrl}/services/data`) { | ||
return Promise.resolve([{ version: '42.0' }]); | ||
} | ||
return testContext.fakeConnectionRequest.call(this, request, options); | ||
}); | ||
// Always start with the default and tests beforeEach or it methods can override it. | ||
testContext.fakeConnectionRequest = defaultFakeConnectionRequest; | ||
}; | ||
/** | ||
* Restore a @salesforce/core test context. This is automatically stubbed in the global beforeEach created by | ||
* `const $$ = testSetup()` but is useful if you don't want to have a global stub of @salesforce/core and you | ||
* want to isolate it to a single describe. | ||
* | ||
* @example | ||
* ``` | ||
* const $$ = instantiateContext(); | ||
* | ||
* beforeEach(() => { | ||
* stubContext($$); | ||
* }); | ||
* | ||
* afterEach(() => { | ||
* restoreContext($$); | ||
* }); | ||
* ``` | ||
* @param testContext | ||
*/ | ||
exports.restoreContext = (testContext) => { | ||
testContext.SANDBOX.restore(); | ||
Object.values(testContext.SANDBOXES).forEach(theSandbox => theSandbox.restore()); | ||
testContext.configStubs = {}; | ||
}; | ||
// tslint:disable-next-line:no-any | ||
const _testSetup = (sinon) => { | ||
const testContext = exports.instantiateContext(sinon); | ||
beforeEach(() => { | ||
// Most core files create a child logger so stub this to return our test logger. | ||
ts_sinon_1.stubMethod(testContext.SANDBOX, logger_1.Logger, 'child').returns(Promise.resolve(testContext.TEST_LOGGER)); | ||
testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile, 'resolveRootFolder').callsFake((isGlobal) => testContext.rootPathRetriever(isGlobal, testContext.id)); | ||
// Mock out all config file IO for all tests. They can restore individually if they need original functionality. | ||
testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile.prototype, 'read').callsFake(async function () { | ||
const stub = testContext.configStubs[this.constructor.name] || {}; | ||
if (stub.readFn) { | ||
return await stub.readFn.call(this); | ||
} | ||
let contents = stub.contents || {}; | ||
if (stub.retrieveContents) { | ||
contents = await stub.retrieveContents.call(this); | ||
} | ||
this.setContentsFromObject(contents); | ||
return Promise.resolve(this.getContents()); | ||
}); | ||
testContext.SANDBOXES.CONFIG.stub(configFile_1.ConfigFile.prototype, 'write').callsFake(async function (newContents) { | ||
if (!testContext.configStubs[this.constructor.name]) { | ||
testContext.configStubs[this.constructor.name] = {}; | ||
} | ||
const stub = testContext.configStubs[this.constructor.name]; | ||
if (!stub) | ||
return; | ||
if (stub.writeFn) { | ||
return await stub.writeFn.call(this, newContents); | ||
} | ||
let contents = newContents || this.getContents(); | ||
if (stub.updateContents) { | ||
contents = await stub.updateContents.call(this); | ||
} | ||
this.setContents(contents); | ||
stub.contents = this.toObject(); | ||
}); | ||
testContext.SANDBOXES.CRYPTO.stub(crypto_2.Crypto.prototype, 'getKeyChain').callsFake(() => Promise.resolve({ | ||
setPassword: () => Promise.resolve(), | ||
getPassword: (data, cb) => cb(undefined, '12345678901234567890123456789012') | ||
})); | ||
testContext.SANDBOXES.CONNECTION.stub(connection_1.Connection.prototype, 'request').callsFake(function (request, options) { | ||
if (request === `${this.instanceUrl}/services/data`) { | ||
return Promise.resolve([{ version: '42.0' }]); | ||
} | ||
return testContext.fakeConnectionRequest.call(this, request, options); | ||
}); | ||
// Always start with the default and tests beforeEach or it methods can override it. | ||
testContext.fakeConnectionRequest = defaultFakeConnectionRequest; | ||
exports.stubContext(testContext); | ||
}); | ||
afterEach(() => { | ||
testContext.SANDBOX.restore(); | ||
Object.values(testContext.SANDBOXES).forEach(theSandbox => theSandbox.restore()); | ||
testContext.configStubs = {}; | ||
exports.restoreContext(testContext); | ||
}); | ||
@@ -150,2 +225,4 @@ return testContext; | ||
* | ||
* **Note:** The testSetup should be outside of the describe. If you need to stub per test, use | ||
* `instantiateContext`, `stubContext`, and `restoreContext`. | ||
* ``` | ||
@@ -152,0 +229,0 @@ * // In a mocha tests |
{ | ||
"name": "@salesforce/core", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Core libraries to interact with SFDX projects, orgs, and APIs.", | ||
@@ -5,0 +5,0 @@ "main": "lib/exported", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
451730
11309