Skip to content

Commit 4dbe196

Browse files
committed
feat(#93): adds mockConfig function export for testing
1 parent c3f7d43 commit 4dbe196

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

app-config/src/index.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { inspect } from 'util';
2-
import { config, loadConfig, resetConfigInternal } from './index';
2+
import { config, loadConfig, mockConfig, resetConfigInternal } from './index';
33
import { withTempFiles } from './test-util';
44

55
beforeEach(() => resetConfigInternal());
@@ -142,3 +142,12 @@ describe('loadConfig', () => {
142142
);
143143
});
144144
});
145+
146+
describe('mockConfig', () => {
147+
it('mocks out the config expert', async () => {
148+
mockConfig({ foo: 'bar' });
149+
expect(config).toEqual({ foo: 'bar' });
150+
await expect(() => loadConfig()).rejects.toThrow();
151+
});
152+
});
153+

app-config/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ExportedConfig {}
1010

1111
// the export of this module is a proxy in front of this value
1212
let loadedConfig: ExportedConfig | undefined;
13+
let isMocked = false;
1314

1415
const assertLoaded = () => {
1516
if (!loadedConfig) {
@@ -24,6 +25,10 @@ export async function loadConfig(
2425
): Promise<ExportedConfig> {
2526
if (loadedConfig) {
2627
logger.warn('Called loadConfig, even though config was already loaded elsewhere');
28+
29+
if (isMocked) {
30+
throw new AppConfigError(`Called loadConfig after config was mocked with mockConfig!`);
31+
}
2732
}
2833

2934
const { fullConfig } = await loadValidatedConfig(options, schemaOptions);
@@ -120,3 +125,11 @@ export {
120125
export function resetConfigInternal() {
121126
loadedConfig = undefined;
122127
}
128+
129+
/**
130+
* Overrides the configuration internally, setting it to the provided override.
131+
*/
132+
export function mockConfig(override: ExportedConfig) {
133+
loadedConfig = override;
134+
isMocked = true;
135+
}

0 commit comments

Comments
 (0)