Skip to content

Commit

Permalink
Merge pull request #22 from santoshshinde2012/dev
Browse files Browse the repository at this point in the history
updates test cases
  • Loading branch information
santoshshinde2012 authored May 25, 2024
2 parents 824812d + 597a7b2 commit 0101a5f
Show file tree
Hide file tree
Showing 12 changed files with 744 additions and 548 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ The swagger documentation is available at the following url `${host}/docs`:

[http://localhost:8080/docs](http://localhost:8080/docs)

## Wiki

- [NodeSeQ](https://github.com/santoshshinde2012/node-ts-sequelize-pg-boilerplate/wiki)


## Refrences
- [How to Use Sequelize with TypeScript, Node.js, and PostgreSQL](https://blog.santoshshinde.com/how-to-use-sequelize-with-typescript-node-js-and-postgresql-c6ff58a3af76)
Expand All @@ -128,6 +132,7 @@ The swagger documentation is available at the following url `${host}/docs`:
- [Testing with Jest in TypeScript and Node.js for Beginners](https://blog.santoshshinde.com/beginners-guide-to-testing-jest-with-node-typescript-1f46a1b87dad)
- [Static Code Analysis for Node.js and TypeScript Project using SonarQube](https://blog.santoshshinde.com/static-code-analysis-for-node-js-and-typescript-project-using-sonarqube-8f90799add06)
- [Visualization of Node.js Event Emitter](https://blog.santoshshinde.com/visualization-of-node-js-event-emitter-4f7c9fe3a477)


<hr/>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "tsc -p . && cp package.json ./build/package.json && cp package-lock.json ./build/package-lock.json && cp .env ./build/.env",
"lint": "eslint src/**/*.ts",
"lint-fix": "eslint --fix src/**/*.ts",
"format": "prettier --write \"src/**/*.ts\"",
"format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
"prepare": "husky",
"test:custom": "npm test -- tests/integration-tests",
"precommit": "npm run lint-fix && npm run format",
Expand Down
32 changes: 20 additions & 12 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Router } from 'express';
import EnquiryController from './components/enquiry/EnquiryController';
import SystemStatusController from './components/system-status/SystemStatusController';
import { RouteDefinition } from './types/RouteDefinition';
import logger from './lib/logger';

/**
*
Expand Down Expand Up @@ -44,19 +45,26 @@ function registerControllerRoutes(routes: RouteDefinition[]): Router {
*
*/
export default function registerRoutes(): Router {
const router = Router();
try {
const router = Router();

// Define an array of controller objects
const controllers = [new SystemStatusController(), new EnquiryController()];
// Define an array of controller objects
const controllers = [
new SystemStatusController(),
new EnquiryController(),
];

// Dynamically register routes for each controller
controllers.forEach((controller) => {
// make sure each controller has basePath attribute and routes() method
router.use(
`/v1/${controller.basePath}`,
registerControllerRoutes(controller.routes()),
);
});
// Dynamically register routes for each controller
controllers.forEach((controller) => {
// make sure each controller has basePath attribute and routes() method
router.use(
`/v1/${controller.basePath}`,
registerControllerRoutes(controller.routes()),
);
});

return router;
return router;
} catch (error) {
logger.error('Unable to register the routes:', error);
}
}
37 changes: 16 additions & 21 deletions tests/helpers/Integration-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ import * as express from 'express';
import App from '../../src/App';
import logger from '../../src/lib/logger';


export default class IntegrationHelpers {

private static appInstance: express.Application;

public static async getApp(): Promise<express.Application> {
if (this.appInstance) {
return this.appInstance;
}
const app: App = new App();
await app.init();
this.appInstance = app.express;

return this.appInstance;
}

public clearDatabase(): void {
logger.info('clear the database');
}

private static appInstance: express.Application;

public static async getApp(): Promise<express.Application> {
if (this.appInstance) {
return this.appInstance;
}
const app: App = new App();
await app.init();
this.appInstance = app.express;

return this.appInstance;
}

public clearDatabase(): void {
logger.info('clear the database');
}
}


45 changes: 21 additions & 24 deletions tests/integration-tests/app.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
import 'jest';
import express from 'express';
import request from 'supertest';
import {
StatusCodes,
} from 'http-status-codes';
import { StatusCodes } from 'http-status-codes';
import IntegrationHelpers from '../helpers/Integration-helpers';

describe('status integration tests', () => {
let app: express.Application;

beforeAll(async() => {
app = await IntegrationHelpers.getApp();
});
let app: express.Application;

beforeAll(async () => {
app = await IntegrationHelpers.getApp();
});

it('can get default route success', async () => {
const response = await request(app)
.get('/')
.set('Accept', 'application/json')
.expect('Content-Type', 'application/json; charset=utf-8');
it('can get default route success', async () => {
const response = await request(app)
.get('/')
.set('Accept', 'application/json')
.expect('Content-Type', 'application/json; charset=utf-8');

const { status } = response;
expect(status).toBe(StatusCodes.OK);
});
const { status } = response;
expect(status).toBe(StatusCodes.OK);
});

it('can get default web route success', async () => {
const response = await request(app)
.get('/web')
.set('Accept', 'application/json')
.expect('Content-Type', 'application/json; charset=utf-8');
it('can get default web route success', async () => {
const response = await request(app)
.get('/web')
.set('Accept', 'application/json')
.expect('Content-Type', 'application/json; charset=utf-8');

const { status } = response;
expect(status).toBe(StatusCodes.OK);
});
const { status } = response;
expect(status).toBe(StatusCodes.OK);
});
});
56 changes: 56 additions & 0 deletions tests/unit-tests/App.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import database from '../../src/database';
import App from '../../src/App';
import logger from '../../src/lib/logger';

jest.mock('../../src/database', () => ({
authenticate: jest.fn(),
sync: jest.fn(),
}));

jest.mock('../../src/routes', () => ({
registerRoutes: jest.fn(),
}));

jest.mock('../../src/lib/logger', () => ({
info: jest.fn(),
error: jest.fn(),
}));

describe('App', () => {
let app: App;

beforeEach(() => {
app = new App();
});

describe('assertDatabaseConnection', () => {
it('should log success message when database connection is established', async () => {
(database.authenticate as jest.Mock).mockResolvedValueOnce(
undefined,
);
(database.sync as jest.Mock).mockResolvedValueOnce(undefined);

await app['assertDatabaseConnection']();

expect(database.authenticate).toHaveBeenCalled();
expect(database.sync).toHaveBeenCalled();
expect(logger.info).toHaveBeenCalledWith(
'Connection has been established successfully.',
);
expect(logger.error).not.toHaveBeenCalled();
});

it('should log error message when database connection fails', async () => {
const error = new Error('Connection failed');
(database.authenticate as jest.Mock).mockRejectedValueOnce(error);

await app['assertDatabaseConnection']();

expect(database.authenticate).toHaveBeenCalled();
expect(logger.error).toHaveBeenCalledWith(
'Unable to connect to the database:',
error,
);
});
});
});
Loading

0 comments on commit 0101a5f

Please sign in to comment.