Skip to content

Commit 5ae5c50

Browse files
committed
chore: remove casting in ConfigLoader tests
1 parent 890d583 commit 5ae5c50

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

test/ConfigLoader.test.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import path from 'path';
44
import { configFile } from '../src/config/file';
55
import {
66
ConfigLoader,
7+
isValidGitUrl,
8+
isValidPath,
9+
isValidBranchName,
10+
} from '../src/config/ConfigLoader';
11+
import {
712
Configuration,
13+
ConfigurationSource,
814
FileSource,
915
GitSource,
1016
HttpSource,
11-
} from '../src/config/ConfigLoader';
12-
import { isValidGitUrl, isValidPath, isValidBranchName } from '../src/config/ConfigLoader';
17+
} from '../src/config/types';
1318
import axios from 'axios';
1419

1520
describe('ConfigLoader', () => {
@@ -108,7 +113,7 @@ describe('ConfigLoader', () => {
108113

109114
describe('reloadConfiguration', () => {
110115
it('should emit configurationChanged event when config changes', async () => {
111-
const initialConfig = {
116+
const initialConfig: Configuration = {
112117
configurationSources: {
113118
enabled: true,
114119
sources: [
@@ -128,7 +133,7 @@ describe('ConfigLoader', () => {
128133

129134
fs.writeFileSync(tempConfigFile, JSON.stringify(newConfig));
130135

131-
configLoader = new ConfigLoader(initialConfig as Configuration);
136+
configLoader = new ConfigLoader(initialConfig);
132137
const spy = vi.fn();
133138
configLoader.on('configurationChanged', spy);
134139

@@ -143,7 +148,7 @@ describe('ConfigLoader', () => {
143148
proxyUrl: 'https://test.com',
144149
};
145150

146-
const config = {
151+
const config: Configuration = {
147152
configurationSources: {
148153
enabled: true,
149154
sources: [
@@ -159,7 +164,7 @@ describe('ConfigLoader', () => {
159164

160165
fs.writeFileSync(tempConfigFile, JSON.stringify(testConfig));
161166

162-
configLoader = new ConfigLoader(config as Configuration);
167+
configLoader = new ConfigLoader(config);
163168
const spy = vi.fn();
164169
configLoader.on('configurationChanged', spy);
165170

@@ -170,13 +175,15 @@ describe('ConfigLoader', () => {
170175
});
171176

172177
it('should not emit event if configurationSources is disabled', async () => {
173-
const config = {
178+
const config: Configuration = {
174179
configurationSources: {
175180
enabled: false,
181+
sources: [],
182+
reloadIntervalSeconds: 0,
176183
},
177184
};
178185

179-
configLoader = new ConfigLoader(config as Configuration);
186+
configLoader = new ConfigLoader(config);
180187
const spy = vi.fn();
181188
configLoader.on('configurationChanged', spy);
182189

@@ -220,7 +227,7 @@ describe('ConfigLoader', () => {
220227

221228
describe('start', () => {
222229
it('should perform initial load on start if configurationSources is enabled', async () => {
223-
const mockConfig = {
230+
const mockConfig: Configuration = {
224231
configurationSources: {
225232
enabled: true,
226233
sources: [
@@ -230,19 +237,19 @@ describe('ConfigLoader', () => {
230237
path: tempConfigFile,
231238
},
232239
],
233-
reloadIntervalSeconds: 30,
240+
reloadIntervalSeconds: 0,
234241
},
235242
};
236243

237-
configLoader = new ConfigLoader(mockConfig as Configuration);
244+
configLoader = new ConfigLoader(mockConfig);
238245
const spy = vi.spyOn(configLoader, 'reloadConfiguration');
239246
await configLoader.start();
240247

241248
expect(spy).toHaveBeenCalledOnce();
242249
});
243250

244251
it('should clear an existing reload interval if it exists', async () => {
245-
const mockConfig = {
252+
const mockConfig: Configuration = {
246253
configurationSources: {
247254
enabled: true,
248255
sources: [
@@ -252,17 +259,20 @@ describe('ConfigLoader', () => {
252259
path: tempConfigFile,
253260
},
254261
],
262+
reloadIntervalSeconds: 0,
255263
},
256264
};
257265

258-
configLoader = new ConfigLoader(mockConfig as Configuration);
266+
configLoader = new ConfigLoader(mockConfig);
267+
268+
// private property overridden for testing
259269
(configLoader as any).reloadTimer = setInterval(() => {}, 1000);
260270
await configLoader.start();
261271
expect((configLoader as any).reloadTimer).toBe(null);
262272
});
263273

264274
it('should run reloadConfiguration multiple times on short reload interval', async () => {
265-
const mockConfig = {
275+
const mockConfig: Configuration = {
266276
configurationSources: {
267277
enabled: true,
268278
sources: [
@@ -276,7 +286,7 @@ describe('ConfigLoader', () => {
276286
},
277287
};
278288

279-
configLoader = new ConfigLoader(mockConfig as Configuration);
289+
configLoader = new ConfigLoader(mockConfig);
280290
const spy = vi.spyOn(configLoader, 'reloadConfiguration');
281291
await configLoader.start();
282292

@@ -287,7 +297,7 @@ describe('ConfigLoader', () => {
287297
});
288298

289299
it('should clear the interval when stop is called', async () => {
290-
const mockConfig = {
300+
const mockConfig: Configuration = {
291301
configurationSources: {
292302
enabled: true,
293303
sources: [
@@ -297,10 +307,13 @@ describe('ConfigLoader', () => {
297307
path: tempConfigFile,
298308
},
299309
],
310+
reloadIntervalSeconds: 0,
300311
},
301312
};
302313

303-
configLoader = new ConfigLoader(mockConfig as Configuration);
314+
configLoader = new ConfigLoader(mockConfig);
315+
316+
// private property overridden for testing
304317
(configLoader as any).reloadTimer = setInterval(() => {}, 1000);
305318
expect((configLoader as any).reloadTimer).not.toBe(null);
306319
await configLoader.stop();
@@ -403,69 +416,69 @@ describe('ConfigLoader', () => {
403416
});
404417

405418
it('should throw error if configuration source is invalid', async () => {
406-
const source = {
407-
type: 'invalid',
419+
const source: ConfigurationSource = {
420+
type: 'invalid' as any, // invalid type
408421
repository: 'https://github.com/finos/git-proxy.git',
409422
path: 'proxy.config.json',
410423
branch: 'main',
411424
enabled: true,
412-
} as any;
425+
};
413426

414427
await expect(configLoader.loadFromSource(source)).rejects.toThrow(
415428
/Unsupported configuration source type/,
416429
);
417430
});
418431

419432
it('should throw error if repository is a valid URL but not a git repository', async () => {
420-
const source = {
433+
const source: ConfigurationSource = {
421434
type: 'git',
422435
repository: 'https://github.com/finos/made-up-test-repo.git',
423436
path: 'proxy.config.json',
424437
branch: 'main',
425438
enabled: true,
426-
} as GitSource;
439+
};
427440

428441
await expect(configLoader.loadFromSource(source)).rejects.toThrow(
429442
/Failed to clone repository/,
430443
);
431444
});
432445

433446
it('should throw error if repository is a valid git repo but the branch does not exist', async () => {
434-
const source = {
447+
const source: ConfigurationSource = {
435448
type: 'git',
436449
repository: 'https://github.com/finos/git-proxy.git',
437450
path: 'proxy.config.json',
438451
branch: 'branch-does-not-exist',
439452
enabled: true,
440-
} as GitSource;
453+
};
441454

442455
await expect(configLoader.loadFromSource(source)).rejects.toThrow(
443456
/Failed to checkout branch/,
444457
);
445458
});
446459

447460
it('should throw error if config path was not found', async () => {
448-
const source = {
461+
const source: ConfigurationSource = {
449462
type: 'git',
450463
repository: 'https://github.com/finos/git-proxy.git',
451464
path: 'path-not-found.json',
452465
branch: 'main',
453466
enabled: true,
454-
} as GitSource;
467+
};
455468

456469
await expect(configLoader.loadFromSource(source)).rejects.toThrow(
457470
/Configuration file not found at/,
458471
);
459472
});
460473

461474
it('should throw error if config file is not valid JSON', async () => {
462-
const source = {
475+
const source: ConfigurationSource = {
463476
type: 'git',
464477
repository: 'https://github.com/finos/git-proxy.git',
465478
path: 'test/fixtures/baz.js',
466479
branch: 'main',
467480
enabled: true,
468-
} as GitSource;
481+
};
469482

470483
await expect(configLoader.loadFromSource(source)).rejects.toThrow(
471484
/Failed to read or parse configuration file/,

0 commit comments

Comments
 (0)