Skip to content

Commit

Permalink
Add some tests related to the logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyrkan committed Jun 16, 2017
1 parent 8c602db commit 5c70b5e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 22 deletions.
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

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

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -52,6 +52,7 @@
"jasmine-spec-reporter": "^4.1.0",
"nyc": "^10.3.2",
"request": "^2.81.0",
"request-promise-native": "^1.0.3"
"request-promise-native": "^1.0.3",
"rimraf": "https://registry.npmjs.org/rimraf/-/rimraf-2.4.5.tgz"
}
}
10 changes: 2 additions & 8 deletions src/utils/Logger.js
Expand Up @@ -21,14 +21,8 @@ function initLogger({Configuration}) {
}

// Create log streams based on the configuration
if (loggerConfig) {
if (Array.isArray(loggerConfig)) {
for (let stream of loggerConfig) {
streams.push(stream);
}
} else {
streams.push(loggerConfig);
}
for (let stream of loggerConfig) {
streams.push(stream);
}

// Create logs directory for each stream if needed
Expand Down
85 changes: 85 additions & 0 deletions test/logger.spec.js
@@ -0,0 +1,85 @@
'use strict';

const bunyan = require('bunyan');
const fs = require('fs');
const rimraf = require('rimraf');
const Mockiji = require('../src/index');

describe("Logger", function() {
beforeEach(function() {
// Helper that creates a new instance of Mockiji and return its
// logger's streams
this.getCreatedStreams = (logs, silent, env = 'dev') => {
let streams = [];
let configuration = {
env,
middlewares: [
// A bit of a hack to retrieve the current logger
({logger}) => {
streams = logger.streams;
return (req, res, next) => {
next();
};
}
],
};

if (typeof logs !== 'undefined') {
configuration.logs = logs;
}

if (typeof silent !== 'undefined') {
configuration.silent = silent;
}

// eslint-disable-next-line no-new
new Mockiji({ configuration });

return streams;
}
});

it('should have two default streams (stdout + rotating-file)', function() {
const streams = this.getCreatedStreams();
expect(streams.length).toBe(2);
});

it('should be possible to disable all the streams', function() {
const streams = this.getCreatedStreams([], true);
expect(streams.length).toBe(0);
});

it('should be possible to add a stream without the default ones', function() {
const noopStream = { write: () => { }}; // eslint-disable-line no-empty-function
const stream = { name: 'stream1', stream: noopStream, level: 'info' };
const streams = this.getCreatedStreams([stream], true);
expect(streams.length).toBe(1);
});

it('should be possible to add multiple streams to the default stdout one', function() {
const noopStream = { write: () => { }}; // eslint-disable-line no-empty-function
const stream1 = { name: 'stream1', stream: noopStream, level: 'info' };
const stream2 = { name: 'stream2', stream: noopStream, level: 'info' };
const streams = this.getCreatedStreams([stream1, stream2]);
expect(streams.length).toBe(3);
});

it('should create logs folders if needed', function(done) {
const randomFolder = `/tmp/mockiji-${new Date().getTime()}-${Math.round(Math.random()*1000)}`;
this.getCreatedStreams([{ path: `${randomFolder}/mockiji.log` }]);
expect(fs.existsSync(randomFolder)).toBe(true);
rimraf(randomFolder, done);
});

it('should use the "debug" level for the default stdout stream in "dev" environment', function() {
let streams = this.getCreatedStreams([], false, 'dev');
expect(streams.length).toBe(1);
expect(streams[0].level).toBe(bunyan.DEBUG);
});

it('should use the "info" level for the default stdout stream in "prod" environment', function() {
let streams = this.getCreatedStreams([], false, 'prod');
expect(streams.length).toBe(1);
expect(streams[0].level).toBe(bunyan.INFO);
});
});
20 changes: 8 additions & 12 deletions yarn.lock
Expand Up @@ -824,10 +824,6 @@ esprima@^2.6.0:
version "2.7.3"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"

esprima@^3.1.1:
version "3.1.3"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633"

esprima@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.0.0.tgz#53cf247acda77313e551c3aa2e73342d3fb4f7d9"
Expand Down Expand Up @@ -1475,20 +1471,13 @@ js-tokens@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"

js-yaml@3.6.1:
js-yaml@3.6.1, js-yaml@^3.5.1:
version "3.6.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.6.1.tgz#6e5fe67d8b205ce4d22fad05b7781e8dadcc4b30"
dependencies:
argparse "^1.0.7"
esprima "^2.6.0"

js-yaml@^3.5.1:
version "3.8.3"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.3.tgz#33a05ec481c850c8875929166fe1beb61c728766"
dependencies:
argparse "^1.0.7"
esprima "^3.1.1"

jsbn@~0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
Expand Down Expand Up @@ -2228,6 +2217,13 @@ rimraf@^2.2.8, rimraf@^2.3.3, rimraf@^2.5.4, rimraf@^2.6.1:
dependencies:
glob "^7.0.5"

"rimraf@https://registry.npmjs.org/rimraf/-/rimraf-2.4.5.tgz":
version "2.4.5"
uid ee710ce5d93a8fdb856fb5ea8ff0e2d75934b2da
resolved "https://registry.npmjs.org/rimraf/-/rimraf-2.4.5.tgz#ee710ce5d93a8fdb856fb5ea8ff0e2d75934b2da"
dependencies:
glob "^6.0.1"

rimraf@~2.4.0:
version "2.4.5"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.4.5.tgz#ee710ce5d93a8fdb856fb5ea8ff0e2d75934b2da"
Expand Down

0 comments on commit 5c70b5e

Please sign in to comment.