Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 8155a99

Browse files
authored
Merge pull request #57 from JupiterOne/generate-config
Generate instance config in mock context if instanceConfig option is not provided
2 parents 32dec56 + 3a2d745 commit 8155a99

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ and this project adheres to
88

99
## [Unreleased]
1010

11+
### Changed
12+
13+
- If unable to create a config from environment variables,
14+
`createMockExecutionContext` and `createMockStepExecutionContext` will now
15+
generate a mock configuration for testing.
16+
1117
## 0.5.0 - 2020-04-20
1218

1319
### Added

docs/testing.md

+30-4
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,50 @@ If the `instanceConfig` option is not set, the SDK will read the
101101
`src/instanceConfigFields.json` and load in values from environment variables
102102
and the project's `.env` file if present.
103103

104+
If unable to load a config from environment variables, the function will create
105+
a configuration with mock values based the types specified in
106+
`src/instanceConfigFields.json`.
107+
108+
Example `instanceConfigFields.json` file:
109+
110+
```json
111+
{
112+
"apiKey": {
113+
"type": "string"
114+
}
115+
}
116+
```
117+
104118
Usage:
105119

106120
```typescript
107121
// creates a simple execution context with a mocked
108-
// logger and instance without a configuration
109-
const simpleContext = createMockExecutionContext();
122+
// logger and an instance with mocked values
123+
const contextWithGeneratedConfig = createMockExecutionContext();
124+
/**
125+
* Returns:
126+
*
127+
* {
128+
* logger: <mockLogger>,
129+
* instance: {...<local integration instance>, config: { apiKey: 'STRING_VALUE' } }
130+
* }
131+
*/
132+
133+
// constructs instance config using environment variables
134+
process.env.API_KEY = 'my-api-key';
135+
const contextWithConfigBuiltFromEnv = createMockExecutionContext();
110136
/**
111137
* Returns:
112138
*
113139
* {
114140
* logger: <mockLogger>,
115-
* instance: <local integration instance>
141+
* instance: {...<local integration instance>, config: { apiKey: 'my-api-key' } }
116142
* }
117143
*/
118144

119145
// creates an execution context with a mocked
120146
// logger and instance populated with the input config
121-
const contextWithInstanceConfig = createMockExecutionContext({
147+
const contextWithProvidedInstanceConfig = createMockExecutionContext({
122148
instanceConfig: {
123149
apiKey: 'test',
124150
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"myBooleanConfig": {
3+
"type": "boolean"
4+
},
5+
"myStringConfig": {
6+
"type": "string"
7+
},
8+
"myTypelessConfig": {}
9+
}
10+

src/testing/__tests__/context.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ import {
6363
},
6464
});
6565
});
66+
67+
test('generates instance config with fake values if unable to populate config with env values', () => {
68+
loadProjectStructure('instanceConfigWithoutEnv');
69+
70+
const { instance } = createContext();
71+
expect(instance).toEqual({
72+
...LOCAL_INTEGRATION_INSTANCE,
73+
config: {
74+
myBooleanConfig: true,
75+
myStringConfig: 'STRING_VALUE',
76+
myTypelessConfig: 'STRING_VALUE',
77+
},
78+
});
79+
});
6680
});
6781
},
6882
);

src/testing/context.ts

+29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {
22
IntegrationInstance,
33
IntegrationExecutionContext,
44
IntegrationStepExecutionContext,
5+
IntegrationInstanceConfigField,
6+
IntegrationInstanceConfigFieldMap,
57
} from '../framework';
68

79
import { loadInstanceConfigFields } from '../framework/config';
@@ -41,9 +43,12 @@ export function createMockExecutionContext({
4143
// failed to load configuration, not end of the world
4244
// because this is only used in testing
4345
//
46+
// For convenience, we will generate a config for the developer
47+
//
4448
// this would generally only happen when a developer does not
4549
// have an .env file configured or when an integration's test suite
4650
// runs in CI
51+
instance.config = generateInstanceConfig(configFields);
4752
}
4853
}
4954
}
@@ -70,3 +75,27 @@ export function createMockStepExecutionContext(
7075
jobState: createMockJobState(options),
7176
};
7277
}
78+
79+
function generateInstanceConfig(
80+
configFields: IntegrationInstanceConfigFieldMap,
81+
): IntegrationInstance['config'] {
82+
return Object.entries(configFields).reduce(
83+
(acc: IntegrationInstance['config'], [field, config]) => {
84+
acc[field] = getInstanceConfigValueFromType(config);
85+
return acc;
86+
},
87+
{},
88+
);
89+
}
90+
91+
function getInstanceConfigValueFromType(
92+
config: IntegrationInstanceConfigField,
93+
) {
94+
switch (config.type) {
95+
case 'boolean':
96+
return true;
97+
case 'string':
98+
default:
99+
return 'STRING_VALUE';
100+
}
101+
}

0 commit comments

Comments
 (0)