Skip to content

Commit

Permalink
test(log): initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reel committed Jul 12, 2017
1 parent a49b1b4 commit 4f1a0b6
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/config/tests/config/test.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"log": "test.log",
"abc": "c",
"location": {
"controllers": "./packages/controller/tests/controllers"
Expand Down
8 changes: 6 additions & 2 deletions packages/log/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ log.add(winston.transports.Console, {
});

if (config.has('log') && typeof config.get('log') === 'string') {
// eslint-disable-next-line no-console
console.log('');
log.info(`logger initialized. also logging to ${config.get('log')}`);
log.add(winston.transports.File, { filename: `logs/${config.get('log')}` });
log.add(winston.transports.File, {
filename: path.resolve(process.cwd(), 'logs', `${config.get('log')}`),
});
} else {
log.warn('no file set in configuration file: logging to console only');
}
Expand All @@ -60,7 +63,7 @@ function getColor(level) {

function notify(title = 'No title', message = 'No message') {
if (process.env.NODE_ENV !== 'production') {
notifier.notify({
return notifier.notify({
title,
message,
icon: path.join(__dirname, 'henri.png'),
Expand All @@ -82,4 +85,5 @@ log.fatalError = msg => {

// We don't use addModule as it is not yet registered
henri.log = log;
henri.log.getColor = getColor;
henri.notify = notify;
1 change: 1 addition & 0 deletions packages/log/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"winston": "^2.3.1"
},
"devDependencies": {
"@types/jest": "^20.0.2",
"@types/node": "^8.0.10",
"@types/winston": "^2.3.3"
}
Expand Down
55 changes: 55 additions & 0 deletions packages/log/tests/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const path = require('path');
const fs = require('fs');

describe('log', () => {
beforeEach(() => {
process.env.ALLOW_CONFIG_MUTATIONS = true;
const logsDirecory = path.resolve(process.cwd(), 'logs');
if (!fs.existsSync(logsDirecory)) {
fs.mkdirSync(logsDirecory);
}
require('../../config/index.js');
require('../index.js');
});
test('initialize', () => {
expect(henri.log).toBeDefined();
});
test('log to console', () => {
expect(henri.log.transports.console).toBeDefined();
});
test('should log to a file', () => {
const filename = path.resolve(
process.cwd(),
'logs',
`${henri.config.get('log')}`
);
expect(fs.existsSync(filename)).toBe(true);
expect(henri.log.transports.file).toBeDefined();
});
test('notify', () => {
expect(henri.notify).toBeDefined();
expect(() =>
henri.notify('henri framework', 'seems like notification works')
).not.toThrow();
});
test('fatalError', () => {
// eslint-disable-next-line no-console
console.log = jest.fn();
henri.log.error = jest.fn();
expect(() => henri.log.fatalError('ohh no..')).toThrow();
// eslint-disable-next-line no-console
expect(console.log).toHaveBeenCalledTimes(2);
expect(henri.log.error).toHaveBeenCalledTimes(1);
});
test('getColor', () => {
const { log: { getColor } } = henri;
expect(getColor).toBeDefined();
expect(getColor('ERROR')).toBe('red');
expect(getColor('wArN')).toBe('yellow');
expect(getColor('info')).toBe('green');
expect(getColor('verbose')).toBe('white');
expect(getColor('debug')).toBe('blue');
expect(getColor('sillY')).toBe('magenta');
expect(getColor('paint me red')).toBe('red');
});
});
43 changes: 43 additions & 0 deletions packages/log/tests/noconf.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
describe('log (no conf)', () => {
beforeAll(() => {
henri = {
config: new Set(),
};
require('../index.js');
});
test('initialize', () => {
expect(henri.log).toBeDefined();
});
test('log to console', () => {
expect(henri.log.transports.console).toBeDefined();
});
test('should not log to a file', () => {
expect(henri.log.transports.file).toBeUndefined();
});
test('notify', () => {
expect(henri.notify).toBeDefined();
expect(() =>
henri.notify('henri framework', 'seems like notification works')
).not.toThrow();
});
test('fatalError', () => {
// eslint-disable-next-line no-console
console.log = jest.fn();
henri.log.error = jest.fn();
expect(() => henri.log.fatalError('ohh no..')).toThrow();
// eslint-disable-next-line no-console
expect(console.log).toHaveBeenCalledTimes(2);
expect(henri.log.error).toHaveBeenCalledTimes(1);
});
test('getColor', () => {
const { log: { getColor } } = henri;
expect(getColor).toBeDefined();
expect(getColor('ERROR')).toBe('red');
expect(getColor('wArN')).toBe('yellow');
expect(getColor('info')).toBe('green');
expect(getColor('verbose')).toBe('white');
expect(getColor('debug')).toBe('blue');
expect(getColor('sillY')).toBe('magenta');
expect(getColor('paint me red')).toBe('red');
});
});
6 changes: 3 additions & 3 deletions packages/log/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
version "8.0.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.4.tgz#d0ca03fa4a3d7ab66c1f4e78a0fd06e30e46a7a9"

"@types/node@^8.0.7":
version "8.0.7"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.7.tgz#fb0ad04b5b6f6eabe0372a32a8f1fbba5c130cae"
"@types/node@^8.0.10":
version "8.0.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.10.tgz#12efec9183b072d5f951cf86395a4c780f868a17"

"@types/winston@^2.3.3":
version "2.3.3"
Expand Down

0 comments on commit 4f1a0b6

Please sign in to comment.