Skip to content

Commit

Permalink
ch: Setup CI workflow for testing, coverage reporting, building and l…
Browse files Browse the repository at this point in the history
…inting
  • Loading branch information
aimedivin committed Apr 28, 2024
1 parent ecf4597 commit a87ae10
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 63 deletions.
29 changes: 16 additions & 13 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@ module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
"@typescript-eslint/no-unused-vars": [
"warn",
'@typescript-eslint/no-unused-vars': [
'warn',
{
"args": "all",
"argsIgnorePattern": "^_",
"caughtErrors": "all",
"caughtErrorsIgnorePattern": "^_",
"destructuredArrayIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
args: 'all',
argsIgnorePattern: '^_',
caughtErrors: 'all',
caughtErrorsIgnorePattern: '^_',
destructuredArrayIgnorePattern: '^_',
varsIgnorePattern: '^_',
ignoreRestSiblings: true,
},
],
'no-undef': 'off',
'semi': ['warn', 'always'],
'no-multi-spaces': 'warn',
'no-trailing-spaces': 'warn',
'space-before-function-paren': ['warn', 'always'],
'func-style': ['warn', 'declaration', { 'allowArrowFunctions': true }],
'func-style': ['warn', 'declaration', { allowArrowFunctions: true }],
'camelcase': 'warn',
'@typescript-eslint/explicit-function-return-type': ['warn', { allowExpressions: true }],
'@typescript-eslint/explicit-member-accessibility': ['off', { accessibility: 'explicit' }],
'no-unused-vars': 'warn',
'no-extra-semi': 'warn',
},
};
};
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
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 }}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ functionalities for the frontend, such as storing, retrieving, deleting data and
List of endpoints exposed by the service

## Setup

- to use loggers in program use below functions

```bash
logger.error('This is an error message');
logger.warn('This is a warning message');
Expand Down
32 changes: 16 additions & 16 deletions jest.config.ts
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
],
};
12 changes: 5 additions & 7 deletions src/__test__/route.test.ts
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);
});
});
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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();

Expand Down
37 changes: 16 additions & 21 deletions src/middlewares/errorHandler.ts
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) => {

Check warning on line 15 in src/middlewares/errorHandler.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 15 in src/middlewares/errorHandler.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function

Check warning on line 15 in src/middlewares/errorHandler.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 15 in src/middlewares/errorHandler.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function
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 };

0 comments on commit a87ae10

Please sign in to comment.