-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ch: Setup CI workflow for testing, coverage reporting, building and l…
…inting
- Loading branch information
Showing
9 changed files
with
122 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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: 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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { | ||
server.close(done); | ||
}); | ||
|
||
it('responds with "Knights Ecommerce API"', done => { | ||
request(app) | ||
.get('/') | ||
.expect(200, 'Knights Ecommerce API', done); | ||
request(app).get('/').expect(200, 'Knights Ecommerce API', done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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) => { | ||
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) => { | ||
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
status: string; | ||
|
||
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) { | ||
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters