Skip to content

Commit

Permalink
[package.json] Add dependency: pino
Browse files Browse the repository at this point in the history
[server:logger] Create a module `server/logger.ts` to provide a singular
pino logger instance
[server:httpLogger] Create a module `server/httpLogger.ts` to provide a
singular pino-http logger instance
  • Loading branch information
chichiwang committed Apr 16, 2024
1 parent 7c1b53f commit effc896
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 27 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"dependencies": {
"express": "^4.19.2",
"js-cookie": "^3.0.5",
"pino": "^8.20.0",
"pino-http": "^9.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
19 changes: 19 additions & 0 deletions server/__tests__/httpLogger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable global-require */
import pinoHttp from 'pino-http';

jest.mock('pino-http');

const mockLogger = jest.fn();

let httpLogger;

describe('httpLogger', () => {
beforeAll(() => {
pinoHttp.mockReturnValue(mockLogger);
httpLogger = require('../httpLogger').default;
});

test('returns an instance of a pino httpLogger', () => {
expect(httpLogger).toEqual(mockLogger);
});
});
27 changes: 8 additions & 19 deletions server/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* eslint global-require: 0 */
import express from 'express';
import pino from 'pino-http';

import logger from '../logger';
import httpLogger from '../httpLogger';
import appHandler from '../appHandler';

jest.mock('express');
jest.mock('pino-http');

jest.mock('../logger');
jest.mock('../httpLogger');
jest.mock('../appHandler', () => () => 'appHandler');

const mockAppGet = jest.fn();
Expand All @@ -16,34 +20,23 @@ const mockExpressApp = {
use: mockAppUse,
};

const mockLogger = 'Finest logger in the business';

function mockExpressStatic(path) {
return `Static Asset Directory: ${path}`;
}

let mockConsoleLog;

describe('server', () => {
beforeAll(() => {
express.mockReturnValue(mockExpressApp);
express.static.mockImplementation(mockExpressStatic);
pino.mockReturnValue(mockLogger);
});

beforeEach(() => {
mockConsoleLog = jest.spyOn(console, 'log').mockImplementation(() => {});

jest.clearAllMocks();
jest.isolateModules(() => {
require('../index');
});
});

afterEach(() => {
mockConsoleLog.mockRestore();
});

test('express is invoked once', () => {
expect(express).toHaveBeenCalledTimes(1);
});
Expand All @@ -60,16 +53,12 @@ describe('server', () => {
const callback = mockAppListen.mock.calls[0][1];
callback();

expect(mockConsoleLog).toHaveBeenCalledWith(expect.stringContaining('8080'));
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('8080'));
});

describe('server middleware', () => {
test('pino-http is invoked to create a logger', () => {
expect(pino).toHaveBeenCalledTimes(1);
});

test('logger is passed to express as middleware', () => {
expect(mockAppUse).toHaveBeenCalledWith(mockLogger);
expect(mockAppUse).toHaveBeenCalledWith(httpLogger);
});
});

Expand Down
19 changes: 19 additions & 0 deletions server/__tests__/logger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable global-require */
import pino from 'pino';

jest.mock('pino');

const mockLogger = jest.fn();

let logger;

describe('logger', () => {
beforeAll(() => {
pino.mockReturnValue(mockLogger);
logger = require('../logger').default;
});

test('returns an instance of a pino logger', () => {
expect(logger).toEqual(mockLogger);
});
});
5 changes: 5 additions & 0 deletions server/httpLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pinoHttp from 'pino-http';

export const httpLogger = pinoHttp();

export default httpLogger;
8 changes: 4 additions & 4 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import path from 'node:path';
import express, {
Express,
} from 'express';
import pino from 'pino-http';

import logger from './logger';
import httpLogger from './httpLogger';
import appHandler from './appHandler';

const app: Express = express();
const logger = pino();
const port: Number = 8080;

app.use(logger);
app.use(httpLogger);
app.use('/scripts', express.static(path.resolve(__dirname, 'scripts')));
app.use('/styles', express.static(path.resolve(__dirname, 'styles')));

app.get('*', appHandler);

app.listen(port, function serverListening() {
console.log(`[server] Server is listening at http://127.0.0.1:${port}`);
logger.info(`[server] Server is listening at http://127.0.0.1:${port}`);
});
5 changes: 5 additions & 0 deletions server/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pino from 'pino';

const logger = pino();

export default logger;

0 comments on commit effc896

Please sign in to comment.