test(configure): testing the value returned by configure(...).

This commit is contained in:
Ilya Denisov 2018-02-12 21:07:03 +02:00
parent 2bc9a5483f
commit 9f5b7be2e6

View File

@ -3,54 +3,79 @@
const test = require('tap').test;
const sandbox = require('sandboxed-module');
test('log4js configure', (batch) => {
batch.test('when configuration file loaded via LOG4JS_CONFIG env variable', (t) => {
process.env.LOG4JS_CONFIG = 'some/path/to/mylog4js.json';
let fileRead = 0;
const modulePath = 'some/path/to/mylog4js.json';
const pathsChecked = [];
const mtime = new Date();
const modulePath = 'some/path/to/mylog4js.json';
const pathsChecked = [];
const fakeFS = {
let fakeFS = {};
let dependencies;
let fileRead;
test('log4js configure', (batch) => {
batch.beforeEach((done) => {
fileRead = 0;
fakeFS = {
config: {
appenders: {
console: { type: 'console', layout: { type: 'messagePassThrough' } }
console: {
type: 'console',
layout: { type: 'messagePassThrough' }
}
},
categories: { default: { appenders: ['console'], level: 'INFO' } }
categories: {
default: {
appenders: ['console'],
level: 'INFO'
}
}
},
readdirSync: function (dir) {
return require('fs').readdirSync(dir);
},
readFileSync: function (file, encoding) {
readdirSync: dir => require('fs').readdirSync(dir),
readFileSync: (file, encoding) => {
fileRead += 1;
t.type(file, 'string');
t.equal(file, modulePath);
t.equal(encoding, 'utf8');
batch.type(file, 'string');
batch.equal(file, modulePath);
batch.equal(encoding, 'utf8');
return JSON.stringify(fakeFS.config);
},
statSync: function (path) {
statSync: (path) => {
pathsChecked.push(path);
if (path === modulePath) {
return { mtime: mtime };
return { mtime: new Date() };
}
throw new Error('no such file');
}
};
const log4js = sandbox.require(
'../../lib/log4js',
{
requires: {
fs: fakeFS,
}
dependencies = {
requires: {
fs: fakeFS
}
);
};
done();
});
batch.test('when configuration file loaded via LOG4JS_CONFIG env variable', (t) => {
process.env.LOG4JS_CONFIG = 'some/path/to/mylog4js.json';
const log4js = sandbox.require('../../lib/log4js', dependencies);
log4js.getLogger('test-logger');
t.equal(fileRead, 1, 'should load the specified local config file');
delete process.env.LOG4JS_CONFIG;
t.equal(fileRead, 1, 'should load the specified local config file');
t.end();
});
batch.test('when configuration is set via configure() method call, return the log4js object', (t) => {
const log4js = sandbox.require('../../lib/log4js', dependencies).configure(fakeFS.config);
t.type(log4js, 'object', 'Configure method call should return the log4js object!');
const log = log4js.getLogger('daemon');
t.type(log, 'object', 'log4js object, returned by configure(...) method should be able to create log object.');
t.type(log.info, 'function');
t.end();
});