@@ -11,8 +11,6 @@ jest.mock("fs", () => ({
1111 access : mockAccess ,
1212} ) ) ;
1313
14- const VError = require ( "verror" ) ;
15-
1614const { stateFromInfo } = require ( "../__common__/from-info" ) ;
1715const { FEATURE , mockConfig : config } = require ( "../__common__/mockdata" ) ;
1816
@@ -94,65 +92,66 @@ describe("local integration test", () => {
9492 ) ;
9593 } ) ;
9694
97- test ( "init config conflict between runtime and file throws " , async ( ) => {
95+ test ( "init config conflict between file and runtime overrides in favor of runtime " , async ( ) => {
9896 const configForConflict = {
9997 ...configForRuntime ,
10098 ...configForFile ,
10199 } ;
102100 mockReadFile . mockImplementationOnce ( ( path , cb ) => cb ( null , Buffer . from ( JSON . stringify ( configForConflict ) ) ) ) ;
101+ const checkKey = Object . keys ( configForRuntime ) [ 0 ] ;
103102
104- const caught = await toggles
105- . initializeFeatures ( { config : configForRuntime , configFile : "toggles.json" } )
106- . catch ( ( err ) => err ) ;
107- expect ( caught ) . toMatchInlineSnapshot (
108- `[FeatureTogglesError: initialization aborted, could not process configuration: feature is configured twice]`
109- ) ;
110- expect ( VError . info ( caught ) ) . toMatchInlineSnapshot ( `
103+ await expect (
104+ toggles . initializeFeatures ( { config : configForRuntime , configFile : "toggles.json" } )
105+ ) . resolves . toBeDefined ( ) ;
106+ expect ( toggles . getFeatureInfo ( checkKey ) ) . toMatchInlineSnapshot ( `
111107 {
112- "featureKey": "test/feature_a",
113- "sourceConflicting": "FILE",
114- "sourceExisting": "RUNTIME",
115- "sourceFilepathConflicting": "toggles.json",
108+ "config": {
109+ "SOURCE": "RUNTIME",
110+ "TYPE": "string",
111+ },
112+ "fallbackValue": "fallbackRuntimeA",
116113 }
117114 ` ) ;
118115 } ) ;
119116
120- test ( "init config conflict between file A and file B throws " , async ( ) => {
117+ test ( "init config conflict between file A and file B overrides in favor of file B " , async ( ) => {
121118 mockReadFile . mockImplementationOnce ( ( path , cb ) => cb ( null , Buffer . from ( JSON . stringify ( configForFile ) ) ) ) ;
122119 mockReadFile . mockImplementationOnce ( ( path , cb ) => cb ( null , Buffer . from ( JSON . stringify ( configForFile ) ) ) ) ;
120+ const checkKey = Object . keys ( configForFile ) [ 0 ] ;
123121
124- const caught = await toggles
125- . initializeFeatures ( { configFiles : [ "toggles-1.json" , "toggles-2.json" ] } )
126- . catch ( ( err ) => err ) ;
127- expect ( caught ) . toMatchInlineSnapshot (
128- `[FeatureTogglesError: initialization aborted, could not process configuration: feature is configured twice]`
129- ) ;
130- expect ( VError . info ( caught ) ) . toMatchInlineSnapshot ( `
122+ await expect (
123+ toggles . initializeFeatures ( { configFiles : [ "toggles-1.json" , "toggles-2.json" ] } )
124+ ) . resolves . toBeDefined ( ) ;
125+ expect ( toggles . getFeatureInfo ( checkKey ) ) . toMatchInlineSnapshot ( `
131126 {
132- "featureKey": "test/feature_c",
133- "sourceConflicting": "FILE",
134- "sourceExisting": "FILE",
135- "sourceFilepathConflicting": "toggles-2.json",
136- "sourceFilepathExisting": "toggles-1.json",
127+ "config": {
128+ "SOURCE": "FILE",
129+ "SOURCE_FILEPATH": "toggles-2.json",
130+ "TYPE": "string",
131+ },
132+ "fallbackValue": "fallbackFileC",
137133 }
138134 ` ) ;
139135 } ) ;
140136
141- test ( "init config conflict between file and auto preserves " , async ( ) => {
137+ test ( "init config conflict between auto and file overrides in favor of file " , async ( ) => {
142138 const firstEntry = Object . entries ( configForFile ) [ 0 ] ;
143139 const configForConflict = Object . fromEntries (
144140 [ firstEntry ] . map ( ( [ key ] ) => [ key , { type : "number" , fallbackValue : 0 } ] )
145141 ) ;
146142 mockReadFile . mockImplementationOnce ( ( filepath , callback ) =>
147143 callback ( null , Buffer . from ( JSON . stringify ( configForFile ) ) )
148144 ) ;
145+ const checkKey = firstEntry [ 0 ] ;
146+
149147 await expect (
150148 toggles . initializeFeatures ( { configFile : "toggles.json" , configAuto : configForConflict } )
151149 ) . resolves . toBeDefined ( ) ;
152- expect ( toggles . getFeatureInfo ( firstEntry [ 0 ] ) ) . toMatchInlineSnapshot ( `
150+ expect ( toggles . getFeatureInfo ( checkKey ) ) . toMatchInlineSnapshot ( `
153151 {
154152 "config": {
155153 "SOURCE": "FILE",
154+ "SOURCE_FILEPATH": "toggles.json",
156155 "TYPE": "string",
157156 },
158157 "fallbackValue": "fallbackFileC",
@@ -173,7 +172,7 @@ describe("local integration test", () => {
173172 expect ( featureTogglesLoggerSpy . info ) . toHaveBeenCalledTimes ( 1 ) ;
174173 expect ( featureTogglesLoggerSpy . info . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
175174 [
176- "finished initialization of "unicorn" with 5 feature toggles (2 runtime , 2 file, 1 auto ) using NO_REDIS",
175+ "finished initialization of "unicorn" with 5 feature toggles (1 auto , 2 file, 2 runtime ) using NO_REDIS",
177176 ]
178177 ` ) ;
179178 } ) ;
@@ -369,7 +368,7 @@ describe("local integration test", () => {
369368 expect ( featureTogglesLoggerSpy . info . mock . calls ) . toMatchInlineSnapshot ( `
370369 [
371370 [
372- "finished initialization of "unicorn" with 9 feature toggles (9 runtime , 0 file, 0 auto ) using NO_REDIS",
371+ "finished initialization of "unicorn" with 9 feature toggles (0 auto , 0 file, 9 runtime ) using NO_REDIS",
373372 ],
374373 ]
375374 ` ) ;
@@ -399,7 +398,7 @@ describe("local integration test", () => {
399398 expect ( featureTogglesLoggerSpy . info . mock . calls ) . toMatchInlineSnapshot ( `
400399 [
401400 [
402- "finished initialization of "unicorn" with 9 feature toggles (9 runtime , 0 file, 0 auto ) using NO_REDIS",
401+ "finished initialization of "unicorn" with 9 feature toggles (0 auto , 0 file, 9 runtime ) using NO_REDIS",
403402 ],
404403 ]
405404 ` ) ;
@@ -536,7 +535,7 @@ describe("local integration test", () => {
536535 expect ( featureTogglesLoggerSpy . info . mock . calls ) . toMatchInlineSnapshot ( `
537536 [
538537 [
539- "finished initialization of "unicorn" with 9 feature toggles (9 runtime , 0 file, 0 auto ) using NO_REDIS",
538+ "finished initialization of "unicorn" with 9 feature toggles (0 auto , 0 file, 9 runtime ) using NO_REDIS",
540539 ],
541540 ]
542541 ` ) ;
@@ -633,7 +632,7 @@ describe("local integration test", () => {
633632 expect ( featureTogglesLoggerSpy . info . mock . calls ) . toMatchInlineSnapshot ( `
634633 [
635634 [
636- "finished initialization of "unicorn" with 9 feature toggles (9 runtime , 0 file, 0 auto ) using NO_REDIS",
635+ "finished initialization of "unicorn" with 9 feature toggles (0 auto , 0 file, 9 runtime ) using NO_REDIS",
637636 ],
638637 ]
639638 ` ) ;
0 commit comments