Skip to content

Commit 2e882cf

Browse files
committed
feat: converted config-loader test to typescript
Migrates the ConfigLoader test suite from JavaScript to TypeScript for improved type safety and maintainability
1 parent e9b0924 commit 2e882cf

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"build-lib": "./scripts/build-for-publish.sh",
1414
"restore-lib": "./scripts/undo-build.sh",
1515
"check-types": "tsc",
16-
"test": "NODE_ENV=test ts-mocha './test/*.js' --exit",
16+
"test": "NODE_ENV=test ts-mocha './test/**/*.{js,ts}' --exit",
1717
"test-coverage": "nyc npm run test",
1818
"test-coverage-ci": "nyc --reporter=lcovonly --reporter=text npm run test",
1919
"prepare": "node ./scripts/prepare.js",
@@ -90,6 +90,7 @@
9090
"@types/lodash": "^4.17.15",
9191
"@types/mocha": "^10.0.10",
9292
"@types/node": "^22.13.5",
93+
"@types/sinon": "^17.0.4",
9394
"@types/yargs": "^17.0.33",
9495
"@typescript-eslint/eslint-plugin": "^8.26.1",
9596
"@typescript-eslint/parser": "^8.26.1",

test/ConfigLoader.test.js renamed to test/ConfigLoader.test.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
const chai = require('chai');
2-
const fs = require('fs');
3-
const path = require('path');
4-
const { expect } = chai;
5-
const ConfigLoader = require('../src/config/ConfigLoader');
6-
const { isValidGitUrl, isValidPath, isValidBranchName } = ConfigLoader;
7-
const sinon = require('sinon');
8-
const axios = require('axios');
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { expect } from 'chai';
4+
import { ConfigLoader, Configuration } from '../src/config/ConfigLoader';
5+
import { isValidGitUrl, isValidPath, isValidBranchName } from '../src/config/ConfigLoader';
6+
import sinon from 'sinon';
7+
import axios from 'axios';
98

109
describe('ConfigLoader', () => {
11-
let configLoader;
12-
let tempDir;
13-
let tempConfigFile;
10+
let configLoader: ConfigLoader;
11+
let tempDir: string;
12+
let tempConfigFile: string;
1413

1514
beforeEach(() => {
1615
// Create temp directory for test files
@@ -35,7 +34,11 @@ describe('ConfigLoader', () => {
3534
fs.writeFileSync(tempConfigFile, JSON.stringify(testConfig));
3635

3736
configLoader = new ConfigLoader({});
38-
const result = await configLoader.loadFromFile({ path: tempConfigFile });
37+
const result = await configLoader.loadFromFile({
38+
type: 'file',
39+
enabled: true,
40+
path: tempConfigFile,
41+
});
3942

4043
expect(result).to.deep.equal(testConfig);
4144
});
@@ -52,6 +55,8 @@ describe('ConfigLoader', () => {
5255

5356
configLoader = new ConfigLoader({});
5457
const result = await configLoader.loadFromHttp({
58+
type: 'http',
59+
enabled: true,
5560
url: 'http://config-service/config',
5661
headers: {},
5762
});
@@ -64,6 +69,8 @@ describe('ConfigLoader', () => {
6469

6570
configLoader = new ConfigLoader({});
6671
await configLoader.loadFromHttp({
72+
type: 'http',
73+
enabled: true,
6774
url: 'http://config-service/config',
6875
auth: {
6976
type: 'bearer',
@@ -81,7 +88,7 @@ describe('ConfigLoader', () => {
8188

8289
describe('reloadConfiguration', () => {
8390
it('should emit configurationChanged event when config changes', async () => {
84-
const initialConfig = {
91+
const initialConfig: Configuration = {
8592
configurationSources: {
8693
enabled: true,
8794
sources: [
@@ -91,6 +98,7 @@ describe('ConfigLoader', () => {
9198
path: tempConfigFile,
9299
},
93100
],
101+
reloadIntervalSeconds: 0,
94102
},
95103
};
96104

@@ -115,7 +123,7 @@ describe('ConfigLoader', () => {
115123
proxyUrl: 'https://test.com',
116124
};
117125

118-
const config = {
126+
const config: Configuration = {
119127
configurationSources: {
120128
enabled: true,
121129
sources: [
@@ -125,6 +133,7 @@ describe('ConfigLoader', () => {
125133
path: tempConfigFile,
126134
},
127135
],
136+
reloadIntervalSeconds: 0,
128137
},
129138
};
130139

@@ -169,15 +178,15 @@ describe('ConfigLoader', () => {
169178
await configLoader.initialize();
170179

171180
// Check if directory exists
172-
expect(fs.existsSync(configLoader.cacheDir)).to.be.true;
181+
expect(fs.existsSync(configLoader.cacheDir!)).to.be.true;
173182
});
174183
});
175184

176185
describe('loadRemoteConfig', () => {
177-
let configLoader;
186+
let configLoader: ConfigLoader;
178187
beforeEach(async () => {
179188
const configFilePath = path.join(__dirname, '..', 'proxy.config.json');
180-
const config = JSON.parse(fs.readFileSync(configFilePath));
189+
const config = JSON.parse(fs.readFileSync(configFilePath, 'utf-8')) as Configuration;
181190

182191
config.configurationSources.enabled = true;
183192
configLoader = new ConfigLoader(config);
@@ -189,10 +198,11 @@ describe('ConfigLoader', () => {
189198
this.timeout(10000);
190199

191200
const source = {
192-
type: 'git',
201+
type: 'git' as const,
193202
repository: 'https://github.com/finos/git-proxy.git',
194203
path: 'proxy.config.json',
195204
branch: 'main',
205+
enabled: true,
196206
};
197207

198208
const config = await configLoader.loadFromGit(source);
@@ -205,10 +215,11 @@ describe('ConfigLoader', () => {
205215

206216
it('should throw error for invalid configuration file path', async function () {
207217
const source = {
208-
type: 'git',
218+
type: 'git' as const,
209219
repository: 'https://github.com/finos/git-proxy.git',
210220
path: '\0', // Invalid path
211221
branch: 'main',
222+
enabled: true,
212223
};
213224

214225
try {
@@ -224,8 +235,9 @@ describe('ConfigLoader', () => {
224235
this.timeout(10000);
225236

226237
const source = {
227-
type: 'http',
238+
type: 'http' as const,
228239
url: 'https://raw.githubusercontent.com/finos/git-proxy/refs/heads/main/proxy.config.json',
240+
enabled: true,
229241
};
230242

231243
const config = await configLoader.loadFromHttp(source);
@@ -238,7 +250,7 @@ describe('ConfigLoader', () => {
238250
});
239251

240252
describe('deepMerge', () => {
241-
let configLoader;
253+
let configLoader: ConfigLoader;
242254

243255
beforeEach(() => {
244256
configLoader = new ConfigLoader({});
@@ -401,7 +413,7 @@ describe('Validation Helpers', () => {
401413
expect(isValidBranchName('fix_123')).to.be.true;
402414
expect(isValidBranchName('user/feature/branch')).to.be.true;
403415

404-
// // Invalid branch names
416+
// Invalid branch names
405417
expect(isValidBranchName('.invalid')).to.be.false;
406418
expect(isValidBranchName('-invalid')).to.be.false;
407419
expect(isValidBranchName('branch with spaces')).to.be.false;

0 commit comments

Comments
 (0)