mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
chore(test): increase test coverage around logger lib
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
import winston from 'winston';
|
import winston from 'winston';
|
||||||
import logger from '../../../../../website/server/libs/logger';
|
import logger from '../../../../../website/server/libs/logger';
|
||||||
|
import {
|
||||||
|
NotFound,
|
||||||
|
} from '../../../../../website/server/libs//errors';
|
||||||
|
|
||||||
describe('logger', () => {
|
describe('logger', () => {
|
||||||
let logSpy;
|
let logSpy;
|
||||||
@@ -21,26 +24,151 @@ describe('logger', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('error', () => {
|
describe('error', () => {
|
||||||
it('passes through arguments if the first arg is not an error object', () => {
|
context('non-error object', () => {
|
||||||
logger.error(1, 2, 3, 4);
|
it('passes through arguments if the first arg is not an error object', () => {
|
||||||
expect(logSpy).to.be.calledOnce;
|
logger.error(1, 2, 3, 4);
|
||||||
expect(logSpy).to.be.calledWith('error', 1, 2, 3, 4);
|
expect(logSpy).to.be.calledOnce;
|
||||||
|
expect(logSpy).to.be.calledWith('error', 1, 2, 3, 4);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parses the error and passes it to the logger when the first arg is an error object', () => {
|
context('error object', () => {
|
||||||
let errInstance = new Error('An error.');
|
it('logs the stack and the err data', () => {
|
||||||
logger.error(errInstance, {
|
let errInstance = new Error('An error.');
|
||||||
data: 1,
|
logger.error(errInstance, {
|
||||||
}, 2, 3);
|
data: 1,
|
||||||
|
}, 2, 3);
|
||||||
|
|
||||||
expect(logSpy).to.be.calledOnce;
|
expect(logSpy).to.be.calledOnce;
|
||||||
expect(logSpy).to.be.calledWith(
|
expect(logSpy).to.be.calledWith(
|
||||||
'error',
|
'error',
|
||||||
errInstance.stack,
|
errInstance.stack,
|
||||||
{ data: 1, fullError: errInstance },
|
{ data: 1, fullError: errInstance },
|
||||||
2,
|
2,
|
||||||
3
|
3
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('logs the stack and the err data with it\'s own fullError property', () => {
|
||||||
|
let errInstance = new Error('An error.');
|
||||||
|
let anotherError = new Error('another error');
|
||||||
|
|
||||||
|
logger.error(errInstance, {
|
||||||
|
data: 1,
|
||||||
|
fullError: anotherError,
|
||||||
|
}, 2, 3);
|
||||||
|
|
||||||
|
expect(logSpy).to.be.calledOnce;
|
||||||
|
expect(logSpy).to.be.calledWith(
|
||||||
|
'error',
|
||||||
|
errInstance.stack,
|
||||||
|
{ data: 1, fullError: anotherError },
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('logs the error when errorData is null', () => {
|
||||||
|
let errInstance = new Error('An error.');
|
||||||
|
|
||||||
|
logger.error(errInstance, null, 2, 3);
|
||||||
|
|
||||||
|
expect(logSpy).to.be.calledOnce;
|
||||||
|
expect(logSpy).to.be.calledWith(
|
||||||
|
'error',
|
||||||
|
errInstance.stack,
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('logs the error when errorData is not an object', () => {
|
||||||
|
let errInstance = new Error('An error.');
|
||||||
|
|
||||||
|
logger.error(errInstance, true, 2, 3);
|
||||||
|
|
||||||
|
expect(logSpy).to.be.calledOnce;
|
||||||
|
expect(logSpy).to.be.calledWith(
|
||||||
|
'error',
|
||||||
|
errInstance.stack,
|
||||||
|
true,
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('logs the error when errorData does not include isHandledError property', () => {
|
||||||
|
let errInstance = new Error('An error.');
|
||||||
|
|
||||||
|
logger.error(errInstance, { httpCode: 400 }, 2, 3);
|
||||||
|
|
||||||
|
expect(logSpy).to.be.calledOnce;
|
||||||
|
expect(logSpy).to.be.calledWith(
|
||||||
|
'error',
|
||||||
|
errInstance.stack,
|
||||||
|
{ httpCode: 400, fullError: errInstance },
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('logs the error when errorData includes isHandledError property but is a 500 error', () => {
|
||||||
|
let errInstance = new Error('An error.');
|
||||||
|
|
||||||
|
logger.error(errInstance, {
|
||||||
|
isHandledError: true,
|
||||||
|
httpCode: 502,
|
||||||
|
}, 2, 3);
|
||||||
|
|
||||||
|
expect(logSpy).to.be.calledOnce;
|
||||||
|
expect(logSpy).to.be.calledWith(
|
||||||
|
'error',
|
||||||
|
errInstance.stack,
|
||||||
|
{ httpCode: 502, isHandledError: true, fullError: errInstance },
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('logs a warning when errorData includes isHandledError property and is not a 500 error', () => {
|
||||||
|
let errInstance = new Error('An error.');
|
||||||
|
|
||||||
|
logger.error(errInstance, {
|
||||||
|
isHandledError: true,
|
||||||
|
httpCode: 403,
|
||||||
|
}, 2, 3);
|
||||||
|
|
||||||
|
expect(logSpy).to.be.calledOnce;
|
||||||
|
expect(logSpy).to.be.calledWith(
|
||||||
|
'warn',
|
||||||
|
errInstance.stack,
|
||||||
|
{ httpCode: 403, isHandledError: true, fullError: errInstance },
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('logs additional data from a CustomError', () => {
|
||||||
|
let errInstance = new NotFound('An error.');
|
||||||
|
|
||||||
|
errInstance.customField = 'Some interesting data';
|
||||||
|
|
||||||
|
logger.error(errInstance, {}, 2, 3);
|
||||||
|
|
||||||
|
expect(logSpy).to.be.calledOnce;
|
||||||
|
expect(logSpy).to.be.calledWith(
|
||||||
|
'error',
|
||||||
|
errInstance.stack,
|
||||||
|
{
|
||||||
|
fullError: {
|
||||||
|
customField: 'Some interesting data',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user