Skip to content

Commit ec49023

Browse files
authored
feat(parameters): add clearCaches function (#1382)
1 parent 051f24c commit ec49023

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Diff for: packages/parameters/src/BaseProvider.ts

+12
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,21 @@ const transformValues = (value: Record<string, string | undefined>, transform: T
220220
return transformedValues;
221221
};
222222

223+
/**
224+
* Utility function to clear all the caches of the default providers.
225+
*
226+
* This is useful when you want to clear the cache of all the providers at once, for example during testing.
227+
*/
228+
const clearCaches = (): void => {
229+
for (const provider of Object.values(DEFAULT_PROVIDERS)) {
230+
provider.clearCache();
231+
}
232+
};
233+
223234
export {
224235
BaseProvider,
225236
ExpirableValue,
226237
transformValue,
227238
DEFAULT_PROVIDERS,
239+
clearCaches,
228240
};

Diff for: packages/parameters/tests/unit/BaseProvider.test.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
BaseProvider,
88
ExpirableValue,
99
GetParameterError,
10-
TransformParameterError
10+
TransformParameterError,
11+
clearCaches,
12+
DEFAULT_PROVIDERS,
1113
} from '../../src';
1214
import { toBase64 } from '@aws-sdk/util-base64-node';
1315

@@ -480,4 +482,39 @@ describe('Class: BaseProvider', () => {
480482

481483
});
482484

485+
});
486+
487+
describe('Function: clearCaches', () => {
488+
489+
class TestProvider extends BaseProvider {
490+
491+
public _get(_name: string): Promise<string> {
492+
throw Error('Not implemented.');
493+
}
494+
495+
public _getMultiple(_path: string): Promise<Record<string, string | undefined>> {
496+
throw Error('Not implemented.');
497+
}
498+
499+
}
500+
501+
test('when called, it clears all the caches', () => {
502+
503+
// Prepare
504+
const provider1 = new TestProvider();
505+
const provider2 = new TestProvider();
506+
const provider1Spy = jest.spyOn(provider1, 'clearCache');
507+
const provider2Spy = jest.spyOn(provider2, 'clearCache');
508+
DEFAULT_PROVIDERS.ssm = provider1;
509+
DEFAULT_PROVIDERS.secretsManager = provider2;
510+
511+
// Act
512+
clearCaches();
513+
514+
// Assess
515+
expect(provider1Spy).toBeCalledTimes(1);
516+
expect(provider2Spy).toBeCalledTimes(1);
517+
518+
});
519+
483520
});

0 commit comments

Comments
 (0)