@salesforce/core 3.31.4 → 3.31.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +11 -11
- package/README.md +222 -222
- package/lib/config/aliasesConfig.d.ts +12 -12
- package/lib/config/aliasesConfig.js +27 -27
- package/lib/config/authInfoConfig.d.ts +19 -19
- package/lib/config/authInfoConfig.js +34 -34
- package/lib/config/config.d.ts +311 -311
- package/lib/config/config.js +574 -574
- package/lib/config/configAggregator.d.ts +232 -232
- package/lib/config/configAggregator.js +379 -379
- package/lib/config/configFile.d.ts +199 -199
- package/lib/config/configFile.js +340 -340
- package/lib/config/configGroup.d.ts +141 -141
- package/lib/config/configGroup.js +224 -224
- package/lib/config/configStore.d.ts +241 -241
- package/lib/config/configStore.js +352 -352
- package/lib/config/envVars.d.ts +101 -101
- package/lib/config/envVars.js +456 -456
- package/lib/config/orgUsersConfig.d.ts +31 -31
- package/lib/config/orgUsersConfig.js +41 -41
- package/lib/config/sandboxOrgConfig.d.ts +37 -37
- package/lib/config/sandboxOrgConfig.js +50 -50
- package/lib/config/sandboxProcessCache.d.ts +16 -16
- package/lib/config/sandboxProcessCache.js +37 -37
- package/lib/config/tokensConfig.d.ts +10 -10
- package/lib/config/tokensConfig.js +28 -28
- package/lib/config/ttlConfig.d.ts +34 -34
- package/lib/config/ttlConfig.js +54 -54
- package/lib/crypto/crypto.d.ts +54 -54
- package/lib/crypto/crypto.js +220 -220
- package/lib/crypto/keyChain.d.ts +8 -8
- package/lib/crypto/keyChain.js +61 -61
- package/lib/crypto/keyChainImpl.d.ts +116 -116
- package/lib/crypto/keyChainImpl.js +486 -486
- package/lib/crypto/secureBuffer.d.ts +46 -46
- package/lib/crypto/secureBuffer.js +82 -82
- package/lib/deviceOauthService.d.ts +71 -71
- package/lib/deviceOauthService.js +191 -191
- package/lib/exported.d.ts +38 -38
- package/lib/exported.js +118 -118
- package/lib/global.d.ts +70 -70
- package/lib/global.js +109 -109
- package/lib/lifecycleEvents.d.ts +93 -93
- package/lib/lifecycleEvents.js +188 -188
- package/lib/logger.d.ts +381 -381
- package/lib/logger.js +734 -734
- package/lib/messages.d.ts +291 -291
- package/lib/messages.js +543 -543
- package/lib/org/authInfo.d.ts +344 -344
- package/lib/org/authInfo.js +892 -892
- package/lib/org/authRemover.d.ts +88 -88
- package/lib/org/authRemover.js +182 -182
- package/lib/org/connection.d.ts +197 -197
- package/lib/org/connection.js +395 -395
- package/lib/org/index.d.ts +6 -6
- package/lib/org/index.js +28 -28
- package/lib/org/org.d.ts +558 -558
- package/lib/org/org.js +1267 -1267
- package/lib/org/orgConfigProperties.d.ts +69 -69
- package/lib/org/orgConfigProperties.js +136 -136
- package/lib/org/permissionSetAssignment.d.ts +35 -35
- package/lib/org/permissionSetAssignment.js +125 -125
- package/lib/org/scratchOrgCache.d.ts +20 -20
- package/lib/org/scratchOrgCache.js +32 -32
- package/lib/org/scratchOrgCreate.d.ts +54 -54
- package/lib/org/scratchOrgCreate.js +216 -216
- package/lib/org/scratchOrgErrorCodes.d.ts +10 -10
- package/lib/org/scratchOrgErrorCodes.js +88 -88
- package/lib/org/scratchOrgFeatureDeprecation.d.ts +26 -26
- package/lib/org/scratchOrgFeatureDeprecation.js +109 -109
- package/lib/org/scratchOrgInfoApi.d.ts +68 -68
- package/lib/org/scratchOrgInfoApi.js +413 -413
- package/lib/org/scratchOrgInfoGenerator.d.ts +64 -64
- package/lib/org/scratchOrgInfoGenerator.js +241 -241
- package/lib/org/scratchOrgLifecycleEvents.d.ts +10 -10
- package/lib/org/scratchOrgLifecycleEvents.js +40 -40
- package/lib/org/scratchOrgSettingsGenerator.d.ts +78 -78
- package/lib/org/scratchOrgSettingsGenerator.js +276 -276
- package/lib/org/scratchOrgTypes.d.ts +43 -43
- package/lib/org/scratchOrgTypes.js +8 -8
- package/lib/org/user.d.ts +187 -187
- package/lib/org/user.js +448 -448
- package/lib/schema/printer.d.ts +79 -79
- package/lib/schema/printer.js +260 -260
- package/lib/schema/validator.d.ts +70 -70
- package/lib/schema/validator.js +169 -169
- package/lib/sfError.d.ts +73 -73
- package/lib/sfError.js +136 -136
- package/lib/sfProject.d.ts +357 -357
- package/lib/sfProject.js +671 -671
- package/lib/stateAggregator/accessors/aliasAccessor.d.ts +98 -98
- package/lib/stateAggregator/accessors/aliasAccessor.js +145 -145
- package/lib/stateAggregator/accessors/orgAccessor.d.ts +101 -101
- package/lib/stateAggregator/accessors/orgAccessor.js +240 -240
- package/lib/stateAggregator/accessors/sandboxAccessor.d.ts +8 -8
- package/lib/stateAggregator/accessors/sandboxAccessor.js +27 -27
- package/lib/stateAggregator/accessors/tokenAccessor.d.ts +63 -63
- package/lib/stateAggregator/accessors/tokenAccessor.js +79 -79
- package/lib/stateAggregator/index.d.ts +4 -4
- package/lib/stateAggregator/index.js +26 -26
- package/lib/stateAggregator/stateAggregator.d.ts +25 -25
- package/lib/stateAggregator/stateAggregator.js +45 -45
- package/lib/status/myDomainResolver.d.ts +66 -66
- package/lib/status/myDomainResolver.js +124 -124
- package/lib/status/pollingClient.d.ts +85 -85
- package/lib/status/pollingClient.js +115 -115
- package/lib/status/streamingClient.d.ts +244 -244
- package/lib/status/streamingClient.js +436 -436
- package/lib/status/types.d.ts +89 -89
- package/lib/status/types.js +17 -17
- package/lib/testSetup.d.ts +553 -553
- package/lib/testSetup.js +871 -871
- package/lib/util/cache.d.ts +11 -11
- package/lib/util/cache.js +69 -69
- package/lib/util/checkLightningDomain.d.ts +1 -1
- package/lib/util/checkLightningDomain.js +28 -28
- package/lib/util/directoryWriter.d.ts +12 -12
- package/lib/util/directoryWriter.js +53 -53
- package/lib/util/getJwtAudienceUrl.d.ts +4 -4
- package/lib/util/getJwtAudienceUrl.js +18 -18
- package/lib/util/internal.d.ts +58 -58
- package/lib/util/internal.js +118 -118
- package/lib/util/jsonXmlTools.d.ts +14 -14
- package/lib/util/jsonXmlTools.js +38 -38
- package/lib/util/mapKeys.d.ts +14 -14
- package/lib/util/mapKeys.js +51 -51
- package/lib/util/sfdc.d.ts +52 -52
- package/lib/util/sfdc.js +85 -85
- package/lib/util/sfdcUrl.d.ts +72 -72
- package/lib/util/sfdcUrl.js +215 -215
- package/lib/util/structuredWriter.d.ts +9 -9
- package/lib/util/structuredWriter.js +2 -2
- package/lib/util/zipWriter.d.ts +16 -16
- package/lib/util/zipWriter.js +67 -67
- package/lib/webOAuthServer.d.ts +156 -156
- package/lib/webOAuthServer.js +388 -388
- package/messages/auth.md +37 -37
- package/messages/config.md +156 -156
- package/messages/connection.md +30 -30
- package/messages/core.json +20 -20
- package/messages/core.md +67 -67
- package/messages/encryption.md +85 -85
- package/messages/envVars.md +303 -303
- package/messages/org.md +63 -63
- package/messages/permissionSetAssignment.md +31 -31
- package/messages/scratchOrgCreate.md +23 -23
- package/messages/scratchOrgErrorCodes.md +115 -115
- package/messages/scratchOrgFeatureDeprecation.md +11 -11
- package/messages/scratchOrgInfoApi.md +15 -15
- package/messages/scratchOrgInfoGenerator.md +23 -23
- package/messages/streaming.md +23 -23
- package/messages/user.md +35 -35
- package/package.json +97 -97
package/LICENSE.txt
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
Copyright (c) 2022, Salesforce.com, Inc.
|
|
2
|
-
All rights reserved.
|
|
3
|
-
|
|
4
|
-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
5
|
-
|
|
6
|
-
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
7
|
-
|
|
8
|
-
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
9
|
-
|
|
10
|
-
* Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
11
|
-
|
|
1
|
+
Copyright (c) 2022, Salesforce.com, Inc.
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
5
|
+
|
|
6
|
+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
7
|
+
|
|
8
|
+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
9
|
+
|
|
10
|
+
* Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
11
|
+
|
|
12
12
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,222 +1,222 @@
|
|
|
1
|
-
[](https://www.npmjs.com/package/@salesforce/core)
|
|
2
|
-
[](https://circleci.com/gh/forcedotcom/sfdx-core)
|
|
3
|
-
|
|
4
|
-
- [Description](#description)
|
|
5
|
-
- [Usage](#usage)
|
|
6
|
-
- [Contributing](#contributing)
|
|
7
|
-
- [Using TestSetup](#using-testsetup)
|
|
8
|
-
- [Mocking Authorizations](#mocking-authorizations)
|
|
9
|
-
- [Mocking Config Files](#mocking-config-files)
|
|
10
|
-
- [Using the Built-in Sinon Sandboxes](#using-the-built-in-sinon-sandboxes)
|
|
11
|
-
- [Testing Expected Failures](#testing-expected-failures)
|
|
12
|
-
- [Testing Log Lines](#testing-log-lines)
|
|
13
|
-
|
|
14
|
-
# Description
|
|
15
|
-
|
|
16
|
-
The @salesforce/core library provides client-side management of Salesforce DX projects, org authentication, connections to Salesforce APIs, and other utilities. Much of the core functionality that powers the Salesforce CLI plugins comes from this library. You can use this functionality in your plugins too.
|
|
17
|
-
|
|
18
|
-
# Usage
|
|
19
|
-
|
|
20
|
-
See the [API documentation](https://forcedotcom.github.io/sfdx-core/).
|
|
21
|
-
|
|
22
|
-
## Contributing
|
|
23
|
-
|
|
24
|
-
If you are interested in contributing, please take a look at the [CONTRIBUTING](CONTRIBUTING.md) guide.
|
|
25
|
-
|
|
26
|
-
# Using TestSetup
|
|
27
|
-
|
|
28
|
-
The Salesforce DX Core Library provides a unit testing utility to help with mocking and sand-boxing core components. This feature allows unit tests to execute without needing to make API calls to salesforce.com.
|
|
29
|
-
|
|
30
|
-
## Mocking Authorizations
|
|
31
|
-
|
|
32
|
-
Here you can mock authorization for a Salesforce scratch org.
|
|
33
|
-
|
|
34
|
-
```typescript
|
|
35
|
-
import { strictEqual } from 'assert';
|
|
36
|
-
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
|
|
37
|
-
import { AuthInfo } from '@salesforce/core';
|
|
38
|
-
|
|
39
|
-
describe('Mocking Auth data', () => {
|
|
40
|
-
const $$ = new TestContext();
|
|
41
|
-
it('example', async () => {
|
|
42
|
-
const testData = new MockTestOrgData();
|
|
43
|
-
await $$.stubAuths(testData);
|
|
44
|
-
const auth = await AuthInfo.create({ username: testData.username });
|
|
45
|
-
strictEqual(auth.getUsername(), testData.username);
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
After having a valid AuthInfo object you can then create fake connections to a Salesforce.com scratch org. This allows for writing tests that can validate result responses for SOQL queries and REST endpoints.
|
|
51
|
-
|
|
52
|
-
```typescript
|
|
53
|
-
import { AuthInfo, Connection, SfError } from '@salesforce/core';
|
|
54
|
-
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
|
|
55
|
-
import { AnyJson, ensureJsonMap, JsonMap } from '@salesforce/ts-types';
|
|
56
|
-
import { ensureString } from '@salesforce/ts-types';
|
|
57
|
-
import { deepStrictEqual } from 'assert';
|
|
58
|
-
import { QueryResult } from 'jsforce';
|
|
59
|
-
|
|
60
|
-
describe('Mocking a force server call', () => {
|
|
61
|
-
const $$ = new TestContext();
|
|
62
|
-
it('example', async () => {
|
|
63
|
-
const records: AnyJson = { records: ['123456', '234567'] };
|
|
64
|
-
const testData = new MockTestOrgData();
|
|
65
|
-
await $$.stubAuths(testData);
|
|
66
|
-
$$.fakeConnectionRequest = (request: AnyJson): Promise<AnyJson> => {
|
|
67
|
-
const _request = ensureJsonMap(request);
|
|
68
|
-
if (request && ensureString(_request.url).includes('Account')) {
|
|
69
|
-
return Promise.resolve(records);
|
|
70
|
-
} else {
|
|
71
|
-
return Promise.reject(new SfError(`Unexpected request: ${_request.url}`));
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
const connection = await Connection.create({
|
|
75
|
-
authInfo: await AuthInfo.create({ username: testData.username }),
|
|
76
|
-
});
|
|
77
|
-
const result = await connection.query('select Id From Account');
|
|
78
|
-
deepStrictEqual(result, records);
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
## Mocking Config Files
|
|
84
|
-
|
|
85
|
-
You can mock the contents of various config files
|
|
86
|
-
|
|
87
|
-
```typescript
|
|
88
|
-
import { strictEqual } from 'assert';
|
|
89
|
-
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
|
|
90
|
-
import { StateAggregator, OrgConfigProperties } from '@salesforce/core';
|
|
91
|
-
|
|
92
|
-
describe('Mocking Aliases', () => {
|
|
93
|
-
const $$ = new TestContext();
|
|
94
|
-
it('example', async () => {
|
|
95
|
-
const testData = new MockTestOrgData();
|
|
96
|
-
await $$.stubAliases({ myAlias: testData.username });
|
|
97
|
-
const alias = (await StateAggregator.getInstance()).aliases.get(testData.username);
|
|
98
|
-
strictEqual(alias, 'myAlais');
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
describe('Mocking Config', () => {
|
|
103
|
-
it('example', async () => {
|
|
104
|
-
const testData = new MockTestOrgData();
|
|
105
|
-
await $$.stubConfig({ [OrgConfigProperties.TARGET_ORG]: testData.username });
|
|
106
|
-
const { value } = (await ConfigAggregator.create()).getInfo(OrgConfigProperties.TARGET_ORG);
|
|
107
|
-
strictEqual(value, testData.username);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('Mocking Arbitrary Config Files', () => {
|
|
112
|
-
it('example', async () => {
|
|
113
|
-
// MyConfigFile must extend the ConfigFile class in order for this to work properly.
|
|
114
|
-
$$.setConfigStubContents('MyConfigFile', { contents: { foo: 'bar' } });
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## Using the Built-in Sinon Sandboxes
|
|
120
|
-
|
|
121
|
-
sfdx-core uses Sinon as its underlying mocking system. If you're unfamiliar with Sinon and its sandboxing concept you can find more information here:
|
|
122
|
-
https://sinonjs.org/
|
|
123
|
-
Sinon `stub`s and `spy`s must be cleaned up after test invocations. To ease the use of Sinon with sfdx core we've exposed our sandbox in TestSetup. After adding your own `stub`s and/or `spy`s they will automatically be cleaned up after each test using mocha's afterEach method.
|
|
124
|
-
|
|
125
|
-
```typescript
|
|
126
|
-
import { strictEqual } from 'assert';
|
|
127
|
-
|
|
128
|
-
import { TestContext } from '@salesforce/core/lib/testSetup';
|
|
129
|
-
import * as os from 'os';
|
|
130
|
-
|
|
131
|
-
describe('Using the built in Sinon sandbox.', () => {
|
|
132
|
-
const $$ = new TestContext();
|
|
133
|
-
it('example', async () => {
|
|
134
|
-
const unsupportedOS = 'LEO';
|
|
135
|
-
$$.SANDBOX.stub(os, 'platform').returns(unsupportedOS);
|
|
136
|
-
strictEqual(os.platform(), unsupportedOS);
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
## Testing Expected Failures
|
|
142
|
-
|
|
143
|
-
It's important to have negative tests that ensure proper error handling. With `shouldThrow` it's easy to test for expected async rejections.
|
|
144
|
-
|
|
145
|
-
```typescript
|
|
146
|
-
import { SfError } from '@salesforce/core';
|
|
147
|
-
import { shouldThrow } from '@salesforce/core/lib/testSetup';
|
|
148
|
-
import { strictEqual } from 'assert';
|
|
149
|
-
|
|
150
|
-
class TestObject {
|
|
151
|
-
public static async method() {
|
|
152
|
-
throw new SfError('Error', 'ExpectedError');
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
describe('Testing for expected errors', () => {
|
|
157
|
-
it('example', async () => {
|
|
158
|
-
try {
|
|
159
|
-
await shouldThrow(TestObject.method());
|
|
160
|
-
} catch (e) {
|
|
161
|
-
strictEqual(e.name, 'ExpectedError');
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
You can also use `shouldThrowSync` for syncrhonous functions you expect to fail
|
|
168
|
-
|
|
169
|
-
```typescript
|
|
170
|
-
import { SfError } from '@salesforce/core';
|
|
171
|
-
import { shouldThrowSync } from '@salesforce/core/lib/testSetup';
|
|
172
|
-
import { strictEqual } from 'assert';
|
|
173
|
-
|
|
174
|
-
class TestObject {
|
|
175
|
-
public static method() {
|
|
176
|
-
throw new SfError('Error', 'ExpectedError');
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
describe('Testing for expected errors', () => {
|
|
181
|
-
it('example', async () => {
|
|
182
|
-
try {
|
|
183
|
-
shouldThrowSync(() => TestObject.method());
|
|
184
|
-
} catch (e) {
|
|
185
|
-
strictEqual(e.name, 'ExpectedError');
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
## Testing Log Lines
|
|
192
|
-
|
|
193
|
-
It's also useful to check expected values and content from log lines. TestSetup configures the sfdx-core logger to use an in memory LogLine storage structure. These can be easily accessed from tests.
|
|
194
|
-
|
|
195
|
-
```typescript
|
|
196
|
-
import { Logger, LogLine } from '@salesforce/core';
|
|
197
|
-
import { TestContext } from '@salesforce/core/lib/testSetup';
|
|
198
|
-
import { strictEqual } from 'assert';
|
|
199
|
-
|
|
200
|
-
const TEST_STRING = 'foo was here';
|
|
201
|
-
|
|
202
|
-
class TestObject {
|
|
203
|
-
constructor(private logger: Logger) {
|
|
204
|
-
this.logger = logger.child('TestObject');
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
public method() {
|
|
208
|
-
this.logger.error(TEST_STRING);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
describe('Testing log lines', () => {
|
|
213
|
-
const $$ = new TestContext();
|
|
214
|
-
it('example', async () => {
|
|
215
|
-
const obj = new TestObject($$.TEST_LOGGER);
|
|
216
|
-
obj.method();
|
|
217
|
-
const records = $$.TEST_LOGGER.getBufferedRecords();
|
|
218
|
-
strictEqual(records.length, 1);
|
|
219
|
-
strictEqual(records[0].msg, TEST_STRING);
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
```
|
|
1
|
+
[](https://www.npmjs.com/package/@salesforce/core)
|
|
2
|
+
[](https://circleci.com/gh/forcedotcom/sfdx-core)
|
|
3
|
+
|
|
4
|
+
- [Description](#description)
|
|
5
|
+
- [Usage](#usage)
|
|
6
|
+
- [Contributing](#contributing)
|
|
7
|
+
- [Using TestSetup](#using-testsetup)
|
|
8
|
+
- [Mocking Authorizations](#mocking-authorizations)
|
|
9
|
+
- [Mocking Config Files](#mocking-config-files)
|
|
10
|
+
- [Using the Built-in Sinon Sandboxes](#using-the-built-in-sinon-sandboxes)
|
|
11
|
+
- [Testing Expected Failures](#testing-expected-failures)
|
|
12
|
+
- [Testing Log Lines](#testing-log-lines)
|
|
13
|
+
|
|
14
|
+
# Description
|
|
15
|
+
|
|
16
|
+
The @salesforce/core library provides client-side management of Salesforce DX projects, org authentication, connections to Salesforce APIs, and other utilities. Much of the core functionality that powers the Salesforce CLI plugins comes from this library. You can use this functionality in your plugins too.
|
|
17
|
+
|
|
18
|
+
# Usage
|
|
19
|
+
|
|
20
|
+
See the [API documentation](https://forcedotcom.github.io/sfdx-core/).
|
|
21
|
+
|
|
22
|
+
## Contributing
|
|
23
|
+
|
|
24
|
+
If you are interested in contributing, please take a look at the [CONTRIBUTING](CONTRIBUTING.md) guide.
|
|
25
|
+
|
|
26
|
+
# Using TestSetup
|
|
27
|
+
|
|
28
|
+
The Salesforce DX Core Library provides a unit testing utility to help with mocking and sand-boxing core components. This feature allows unit tests to execute without needing to make API calls to salesforce.com.
|
|
29
|
+
|
|
30
|
+
## Mocking Authorizations
|
|
31
|
+
|
|
32
|
+
Here you can mock authorization for a Salesforce scratch org.
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { strictEqual } from 'assert';
|
|
36
|
+
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
|
|
37
|
+
import { AuthInfo } from '@salesforce/core';
|
|
38
|
+
|
|
39
|
+
describe('Mocking Auth data', () => {
|
|
40
|
+
const $$ = new TestContext();
|
|
41
|
+
it('example', async () => {
|
|
42
|
+
const testData = new MockTestOrgData();
|
|
43
|
+
await $$.stubAuths(testData);
|
|
44
|
+
const auth = await AuthInfo.create({ username: testData.username });
|
|
45
|
+
strictEqual(auth.getUsername(), testData.username);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
After having a valid AuthInfo object you can then create fake connections to a Salesforce.com scratch org. This allows for writing tests that can validate result responses for SOQL queries and REST endpoints.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { AuthInfo, Connection, SfError } from '@salesforce/core';
|
|
54
|
+
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
|
|
55
|
+
import { AnyJson, ensureJsonMap, JsonMap } from '@salesforce/ts-types';
|
|
56
|
+
import { ensureString } from '@salesforce/ts-types';
|
|
57
|
+
import { deepStrictEqual } from 'assert';
|
|
58
|
+
import { QueryResult } from 'jsforce';
|
|
59
|
+
|
|
60
|
+
describe('Mocking a force server call', () => {
|
|
61
|
+
const $$ = new TestContext();
|
|
62
|
+
it('example', async () => {
|
|
63
|
+
const records: AnyJson = { records: ['123456', '234567'] };
|
|
64
|
+
const testData = new MockTestOrgData();
|
|
65
|
+
await $$.stubAuths(testData);
|
|
66
|
+
$$.fakeConnectionRequest = (request: AnyJson): Promise<AnyJson> => {
|
|
67
|
+
const _request = ensureJsonMap(request);
|
|
68
|
+
if (request && ensureString(_request.url).includes('Account')) {
|
|
69
|
+
return Promise.resolve(records);
|
|
70
|
+
} else {
|
|
71
|
+
return Promise.reject(new SfError(`Unexpected request: ${_request.url}`));
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const connection = await Connection.create({
|
|
75
|
+
authInfo: await AuthInfo.create({ username: testData.username }),
|
|
76
|
+
});
|
|
77
|
+
const result = await connection.query('select Id From Account');
|
|
78
|
+
deepStrictEqual(result, records);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Mocking Config Files
|
|
84
|
+
|
|
85
|
+
You can mock the contents of various config files
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { strictEqual } from 'assert';
|
|
89
|
+
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup';
|
|
90
|
+
import { StateAggregator, OrgConfigProperties } from '@salesforce/core';
|
|
91
|
+
|
|
92
|
+
describe('Mocking Aliases', () => {
|
|
93
|
+
const $$ = new TestContext();
|
|
94
|
+
it('example', async () => {
|
|
95
|
+
const testData = new MockTestOrgData();
|
|
96
|
+
await $$.stubAliases({ myAlias: testData.username });
|
|
97
|
+
const alias = (await StateAggregator.getInstance()).aliases.get(testData.username);
|
|
98
|
+
strictEqual(alias, 'myAlais');
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('Mocking Config', () => {
|
|
103
|
+
it('example', async () => {
|
|
104
|
+
const testData = new MockTestOrgData();
|
|
105
|
+
await $$.stubConfig({ [OrgConfigProperties.TARGET_ORG]: testData.username });
|
|
106
|
+
const { value } = (await ConfigAggregator.create()).getInfo(OrgConfigProperties.TARGET_ORG);
|
|
107
|
+
strictEqual(value, testData.username);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('Mocking Arbitrary Config Files', () => {
|
|
112
|
+
it('example', async () => {
|
|
113
|
+
// MyConfigFile must extend the ConfigFile class in order for this to work properly.
|
|
114
|
+
$$.setConfigStubContents('MyConfigFile', { contents: { foo: 'bar' } });
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Using the Built-in Sinon Sandboxes
|
|
120
|
+
|
|
121
|
+
sfdx-core uses Sinon as its underlying mocking system. If you're unfamiliar with Sinon and its sandboxing concept you can find more information here:
|
|
122
|
+
https://sinonjs.org/
|
|
123
|
+
Sinon `stub`s and `spy`s must be cleaned up after test invocations. To ease the use of Sinon with sfdx core we've exposed our sandbox in TestSetup. After adding your own `stub`s and/or `spy`s they will automatically be cleaned up after each test using mocha's afterEach method.
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { strictEqual } from 'assert';
|
|
127
|
+
|
|
128
|
+
import { TestContext } from '@salesforce/core/lib/testSetup';
|
|
129
|
+
import * as os from 'os';
|
|
130
|
+
|
|
131
|
+
describe('Using the built in Sinon sandbox.', () => {
|
|
132
|
+
const $$ = new TestContext();
|
|
133
|
+
it('example', async () => {
|
|
134
|
+
const unsupportedOS = 'LEO';
|
|
135
|
+
$$.SANDBOX.stub(os, 'platform').returns(unsupportedOS);
|
|
136
|
+
strictEqual(os.platform(), unsupportedOS);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Testing Expected Failures
|
|
142
|
+
|
|
143
|
+
It's important to have negative tests that ensure proper error handling. With `shouldThrow` it's easy to test for expected async rejections.
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
import { SfError } from '@salesforce/core';
|
|
147
|
+
import { shouldThrow } from '@salesforce/core/lib/testSetup';
|
|
148
|
+
import { strictEqual } from 'assert';
|
|
149
|
+
|
|
150
|
+
class TestObject {
|
|
151
|
+
public static async method() {
|
|
152
|
+
throw new SfError('Error', 'ExpectedError');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
describe('Testing for expected errors', () => {
|
|
157
|
+
it('example', async () => {
|
|
158
|
+
try {
|
|
159
|
+
await shouldThrow(TestObject.method());
|
|
160
|
+
} catch (e) {
|
|
161
|
+
strictEqual(e.name, 'ExpectedError');
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
You can also use `shouldThrowSync` for syncrhonous functions you expect to fail
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { SfError } from '@salesforce/core';
|
|
171
|
+
import { shouldThrowSync } from '@salesforce/core/lib/testSetup';
|
|
172
|
+
import { strictEqual } from 'assert';
|
|
173
|
+
|
|
174
|
+
class TestObject {
|
|
175
|
+
public static method() {
|
|
176
|
+
throw new SfError('Error', 'ExpectedError');
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
describe('Testing for expected errors', () => {
|
|
181
|
+
it('example', async () => {
|
|
182
|
+
try {
|
|
183
|
+
shouldThrowSync(() => TestObject.method());
|
|
184
|
+
} catch (e) {
|
|
185
|
+
strictEqual(e.name, 'ExpectedError');
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Testing Log Lines
|
|
192
|
+
|
|
193
|
+
It's also useful to check expected values and content from log lines. TestSetup configures the sfdx-core logger to use an in memory LogLine storage structure. These can be easily accessed from tests.
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import { Logger, LogLine } from '@salesforce/core';
|
|
197
|
+
import { TestContext } from '@salesforce/core/lib/testSetup';
|
|
198
|
+
import { strictEqual } from 'assert';
|
|
199
|
+
|
|
200
|
+
const TEST_STRING = 'foo was here';
|
|
201
|
+
|
|
202
|
+
class TestObject {
|
|
203
|
+
constructor(private logger: Logger) {
|
|
204
|
+
this.logger = logger.child('TestObject');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
public method() {
|
|
208
|
+
this.logger.error(TEST_STRING);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
describe('Testing log lines', () => {
|
|
213
|
+
const $$ = new TestContext();
|
|
214
|
+
it('example', async () => {
|
|
215
|
+
const obj = new TestObject($$.TEST_LOGGER);
|
|
216
|
+
obj.method();
|
|
217
|
+
const records = $$.TEST_LOGGER.getBufferedRecords();
|
|
218
|
+
strictEqual(records.length, 1);
|
|
219
|
+
strictEqual(records[0].msg, TEST_STRING);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
```
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { ConfigGroup } from './configGroup';
|
|
2
|
-
import { ConfigContents, ConfigValue } from './configStore';
|
|
3
|
-
/**
|
|
4
|
-
* Different groups of aliases. Currently only support orgs.
|
|
5
|
-
*/
|
|
6
|
-
export declare enum AliasGroup {
|
|
7
|
-
ORGS = "orgs"
|
|
8
|
-
}
|
|
9
|
-
export declare class AliasesConfig extends ConfigGroup<ConfigGroup.Options> {
|
|
10
|
-
static getDefaultOptions(): ConfigGroup.Options;
|
|
11
|
-
protected setMethod(contents: ConfigContents, key: string, value?: ConfigValue): void;
|
|
12
|
-
}
|
|
1
|
+
import { ConfigGroup } from './configGroup';
|
|
2
|
+
import { ConfigContents, ConfigValue } from './configStore';
|
|
3
|
+
/**
|
|
4
|
+
* Different groups of aliases. Currently only support orgs.
|
|
5
|
+
*/
|
|
6
|
+
export declare enum AliasGroup {
|
|
7
|
+
ORGS = "orgs"
|
|
8
|
+
}
|
|
9
|
+
export declare class AliasesConfig extends ConfigGroup<ConfigGroup.Options> {
|
|
10
|
+
static getDefaultOptions(): ConfigGroup.Options;
|
|
11
|
+
protected setMethod(contents: ConfigContents, key: string, value?: ConfigValue): void;
|
|
12
|
+
}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2022, salesforce.com, inc.
|
|
4
|
-
* All rights reserved.
|
|
5
|
-
* Licensed under the BSD 3-Clause license.
|
|
6
|
-
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.AliasesConfig = exports.AliasGroup = void 0;
|
|
10
|
-
const configGroup_1 = require("./configGroup");
|
|
11
|
-
/**
|
|
12
|
-
* Different groups of aliases. Currently only support orgs.
|
|
13
|
-
*/
|
|
14
|
-
var AliasGroup;
|
|
15
|
-
(function (AliasGroup) {
|
|
16
|
-
AliasGroup["ORGS"] = "orgs";
|
|
17
|
-
})(AliasGroup = exports.AliasGroup || (exports.AliasGroup = {}));
|
|
18
|
-
class AliasesConfig extends configGroup_1.ConfigGroup {
|
|
19
|
-
static getDefaultOptions() {
|
|
20
|
-
return { ...configGroup_1.ConfigGroup.getOptions(AliasGroup.ORGS, 'alias.json'), isGlobal: true, isState: true };
|
|
21
|
-
}
|
|
22
|
-
// eslint-disable-next-line class-methods-use-this
|
|
23
|
-
setMethod(contents, key, value) {
|
|
24
|
-
contents[key] = value;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
exports.AliasesConfig = AliasesConfig;
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2022, salesforce.com, inc.
|
|
4
|
+
* All rights reserved.
|
|
5
|
+
* Licensed under the BSD 3-Clause license.
|
|
6
|
+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.AliasesConfig = exports.AliasGroup = void 0;
|
|
10
|
+
const configGroup_1 = require("./configGroup");
|
|
11
|
+
/**
|
|
12
|
+
* Different groups of aliases. Currently only support orgs.
|
|
13
|
+
*/
|
|
14
|
+
var AliasGroup;
|
|
15
|
+
(function (AliasGroup) {
|
|
16
|
+
AliasGroup["ORGS"] = "orgs";
|
|
17
|
+
})(AliasGroup = exports.AliasGroup || (exports.AliasGroup = {}));
|
|
18
|
+
class AliasesConfig extends configGroup_1.ConfigGroup {
|
|
19
|
+
static getDefaultOptions() {
|
|
20
|
+
return { ...configGroup_1.ConfigGroup.getOptions(AliasGroup.ORGS, 'alias.json'), isGlobal: true, isState: true };
|
|
21
|
+
}
|
|
22
|
+
// eslint-disable-next-line class-methods-use-this
|
|
23
|
+
setMethod(contents, key, value) {
|
|
24
|
+
contents[key] = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.AliasesConfig = AliasesConfig;
|
|
28
28
|
//# sourceMappingURL=aliasesConfig.js.map
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { AuthFields } from '../org';
|
|
2
|
-
import { ConfigFile } from './configFile';
|
|
3
|
-
/**
|
|
4
|
-
* An auth config file that stores information such as access tokens, usernames, etc.,
|
|
5
|
-
* in the global sfdx directory (~/.sfdx).
|
|
6
|
-
*
|
|
7
|
-
* ```
|
|
8
|
-
* const authInfo = await AuthInfoConfig.create(AuthInfoConfig.getOptions(username));
|
|
9
|
-
* ```
|
|
10
|
-
*/
|
|
11
|
-
export declare class AuthInfoConfig extends ConfigFile<ConfigFile.Options, AuthFields> {
|
|
12
|
-
protected static encryptedKeys: RegExp[];
|
|
13
|
-
/**
|
|
14
|
-
* Gets the config options for a given org ID.
|
|
15
|
-
*
|
|
16
|
-
* @param username The username for the org.
|
|
17
|
-
*/
|
|
18
|
-
static getOptions(username: string): ConfigFile.Options;
|
|
19
|
-
}
|
|
1
|
+
import { AuthFields } from '../org';
|
|
2
|
+
import { ConfigFile } from './configFile';
|
|
3
|
+
/**
|
|
4
|
+
* An auth config file that stores information such as access tokens, usernames, etc.,
|
|
5
|
+
* in the global sfdx directory (~/.sfdx).
|
|
6
|
+
*
|
|
7
|
+
* ```
|
|
8
|
+
* const authInfo = await AuthInfoConfig.create(AuthInfoConfig.getOptions(username));
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare class AuthInfoConfig extends ConfigFile<ConfigFile.Options, AuthFields> {
|
|
12
|
+
protected static encryptedKeys: RegExp[];
|
|
13
|
+
/**
|
|
14
|
+
* Gets the config options for a given org ID.
|
|
15
|
+
*
|
|
16
|
+
* @param username The username for the org.
|
|
17
|
+
*/
|
|
18
|
+
static getOptions(username: string): ConfigFile.Options;
|
|
19
|
+
}
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2022, salesforce.com, inc.
|
|
4
|
-
* All rights reserved.
|
|
5
|
-
* Licensed under the BSD 3-Clause license.
|
|
6
|
-
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.AuthInfoConfig = void 0;
|
|
10
|
-
const configFile_1 = require("./configFile");
|
|
11
|
-
/**
|
|
12
|
-
* An auth config file that stores information such as access tokens, usernames, etc.,
|
|
13
|
-
* in the global sfdx directory (~/.sfdx).
|
|
14
|
-
*
|
|
15
|
-
* ```
|
|
16
|
-
* const authInfo = await AuthInfoConfig.create(AuthInfoConfig.getOptions(username));
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
class AuthInfoConfig extends configFile_1.ConfigFile {
|
|
20
|
-
/**
|
|
21
|
-
* Gets the config options for a given org ID.
|
|
22
|
-
*
|
|
23
|
-
* @param username The username for the org.
|
|
24
|
-
*/
|
|
25
|
-
static getOptions(username) {
|
|
26
|
-
return {
|
|
27
|
-
isGlobal: true,
|
|
28
|
-
isState: true,
|
|
29
|
-
filename: `${username}.json`,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
exports.AuthInfoConfig = AuthInfoConfig;
|
|
34
|
-
AuthInfoConfig.encryptedKeys = [/token/i, /password/i, /secret/i];
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2022, salesforce.com, inc.
|
|
4
|
+
* All rights reserved.
|
|
5
|
+
* Licensed under the BSD 3-Clause license.
|
|
6
|
+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.AuthInfoConfig = void 0;
|
|
10
|
+
const configFile_1 = require("./configFile");
|
|
11
|
+
/**
|
|
12
|
+
* An auth config file that stores information such as access tokens, usernames, etc.,
|
|
13
|
+
* in the global sfdx directory (~/.sfdx).
|
|
14
|
+
*
|
|
15
|
+
* ```
|
|
16
|
+
* const authInfo = await AuthInfoConfig.create(AuthInfoConfig.getOptions(username));
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
class AuthInfoConfig extends configFile_1.ConfigFile {
|
|
20
|
+
/**
|
|
21
|
+
* Gets the config options for a given org ID.
|
|
22
|
+
*
|
|
23
|
+
* @param username The username for the org.
|
|
24
|
+
*/
|
|
25
|
+
static getOptions(username) {
|
|
26
|
+
return {
|
|
27
|
+
isGlobal: true,
|
|
28
|
+
isState: true,
|
|
29
|
+
filename: `${username}.json`,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.AuthInfoConfig = AuthInfoConfig;
|
|
34
|
+
AuthInfoConfig.encryptedKeys = [/token/i, /password/i, /secret/i];
|
|
35
35
|
//# sourceMappingURL=authInfoConfig.js.map
|