-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
…inting
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: knights-ecomm-be CI | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
build-lint-test-coverage: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Use Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20.x' | ||
|
||
- name: Install dependencies | ||
run: npm install | ||
|
||
- name: Run ESLint and Prettier | ||
run: npm run lint | ||
|
||
- name: Build project | ||
run: npm run build --if-present | ||
|
||
- name: Run tests | ||
run: npm test | ||
|
||
- name: Upload coverage report to Coveralls | ||
uses: coverallsapp/[email protected] | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
export default { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
testMatch: ["**/**/*.test.ts"], | ||
verbose: true, | ||
forceExit: true, | ||
clearMocks: true, | ||
resetMocks: true, | ||
restoreMocks: true, | ||
collectCoverageFrom: [ | ||
"src/**/*.{ts,tsx}", // Include all JavaScript/JSX files in the src directory | ||
], | ||
coveragePathIgnorePatterns: [ | ||
"/node_modules/", // Exclude the node_modules directory | ||
"/__tests__/", // Exclude the tests directory | ||
], | ||
}; | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testMatch: ['**/**/*.test.ts'], | ||
verbose: true, | ||
forceExit: true, | ||
clearMocks: true, | ||
resetMocks: true, | ||
restoreMocks: true, | ||
collectCoverageFrom: [ | ||
'src/**/*.{ts,tsx}', // Include all JavaScript/JSX files in the src directory | ||
], | ||
coveragePathIgnorePatterns: [ | ||
'/node_modules/', // Exclude the node_modules directory | ||
'/__tests__/', // Exclude the tests directory | ||
], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import request from 'supertest'; | ||
import {app, server} from '../index'; // update this with the path to your app file | ||
import { app, server } from '../index'; // update this with the path to your app file | ||
|
||
describe('GET /', () => { | ||
afterAll(done => { | ||
server.close(done); | ||
}); | ||
afterAll(done => { | ||
Check failure on line 5 in src/__test__/route.test.ts GitHub Actions / build-lint-test-coverage
|
||
server.close(done); | ||
}); | ||
|
||
it('responds with "Knights Ecommerce API"', done => { | ||
Check warning on line 9 in src/__test__/route.test.ts GitHub Actions / build-lint-test-coverage
Check failure on line 9 in src/__test__/route.test.ts GitHub Actions / build-lint-test-coverage
Check warning on line 9 in src/__test__/route.test.ts GitHub Actions / build-lint-test-coverage
|
||
request(app) | ||
.get('/') | ||
.expect(200, 'Knights Ecommerce API', done); | ||
request(app).get('/').expect(200, 'Knights Ecommerce API', done); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
import express, { Request, Response } from "express"; | ||
import cors from "cors"; | ||
import dotenv from "dotenv"; | ||
import router from "./routes"; | ||
import { addDocumentation } from "./startups/docs"; | ||
import express, { Request, Response } from 'express'; | ||
import cors from 'cors'; | ||
import dotenv from 'dotenv'; | ||
import router from './routes'; | ||
import { addDocumentation } from './startups/docs'; | ||
|
||
import {CustomError,errorHandler} from "./middlewares/errorHandler"; | ||
import { CustomError, errorHandler } from './middlewares/errorHandler'; | ||
import morgan from 'morgan'; | ||
dotenv.config(); | ||
|
||
export const app = express(); | ||
const port = process.env.PORT as string; | ||
Check warning on line 12 in src/index.ts GitHub Actions / build-lint-test-coverage
|
||
app.use(express.json()); | ||
|
||
app.use(cors({ origin: "*" })); | ||
app.use(cors({ origin: '*' })); | ||
|
||
app.all('*', (req: Request,res: Response,next) =>{ | ||
const error = new CustomError(`Can't find ${req.originalUrl} on the server!`,404); | ||
error.status = 'fail'; | ||
next(error); | ||
app.get('/', (req: Request, res: Response) => { | ||
Check warning on line 17 in src/index.ts GitHub Actions / build-lint-test-coverage
|
||
res.send('Knights Ecommerce API'); | ||
}); | ||
|
||
addDocumentation(app); | ||
app.get("/api/v1", (req: Request, res: Response) => { | ||
res.send("Knights Ecommerce API"); | ||
app.all('*', (req: Request, res: Response, next) => { | ||
Check warning on line 21 in src/index.ts GitHub Actions / build-lint-test-coverage
|
||
const error = new CustomError(`Can't find ${req.originalUrl} on the server!`, 404); | ||
error.status = 'fail'; | ||
next(error); | ||
}); | ||
|
||
addDocumentation(app); | ||
|
||
app.use(router); | ||
app.use(errorHandler); | ||
|
||
//morgan | ||
const morganFormat = ':method :url :status :response-time ms - :res[content-length]'; | ||
app.use(morgan(morganFormat)); | ||
|
||
|
||
|
||
app.listen(port, () => { | ||
console.log(`[server]: Server is running at http://localhost:${port}`); | ||
export const server = app.listen(port, () => { | ||
console.log(`[server]: Server is running at http://localhost:${port}`); | ||
Check warning on line 37 in src/index.ts GitHub Actions / build-lint-test-coverage
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,25 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
class CustomError extends Error { | ||
statusCode: number; | ||
status: string; | ||
statusCode: number; | ||
Check warning on line 4 in src/middlewares/errorHandler.ts GitHub Actions / build-lint-test-coverage
|
||
status: string; | ||
Check warning on line 5 in src/middlewares/errorHandler.ts GitHub Actions / build-lint-test-coverage
|
||
|
||
constructor (message: string, statusCode: number) { | ||
super(message); | ||
this.statusCode = statusCode; | ||
this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error'; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
constructor (message: string, statusCode: number) { | ||
Check warning on line 7 in src/middlewares/errorHandler.ts GitHub Actions / build-lint-test-coverage
|
||
super(message); | ||
this.statusCode = statusCode; | ||
this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error'; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
|
||
const errorHandler = ( | ||
err: CustomError, | ||
req: Request, | ||
res: Response, | ||
|
||
) => { | ||
err.statusCode = err.statusCode || 500; | ||
err.status = err.status || 'error'; | ||
res.status(err.statusCode).json({ | ||
status: err.statusCode, | ||
message: err.message | ||
}); | ||
console.error(err.stack); | ||
const errorHandler = (err: CustomError, req: Request, res: Response) => { | ||
err.statusCode = err.statusCode || 500; | ||
err.status = err.status || 'error'; | ||
res.status(err.statusCode).json({ | ||
status: err.statusCode, | ||
message: err.message, | ||
}); | ||
console.error(err.stack); | ||
}; | ||
|
||
export { CustomError, errorHandler }; |