@@ -16,14 +16,40 @@ describe('ensureValidConfiguration', () => {
1616 beforeEach ( ( ) => {
1717 jest . clearAllMocks ( ) ;
1818 configService = configServiceMock . create ( ) ;
19- configService . getUsedPaths . mockReturnValue ( Promise . resolve ( [ 'core' , 'elastic' ] ) ) ;
19+
20+ configService . validate . mockResolvedValue ( ) ;
21+ configService . getUsedPaths . mockReturnValue ( Promise . resolve ( [ ] ) ) ;
2022 } ) ;
2123
22- it ( 'returns normally when there is no unused keys' , async ( ) => {
23- configService . getUnusedPaths . mockResolvedValue ( [ ] ) ;
24+ it ( 'returns normally when there is no unused keys and when the config validates' , async ( ) => {
2425 await expect ( ensureValidConfiguration ( configService as any ) ) . resolves . toBeUndefined ( ) ;
2526 } ) ;
2627
28+ it ( 'throws when config validation fails' , async ( ) => {
29+ configService . validate . mockImplementation ( ( ) => {
30+ throw new Error ( 'some message' ) ;
31+ } ) ;
32+
33+ await expect ( ensureValidConfiguration ( configService as any ) ) . rejects . toMatchInlineSnapshot (
34+ `[Error: some message]`
35+ ) ;
36+ } ) ;
37+
38+ it ( 'throws a `CriticalError` with the correct processExitCode value when config validation fails' , async ( ) => {
39+ expect . assertions ( 2 ) ;
40+
41+ configService . validate . mockImplementation ( ( ) => {
42+ throw new Error ( 'some message' ) ;
43+ } ) ;
44+
45+ try {
46+ await ensureValidConfiguration ( configService as any ) ;
47+ } catch ( e ) {
48+ expect ( e ) . toBeInstanceOf ( CriticalError ) ;
49+ expect ( e . processExitCode ) . toEqual ( 78 ) ;
50+ }
51+ } ) ;
52+
2753 it ( 'throws when there are some unused keys' , async ( ) => {
2854 configService . getUnusedPaths . mockResolvedValue ( [ 'some.key' , 'some.other.key' ] ) ;
2955
@@ -44,4 +70,18 @@ describe('ensureValidConfiguration', () => {
4470 expect ( e . processExitCode ) . toEqual ( 64 ) ;
4571 }
4672 } ) ;
73+
74+ it ( 'does not throw when all unused keys are included in the ignored paths' , async ( ) => {
75+ configService . getUnusedPaths . mockResolvedValue ( [ 'dev.someDevKey' , 'elastic.apm.enabled' ] ) ;
76+
77+ await expect ( ensureValidConfiguration ( configService as any ) ) . resolves . toBeUndefined ( ) ;
78+ } ) ;
79+
80+ it ( 'throws when only some keys are included in the ignored paths' , async ( ) => {
81+ configService . getUnusedPaths . mockResolvedValue ( [ 'dev.someDevKey' , 'some.key' ] ) ;
82+
83+ await expect ( ensureValidConfiguration ( configService as any ) ) . rejects . toMatchInlineSnapshot (
84+ `[Error: Unknown configuration key(s): "some.key". Check for spelling errors and ensure that expected plugins are installed.]`
85+ ) ;
86+ } ) ;
4787} ) ;
0 commit comments