@bbc/spartacus
Version:
177 lines (148 loc) • 5.27 kB
JavaScript
;
/* eslint-disable global-require */
var fs = require('fs');
var path = require('path');
var winston = void 0;
describe('Logger node - for the server', function () {
afterEach(function () {
jest.clearAllMocks();
});
var deleteFile = function deleteFile(logPath, logFile) {
fs.unlink(path.join(logPath, logFile), function () {});
};
var deleteFolder = function deleteFolder(logPath) {
if (fs.existsSync(logPath)) {
deleteFile(logPath, 'app.log');
fs.rmdir(logPath, function (err) {
if (err) throw err;
});
}
};
describe('Setting up logger', function () {
beforeEach(function () {
jest.resetModules();
});
describe('Folder creation', function () {
var logPath = path.join(__dirname, '../../..', 'log-temp');
var defaultLogPath = path.join(__dirname, '../../..', 'log');
beforeEach(function () {
process.env.LOG_DIR = logPath;
deleteFolder(logPath);
deleteFolder(defaultLogPath);
jest.resetModules();
jest.resetAllMocks();
});
afterAll(function () {
deleteFolder(logPath);
deleteFolder(defaultLogPath);
});
it('creates folder log-temp', function () {
require('./logger.node');
expect(fs.existsSync(logPath)).toBe(true);
});
it('creates default folder log when LOG_DIR isnt set', function () {
delete process.env.LOG_DIR;
require('./logger.node');
expect(fs.existsSync(defaultLogPath)).toBe(true);
});
it('does not create folder log-temp when it already exists', function () {
fs.mkdirSync(logPath);
jest.spyOn(fs, 'mkdirSync');
require('./logger.node');
expect(fs.mkdirSync).not.toHaveBeenCalled();
});
});
describe('Configuring Winston', function () {
beforeEach(function () {
jest.mock('winston', function () {
return jest.fn();
});
winston = require('winston');
winston.format = jest.fn();
winston.createLogger = jest.fn();
winston.format.combine = jest.fn();
winston.format.label = jest.fn();
winston.format.printf = jest.fn();
winston.format.simple = jest.fn();
winston.format.timestamp = jest.fn();
winston.transports = jest.fn();
winston.transports.File = jest.fn();
winston.transports.Console = jest.fn();
winston.format.label.mockImplementation(function () {
return 'Label Mock';
});
winston.format.simple.mockImplementation(function () {
return 'Simple Mock';
});
winston.format.timestamp.mockImplementation(function () {
return 'Timestamp Mock';
});
winston.format.printf.mockImplementation(function () {
return 'Printf Mock';
});
winston.format.combine.mockImplementation(function () {
return 'Combine Mock';
});
});
it('sets up file transport when LOG_DIR is set ', function () {
process.env.LOG_DIR = 'foobarDir';
require('./logger.node');
expect(winston.transports.File).toHaveBeenCalledWith({
filename: 'foobarDir/app.log',
handleExceptions: true,
humanReadableUnhandledException: true,
json: true,
level: 'info',
maxFiles: 1,
maxsize: 104857600
});
});
it('sets up file transport when LOG_DIR isnt set', function () {
delete process.env.LOG_DIR;
require('./logger.node');
expect(winston.transports.File).toHaveBeenCalledWith({
filename: 'log/app.log',
handleExceptions: true,
humanReadableUnhandledException: true,
json: true,
level: 'info',
maxFiles: 1,
maxsize: 104857600
});
});
it('sets up console transport', function () {
process.env.LOG_DIR = 'foobarDir';
require('./logger.node');
expect(winston.transports.Console).toHaveBeenCalledWith({
handleExceptions: true,
humanReadableUnhandledException: true,
level: 'info',
timestamp: true
});
});
it('calls printf with a function', function () {
process.env.LOG_DIR = 'foobarDir';
require('./logger.node');
expect(winston.format.printf).toHaveBeenCalledWith(expect.any(Function));
});
describe('createLogger', function () {
it('is configured correctly', function () {
var loggerNode = require('./logger.node');
loggerNode('path/file/foo.js');
expect(winston.format.combine).toHaveBeenCalledWith('Label Mock', 'Simple Mock', 'Timestamp Mock', 'Printf Mock');
expect(winston.format.label).toHaveBeenCalledWith({
label: 'file/foo.js'
});
expect(winston.format.simple).toHaveBeenCalled();
expect(winston.format.timestamp).toHaveBeenCalledWith({
format: 'YYYY-MM-DD HH:mm:ss'
});
expect(winston.createLogger).toHaveBeenCalledWith({
format: 'Combine Mock',
transports: [{}, {}]
});
});
});
});
});
});