mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
v3: expose public interface for logger and add tests
This commit is contained in:
58
test/api/v3/unit/libs/logger.js
Normal file
58
test/api/v3/unit/libs/logger.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import winston from 'winston';
|
||||
|
||||
/* eslint-disable global-require */
|
||||
describe('logger', () => {
|
||||
let pathToLoggerLib = '../../../../../website/src/libs/api-v3/logger';
|
||||
let infoSpy;
|
||||
let errorSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
delete require.cache[require.resolve(pathToLoggerLib)];
|
||||
|
||||
infoSpy = sandbox.stub();
|
||||
errorSpy = sandbox.stub();
|
||||
sandbox.stub(winston, 'Logger').returns({
|
||||
info: infoSpy,
|
||||
error: errorSpy,
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it('info', () => {
|
||||
let attachLogger = require(pathToLoggerLib);
|
||||
attachLogger.info(1, 2, 3);
|
||||
expect(infoSpy).to.be.calledOnce;
|
||||
expect(infoSpy).to.be.calledWith(1, 2, 3);
|
||||
});
|
||||
|
||||
describe('error', () => {
|
||||
it('with custom arguments', () => {
|
||||
let attachLogger = require(pathToLoggerLib);
|
||||
attachLogger.error(1, 2, 3, 4);
|
||||
expect(errorSpy).to.be.calledOnce;
|
||||
expect(errorSpy).to.be.calledWith(1, 2, 3, 4);
|
||||
});
|
||||
|
||||
it('with error', () => {
|
||||
let attachLogger = require(pathToLoggerLib);
|
||||
let errInstance = new Error('An error.');
|
||||
attachLogger.error(errInstance, {
|
||||
data: 1,
|
||||
}, 2, 3);
|
||||
expect(errorSpy).to.be.calledOnce;
|
||||
// using calledWith doesn't work
|
||||
let lastCallArgs = errorSpy.lastCall.args;
|
||||
|
||||
expect(lastCallArgs[3]).to.equal(3);
|
||||
expect(lastCallArgs[2]).to.equal(2);
|
||||
expect(lastCallArgs[1]).to.eql({
|
||||
data: 1,
|
||||
fullError: errInstance,
|
||||
});
|
||||
expect(lastCallArgs[0]).to.eql(errInstance.stack);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user