Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
myrotvorets-team committed Sep 25, 2023
1 parent 5c81077 commit 922203e
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 87 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"sourceType": "module"
},
"extends": [
"@myrotvorets/myrotvorets-ts"
"@myrotvorets/myrotvorets-ts",
"plugin:mocha/recommended"
],
"env": {
"es2022": true,
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ USER nobody:nobody
COPY --chown=nobody:nobody ./package.json ./package-lock.json ./tsconfig.json .npmrc* ./
RUN \
npm r --package-lock-only \
@myrotvorets/eslint-config-myrotvorets-ts eslint-formatter-gha \
@myrotvorets/eslint-config-myrotvorets-ts eslint-formatter-gha eslint-plugin-mocha \
mocha @types/mocha chai @types/chai chai-as-promised @types/chai-as-promised supertest @types/supertest testdouble c8 mocha-multi mocha-reporter-gha mocha-reporter-sonarqube \
ts-node nodemon && \
npm ci --ignore-scripts --userconfig .npmrc.local && \
Expand Down
5 changes: 3 additions & 2 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"watch": ["src"],
"ext": "mts",
"exec": "rm -rf dist && ts-node-script --transpile-only ./src/index.mts"
"ext": "mts,yaml",
"exec": "rm -rf dist && ts-node-script --transpile-only ./src/index.mts",
"signal": "SIGTERM"
}
71 changes: 55 additions & 16 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"dependencies": {
"@cloudnative/health-connect": "^2.1.0",
"@myrotvorets/clean-up-after-multer": "^1.1.6",
"@myrotvorets/create-server": "^2.1.1",
"@myrotvorets/create-server": "^2.2.0",
"@myrotvorets/envalidators": "^2.1.0",
"@myrotvorets/express-async-middleware-wrapper": "^2.1.0",
"@myrotvorets/express-microservice-middlewares": "^1.5.7",
"@myrotvorets/facex": "^2.6.0",
"@myrotvorets/oav-installer": "^4.0.0",
"@myrotvorets/oav-installer": "^4.0.1",
"@myrotvorets/opentelemetry-configurator": "^4.0.1",
"@opentelemetry/instrumentation-express": "^0.33.0",
"envalid": "^8.0.0",
Expand All @@ -45,6 +45,7 @@
"chai": "^4.3.8",
"chai-as-promised": "^7.1.1",
"eslint-formatter-gha": "^1.2.0",
"eslint-plugin-mocha": "^10.2.0",
"mocha": "^10.2.0",
"mocha-multi": "^1.1.7",
"mocha-reporter-gha": "^1.0.2",
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/monitoring.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
import { Router } from 'express';
import { addJsonContentTypeMiddleware } from '@myrotvorets/express-microservice-middlewares';

export const healthChecker = new HealthChecker();
export let healthChecker: HealthChecker | undefined;

export function monitoringController(): Router {
const router = Router();

const shutdownCheck = new ShutdownCheck('SIGTERM', (): Promise<void> => Promise.resolve());

healthChecker = new HealthChecker();
healthChecker.registerShutdownCheck(shutdownCheck);

router.use(addJsonContentTypeMiddleware);
Expand Down
44 changes: 30 additions & 14 deletions test/functional/controllers/monitoring.test.mts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { afterEach, before, beforeEach } from 'mocha';
import express, { type Express } from 'express';
import request from 'supertest';
import { healthChecker, monitoringController } from '../../../src/controllers/monitoring.mjs';

describe('MonitoringController', () => {
describe('MonitoringController', function () {
let app: Express;

before(() => {
before(function () {
app = express();
app.disable('x-powered-by');
app.use('/monitoring', monitoringController());
});

beforeEach(() => {
beforeEach(function () {
healthChecker.shutdownRequested = false;
});

afterEach(() => process.removeAllListeners('SIGTERM'));
afterEach(function () {
process.removeAllListeners('SIGTERM');
});

const checker200 = (endpoint: string): Promise<unknown> =>
request(app).get(`/monitoring/${endpoint}`).expect('Content-Type', /json/u).expect(200);
Expand All @@ -26,18 +27,33 @@ describe('MonitoringController', () => {
return request(app).get(`/monitoring/${endpoint}`).expect('Content-Type', /json/u).expect(503);
};

describe('Liveness Check', () => {
it('should succeed', () => checker200('live'));
it('should fail when shutdown requested', () => checker503('live'));
describe('Liveness Check', function () {
it('should succeed', function () {
return checker200('live');
});

it('should fail when shutdown requested', function () {
return checker503('live');
});
});

describe('Readiness Check', () => {
it('should succeed', () => checker200('ready'));
it('should fail when shutdown requested', () => checker503('ready'));
describe('Readiness Check', function () {
it('should succeed', function () {
return checker200('ready');
});

it('should fail when shutdown requested', function () {
return checker503('ready');
});
});

describe('Health Check', () => {
it('should succeed', () => checker200('health'));
it('should fail when shutdown requested', () => checker503('health'));
describe('Health Check', function () {
it('should succeed', function () {
return checker200('health');
});

it('should fail when shutdown requested', function () {
return checker503('health');
});
});
});
27 changes: 15 additions & 12 deletions test/functional/middleware/error.test.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { afterEach, beforeEach, describe, it } from 'mocha';
import { expect } from 'chai';
import express, { type Express, type NextFunction } from 'express';
import request from 'supertest';
Expand All @@ -9,11 +8,15 @@ import { environment } from '../../../src/lib/environment.mjs';
import { UploadError } from '../../../src/lib/uploaderror.mjs';
import { faceXErrorHandlerMiddleware } from '../../../src/middleware/error.mjs';

describe('faceXErrorHandlerMiddleware', () => {
describe('faceXErrorHandlerMiddleware', function () {
let app: Express;
const env = { ...process.env };
let env: typeof process.env;

beforeEach(() => {
before(function () {
env = { ...process.env };
});

beforeEach(function () {
process.env = {
NODE_ENV: 'test',
PORT: '3030',
Expand All @@ -26,7 +29,7 @@ describe('faceXErrorHandlerMiddleware', () => {
app.disable('x-powered-by');
});

afterEach(() => {
afterEach(function () {
process.env = { ...env };
});

Expand All @@ -39,7 +42,7 @@ describe('faceXErrorHandlerMiddleware', () => {

const expectBadGateway = (res: request.Response): unknown => expectError(res, 'BAD_GATEWAY', 502);

it('should ignore non-errors', () => {
it('should ignore non-errors', function () {
app.use('/', (_req, _res, next: NextFunction) => next(123));
app.use(faceXErrorHandlerMiddleware);
app.use(errorMiddleware);
Expand All @@ -49,7 +52,7 @@ describe('faceXErrorHandlerMiddleware', () => {
.expect((res) => expect(res.body).to.deep.equal({}));
});

it('should catch UploadError', () => {
it('should catch UploadError', function () {
app.use('/', (_req, _res, next: NextFunction) => next(new UploadError('message', 'file')));
app.use(faceXErrorHandlerMiddleware);
app.use(errorMiddleware);
Expand All @@ -60,7 +63,7 @@ describe('faceXErrorHandlerMiddleware', () => {
.expect((res) => expectError(res, 'UPLOAD_FAILED', 400));
});

it('should catch FaceX HttpError', () => {
it('should catch FaceX HttpError', function () {
app.use('/', (_req, _res, next: NextFunction) =>
next(new HttpError({ status: 418, statusText: "I'm a teapot" })),
);
Expand All @@ -69,21 +72,21 @@ describe('faceXErrorHandlerMiddleware', () => {
return request(app).get('/').expect(502).expect('Content-Type', /json/u).expect(expectBadGateway);
});

it('should catch FaceX NetworkError', () => {
it('should catch FaceX NetworkError', function () {
app.use('/', (_req, _res, next: NextFunction) => next(new NetworkError('Boom-boom bye-bye')));
app.use(faceXErrorHandlerMiddleware);
app.use(errorMiddleware);
return request(app).get('/').expect(502).expect('Content-Type', /json/u).expect(expectBadGateway);
});

it('should catch FaceX BadResponseError', () => {
it('should catch FaceX BadResponseError', function () {
app.use('/', (_req, _res, next: NextFunction) => next(new BadResponseError('Boom-boom bye-bye')));
app.use(faceXErrorHandlerMiddleware);
app.use(errorMiddleware);
return request(app).get('/').expect(502).expect('Content-Type', /json/u).expect(expectBadGateway);
});

it('should catch BadRequestError', () => {
it('should catch BadRequestError', function () {
app.use('/', (_req, _res, next: NextFunction) => next(new BadRequestError('Boom-boom bye-bye')));
app.use(faceXErrorHandlerMiddleware);
app.use(errorMiddleware);
Expand All @@ -94,7 +97,7 @@ describe('faceXErrorHandlerMiddleware', () => {
.expect((res) => expectError(res, 'BAD_REQUEST', 400));
});

it('should catch FaceXError', () => {
it('should catch FaceXError', function () {
app.use('/', (_req, _res, next: NextFunction) => next(new FaceXError('Boom-boom bye-bye')));
app.use(faceXErrorHandlerMiddleware);
app.use(errorMiddleware);
Expand Down
Loading

0 comments on commit 922203e

Please sign in to comment.