Skip to content

Commit cbfcb2c

Browse files
committed
work on tests
1 parent cdfe9e7 commit cbfcb2c

File tree

5 files changed

+39
-41
lines changed

5 files changed

+39
-41
lines changed

src/feature-toggles.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const CONFIG_INFO_KEY = {
7171
[CONFIG_KEY.TYPE]: true,
7272
[CONFIG_KEY.ACTIVE]: true,
7373
[CONFIG_KEY.SOURCE]: true,
74+
[CONFIG_KEY.SOURCE_FILEPATH]: true,
7475
[CONFIG_KEY.APP_URL]: true,
7576
[CONFIG_KEY.APP_URL_ACTIVE]: true,
7677
[CONFIG_KEY.VALIDATIONS]: true,

test/feature-toggles.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ describe("feature toggles test", () => {
6666

6767
describe("enums", () => {
6868
test("config info consistency", () => {
69-
const internalKeys = [
70-
CONFIG_KEY.VALIDATIONS_SCOPES_MAP,
71-
CONFIG_KEY.VALIDATIONS_REGEX,
72-
CONFIG_KEY.SOURCE_FILEPATH,
73-
];
69+
const internalKeys = [CONFIG_KEY.VALIDATIONS_SCOPES_MAP, CONFIG_KEY.VALIDATIONS_REGEX];
7470
const configKeysCheck = [].concat(Object.keys(CONFIG_INFO_KEY), internalKeys).sort();
7571
const configKeys = Object.values(CONFIG_KEY).sort();
7672

test/integration-local/__snapshots__/feature-toggles.integration.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,15 @@ exports[`local integration test init init config works for runtime file auto sim
153153
"test/feature_c": {
154154
"config": {
155155
"SOURCE": "FILE",
156+
"SOURCE_FILEPATH": "toggles.json",
156157
"TYPE": "string",
157158
},
158159
"fallbackValue": "fallbackFileC",
159160
},
160161
"test/feature_d": {
161162
"config": {
162163
"SOURCE": "FILE",
164+
"SOURCE_FILEPATH": "toggles.json",
163165
"TYPE": "string",
164166
},
165167
"fallbackValue": "fallbackFileD",

test/integration-local/feature-toggles.integration.test.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ jest.mock("fs", () => ({
1111
access: mockAccess,
1212
}));
1313

14-
const VError = require("verror");
15-
1614
const { stateFromInfo } = require("../__common__/from-info");
1715
const { 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
`);

test/shared/logger.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe("logger test", () => {
8585
}",
8686
],
8787
[
88-
"88:88:88.888 | INFO | /test | finished initialization with 9 feature toggles (9 runtime, 0 file, 0 auto) using CF_REDIS",
88+
"88:88:88.888 | INFO | /test | finished initialization with 9 feature toggles (0 auto, 0 file, 9 runtime) using CF_REDIS",
8989
],
9090
]
9191
`);
@@ -127,7 +127,7 @@ describe("logger test", () => {
127127
"{"level":"warn","msg":"FeatureTogglesError: found invalid fallback values during initialization\\n{\\n validationErrors: '[{\\"featureKey\\":\\"test/feature_b\\",\\"errorMessage\\":\\"registered validator \\\\\\\\\\"{0}\\\\\\\\\\" failed for value \\\\\\\\\\"{1}\\\\\\\\\\" with error {2}\\",\\"errorMessageValues\\":[\\"mockConstructor\\",1,\\"bad validator\\"]}]'\\n}","type":"log","layer":"/test"}",
128128
],
129129
[
130-
"{"level":"info","msg":"finished initialization with 9 feature toggles (9 runtime, 0 file, 0 auto) using CF_REDIS","type":"log","layer":"/test"}",
130+
"{"level":"info","msg":"finished initialization with 9 feature toggles (0 auto, 0 file, 9 runtime) using CF_REDIS","type":"log","layer":"/test"}",
131131
],
132132
]
133133
`);

0 commit comments

Comments
 (0)