Skip to content

Commit 88ab0ac

Browse files
committed
remove mocking for happy paths
1 parent 44f62db commit 88ab0ac

File tree

5 files changed

+23
-48
lines changed

5 files changed

+23
-48
lines changed

tests/sdk/flagsmith-cache.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ beforeEach(() => {
55
});
66

77
test('test_empty_cache_not_read_but_populated', async () => {
8-
fetch.mockResolvedValue(new Response(flagsJSON));
9-
108
const cache = new TestCache();
119
const set = vi.spyOn(cache, 'set');
1210

@@ -23,8 +21,6 @@ test('test_empty_cache_not_read_but_populated', async () => {
2321
});
2422

2523
test('test_api_not_called_when_cache_present', async () => {
26-
fetch.mockResolvedValue(new Response(flagsJSON));
27-
2824
const cache = new TestCache();
2925
const set = vi.spyOn(cache, 'set');
3026

@@ -56,8 +52,6 @@ test('test_api_called_twice_when_no_cache', async () => {
5652
});
5753

5854
test('test_get_environment_flags_uses_local_environment_when_available', async () => {
59-
fetch.mockResolvedValue(new Response(flagsJSON));
60-
6155
const cache = new TestCache();
6256
const set = vi.spyOn(cache, 'set');
6357

@@ -77,8 +71,6 @@ test('test_get_environment_flags_uses_local_environment_when_available', async (
7771
});
7872

7973
test('test_cache_used_for_identity_flags', async () => {
80-
fetch.mockResolvedValue(new Response(identitiesJSON));
81-
8274
const cache = new TestCache();
8375
const set = vi.spyOn(cache, 'set');
8476

@@ -100,8 +92,6 @@ test('test_cache_used_for_identity_flags', async () => {
10092
});
10193

10294
test('test_cache_used_for_identity_flags_local_evaluation', async () => {
103-
fetch.mockResolvedValue(new Response(environmentJSON));
104-
10595
const cache = new TestCache();
10696
const set = vi.spyOn(cache, 'set');
10797

tests/sdk/flagsmith-environment-flags.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ beforeEach(() => {
99
});
1010

1111
test('test_get_environment_flags_calls_api_when_no_local_environment', async () => {
12-
fetch.mockResolvedValue(new Response(flagsJSON));
13-
1412
const flg = flagsmith();
1513
const allFlags = await (await flg.getEnvironmentFlags()).allFlags();
1614

@@ -43,8 +41,6 @@ test('test_default_flag_is_used_when_no_environment_flags_returned', async () =>
4341
});
4442

4543
test('test_analytics_processor_tracks_flags', async () => {
46-
fetch.mockResolvedValue(new Response(flagsJSON));
47-
4844
const defaultFlag = new DefaultFlag('some-default-value', true);
4945

5046
const defaultFlagHandler = (featureName: string) => defaultFlag;
@@ -64,8 +60,6 @@ test('test_analytics_processor_tracks_flags', async () => {
6460
});
6561

6662
test('test_getFeatureValue', async () => {
67-
fetch.mockResolvedValue(new Response(flagsJSON));
68-
6963
const defaultFlag = new DefaultFlag('some-default-value', true);
7064

7165
const defaultFlagHandler = (featureName: string) => defaultFlag;
@@ -108,8 +102,6 @@ test('test_non_200_response_raises_flagsmith_api_error', async () => {
108102
await expect(flg.getEnvironmentFlags()).rejects.toThrow();
109103
});
110104
test('test_default_flag_is_not_used_when_environment_flags_returned', async () => {
111-
fetch.mockResolvedValue(new Response(flagsJSON));
112-
113105
const defaultFlag = new DefaultFlag('some-default-value', true);
114106

115107
const defaultFlagHandler = (featureName: string) => defaultFlag;
@@ -147,8 +139,6 @@ test('test_default_flag_is_used_when_bad_api_response_happens', async () => {
147139
});
148140

149141
test('test_local_evaluation', async () => {
150-
fetch.mockResolvedValue(new Response(environmentJSON));
151-
152142
const defaultFlag = new DefaultFlag('some-default-value', true);
153143

154144
const defaultFlagHandler = (featureName: string) => defaultFlag;

tests/sdk/flagsmith-identity-flags.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ beforeEach(() => {
1818

1919

2020
test('test_get_identity_flags_calls_api_when_no_local_environment_no_traits', async () => {
21-
fetch.mockResolvedValue(new Response(identitiesJSON));
2221
const identifier = 'identifier';
2322

2423
const flg = flagsmith();
@@ -31,7 +30,6 @@ test('test_get_identity_flags_calls_api_when_no_local_environment_no_traits', as
3130
});
3231

3332
test('test_get_identity_flags_uses_environment_when_local_environment_no_traits', async () => {
34-
fetch.mockResolvedValue(new Response(environmentJSON))
3533
const identifier = 'identifier';
3634

3735
const flg = flagsmith({
@@ -48,7 +46,6 @@ test('test_get_identity_flags_uses_environment_when_local_environment_no_traits'
4846
});
4947

5048
test('test_get_identity_flags_calls_api_when_no_local_environment_with_traits', async () => {
51-
fetch.mockResolvedValue(new Response(identitiesJSON))
5249
const identifier = 'identifier';
5350
const traits = { some_trait: 'some_value' };
5451
const flg = flagsmith();
@@ -61,8 +58,6 @@ test('test_get_identity_flags_calls_api_when_no_local_environment_with_traits',
6158
});
6259

6360
test('test_default_flag_is_not_used_when_identity_flags_returned', async () => {
64-
fetch.mockResolvedValue(new Response(identitiesJSON))
65-
6661
const defaultFlag = new DefaultFlag('some-default-value', true);
6762

6863
const defaultFlagHandler = (featureName: string) => defaultFlag;
@@ -140,7 +135,6 @@ test('test_get_identity_flags_multivariate_value_with_local_evaluation_enabled',
140135
const flg = flagsmith({
141136
environmentKey: 'ser.key',
142137
enableLocalEvaluation: true,
143-
144138
});
145139

146140
const identityFlags = (await flg.getIdentityFlags(identifier))

tests/sdk/flagsmith.test.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { EnvironmentDataPollingManager } from '../../sdk/polling_manager.js';
33
import {
44
environmentJSON,
55
environmentModel,
6-
flagsJSON,
76
flagsmith,
87
fetch,
98
offlineEnvironmentJSON,
@@ -21,7 +20,6 @@ beforeEach(() => {
2120
});
2221

2322
test('test_flagsmith_starts_polling_manager_on_init_if_enabled', () => {
24-
fetch.mockResolvedValue(new Response(environmentJSON));
2523
new Flagsmith({
2624
environmentKey: 'ser.key',
2725
enableLocalEvaluation: true
@@ -30,7 +28,6 @@ test('test_flagsmith_starts_polling_manager_on_init_if_enabled', () => {
3028
});
3129

3230
test('test_flagsmith_local_evaluation_key_required', () => {
33-
fetch.mockResolvedValue(new Response(environmentJSON));
3431
expect(() => {
3532
new Flagsmith({
3633
environmentKey: 'bad.key',
@@ -40,16 +37,17 @@ test('test_flagsmith_local_evaluation_key_required', () => {
4037
});
4138

4239
test('test_update_environment_sets_environment', async () => {
43-
fetch.mockResolvedValue(new Response(environmentJSON));
44-
const flg = flagsmith();
40+
const flg = flagsmith({
41+
environmentKey: 'ser.key',
42+
});
4543
const model = environmentModel(JSON.parse(environmentJSON));
4644
expect(await flg.getEnvironment()).toStrictEqual(model);
4745
});
4846

4947
test('test_set_agent_options', async () => {
5048
const agent = new Agent({})
5149

52-
fetch.mockImplementation((url, options) => {
50+
fetch.mockImplementationOnce((url, options) => {
5351
//@ts-ignore I give up
5452
if (options.dispatcher !== agent) {
5553
throw new Error("Agent has not been set on retry fetch")
@@ -65,7 +63,6 @@ test('test_set_agent_options', async () => {
6563
});
6664

6765
test('test_get_identity_segments', async () => {
68-
fetch.mockResolvedValue(new Response(environmentJSON));
6966
const flg = flagsmith({
7067
environmentKey: 'ser.key',
7168
enableLocalEvaluation: true
@@ -78,7 +75,6 @@ test('test_get_identity_segments', async () => {
7875

7976

8077
test('test_get_identity_segments_empty_without_local_eval', async () => {
81-
fetch.mockResolvedValue(new Response(environmentJSON));
8278
const flg = new Flagsmith({
8379
environmentKey: 'ser.key',
8480
enableLocalEvaluation: false
@@ -88,8 +84,6 @@ test('test_get_identity_segments_empty_without_local_eval', async () => {
8884
});
8985

9086
test('test_update_environment_uses_req_when_inited', async () => {
91-
fetch.mockResolvedValue(new Response(environmentJSON));
92-
9387
const flg = flagsmith({
9488
environmentKey: 'ser.key',
9589
enableLocalEvaluation: true,
@@ -103,7 +97,6 @@ test('test_update_environment_uses_req_when_inited', async () => {
10397
});
10498

10599
test('test_isFeatureEnabled_environment', async () => {
106-
fetch.mockResolvedValue(new Response(environmentJSON));
107100
const defaultFlag = new DefaultFlag('some-default-value', true);
108101

109102
const defaultFlagHandler = (featureName: string) => defaultFlag;
@@ -121,9 +114,7 @@ test('test_isFeatureEnabled_environment', async () => {
121114
});
122115

123116
test('test_fetch_recovers_after_single_API_error', async () => {
124-
fetch
125-
.mockRejectedValue('Error during fetching the API response')
126-
.mockResolvedValue(new Response(flagsJSON));
117+
fetch.mockRejectedValue('Error during fetching the API response')
127118
const flg = flagsmith({
128119
environmentKey: 'key',
129120
});
@@ -157,8 +148,9 @@ test.each([
157148

158149
test('default flag handler used when timeout occurs', async () => {
159150
fetch.mockImplementation(async (...args) => {
160-
await sleep(10000)
161-
return fetch(...args)
151+
const forever = new Promise(() => {})
152+
await forever
153+
throw new Error('waited forever')
162154
});
163155

164156
const defaultFlag = new DefaultFlag('some-default-value', true);

tests/sdk/utils.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,24 @@ export class TestCache implements FlagsmithCache {
1919
}
2020
}
2121

22-
export const fetch = vi.fn((_, options) => {
22+
export const fetch = vi.fn((url: string, options?: RequestInit) => {
2323
const headers = options?.headers as Record<string, string>;
2424
if (!headers) throw new Error('missing request headers')
2525
const env = headers['X-Environment-Key'];
26-
if (!env) return Promise.resolve(new Response("missing x-environment-key header", { status: 404 }))
26+
if (!env) return Promise.resolve(new Response('missing x-environment-key header', { status: 404 }));
2727
if (env.startsWith('ser.')) {
28-
return Promise.resolve(new Response(environmentJSON, { status: 200 }))
28+
if (url.includes('/environment-document')) {
29+
return Promise.resolve(new Response(environmentJSON, { status: 200 }))
30+
}
31+
return Promise.resolve(new Response('environment-document called without a server-side key', { status: 401 }))
2932
}
30-
return Promise.resolve(new Response(identitiesJSON, { status: 200 }))
33+
if (url.includes("/flags")) {
34+
return Promise.resolve(new Response(flagsJSON, { status: 200 }))
35+
}
36+
if (url.includes("/identities")) {
37+
return Promise.resolve(new Response(identitiesJSON, { status: 200 }))
38+
}
39+
return Promise.resolve(new Response('unknown url ' + url, { status: 404 }))
3140
});
3241

3342
export const badFetch: Fetch = () => { throw new Error('fetch failed')}
@@ -36,7 +45,7 @@ export function analyticsProcessor() {
3645
return new AnalyticsProcessor({
3746
environmentKey: 'test-key',
3847
analyticsUrl: 'http://testUrl/analytics/flags/',
39-
fetch,
48+
fetch: (url, options) => fetch(url.toString(), options),
4049
});
4150
}
4251

@@ -49,7 +58,7 @@ export function flagsmith(params: FlagsmithConfig = {}) {
4958
environmentKey: apiKey(),
5059
environmentRefreshIntervalSeconds: 0,
5160
requestRetryDelayMilliseconds: 0,
52-
fetch,
61+
fetch: (url, options) => fetch(url.toString(), options),
5362
...params,
5463
});
5564
}

0 commit comments

Comments
 (0)