Skip to content

Commit

Permalink
add test and husky hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gladwindos committed Feb 16, 2024
1 parent e56f901 commit e908f2c
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
jest.config.js
jest.config.js
dist/
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm run lint
2 changes: 2 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
npm run build
npm test
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"main": "index.ts",
"scripts": {
"dev": "nodemon ./src/index.ts",
"build": "tsc",
"start": "node ./dist/index.js",
"test": "jest",
"lint": "eslint ."
"lint": "eslint .",
"prepare": "husky"
},
"author": "",
"license": "ISC",
Expand All @@ -25,6 +28,7 @@
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"http-proxy-middleware": "^2.0.6",
"husky": "^9.0.11",
"jest": "^29.7.0",
"nodemon": "^3.0.3",
"prettier": "3.2.5",
Expand Down
1 change: 0 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import express from "express";
import { parseConfig } from "./utils/config/parse-config";
import { createProxies } from "./utils/proxy/create-proxies";

// TODO: prevent the app from crashing if the config is invalid
const config = parseConfig("config.yml");

const router = express.Router();
Expand Down
96 changes: 96 additions & 0 deletions src/utils/config/validate-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { validateConfig } from "./validate-config";
import { Config } from "../../types/config";

describe("validateConfig", () => {
it("should not throw an error if config is valid", () => {
const validConfig: Config = {
endpoints: [
{
name: "endpoint1",
method: "GET",
path: "/path1",
destination: {
url: "http:https://example.com",
},
},
{
name: "endpoint2",
method: "POST",
path: "/path2",
destination: {
url: "http:https://example.com",
},
},
],
};

expect(() => validateConfig(validConfig)).not.toThrow();
});

it("should throw an error if endpoint names are duplicated", () => {
const invalidConfig: Config = {
endpoints: [
{
name: "duplicate",
method: "GET",
path: "/path1",
destination: {
url: "http:https://example.com",
},
},
{
name: "duplicate",
method: "GET",
path: "/path2",
destination: {
url: "http:https://example.com",
},
},
],
};

expect(() => validateConfig(invalidConfig)).toThrow();
});

it("should throw an error if endpoint path/method combination is duplicated", () => {
const invalidConfig: Config = {
endpoints: [
{
name: "endpoint1",
method: "GET",
path: "/duplicate",
destination: {
url: "http:https://example.com",
},
},
{
name: "endpoint2",
method: "GET",
path: "/duplicate",
destination: {
url: "http:https://example.com",
},
},
],
};

expect(() => validateConfig(invalidConfig)).toThrow();
});

it("should throw an error if destination URL is invalid", () => {
const invalidUrlConfig: Config = {
endpoints: [
{
name: "endpoint",
method: "GET",
path: "/path",
destination: {
url: "invalid_url",
},
},
],
};

expect(() => validateConfig(invalidUrlConfig)).toThrow();
});
});
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"esModuleInterop": true,
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "./dist"
}
}

0 comments on commit e908f2c

Please sign in to comment.