Skip to content

Commit 7521bb9

Browse files
committed
fix(logger): do not stub console.log in tests (mocha uses it)
1 parent 30e6b5a commit 7521bb9

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/logger/logger.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Logger {
2222
static _validate(options) {
2323
Object.keys(options).forEach(key => {
2424
if (!allowedKeys.includes(key)) {
25-
throw new Error('Only the following keys are allowed: formatter, output')
25+
throw new Error('Only the following keys are allowed: formatter, output');
2626
}
2727
});
2828
}
@@ -46,7 +46,7 @@ class Logger {
4646
_shortenStackTrace(stack) {
4747
return stack.length > STACK_TRACE_LIMIT
4848
? stack.substring(0, STACK_TRACE_LIMIT) + ' ...'
49-
: stack
49+
: stack;
5050
}
5151

5252
_shortenData(data) {
@@ -58,7 +58,7 @@ class Logger {
5858

5959
return stringifiedData.length > DATA_LIMIT
6060
? stringifiedData.substring(0, DATA_LIMIT) + ' ...'
61-
: stringifiedData
61+
: stringifiedData;
6262
}
6363

6464
_getErrorDetails(error) {
@@ -67,7 +67,7 @@ class Logger {
6767
error_stack: this._shortenStackTrace(error.stack),
6868
error_message: error.message,
6969
error_data: this._shortenData(error.data)
70-
}
70+
};
7171
}
7272
}
7373

@@ -94,13 +94,13 @@ const logMethodFactory = function(level) {
9494
);
9595

9696
Logger.config.transformers.forEach((transform) => {
97-
dataToLog = transform(dataToLog)
97+
dataToLog = transform(dataToLog);
9898
});
9999

100100
Logger.config.output(
101101
Logger.config.formatter(dataToLog)
102102
);
103-
}
103+
};
104104
};
105105

106106
Logger.prototype.trace = logMethodFactory('trace');

src/logger/logger.spec.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ const consoleOutput = require('../output/console');
66

77
describe('Logger', function() {
88
let logger;
9+
let outputStub;
910

1011
beforeEach(function() {
1112
logger = new Logger('mongo', true);
12-
this.sandbox.stub(console, 'log');
13+
outputStub = this.sandbox.stub(Logger.config, 'output');
1314
});
1415

1516
afterEach(function() {
@@ -23,7 +24,7 @@ describe('Logger', function() {
2324
it('should call log info method when enabled', function() {
2425
logger.info('wedidit', { details: 'forever' });
2526

26-
const logArguments = JSON.parse(console.log.args[0]);
27+
const logArguments = JSON.parse(Logger.config.output.args[0]);
2728
expect(logArguments.name).to.eql('mongo');
2829
expect(logArguments.action).to.eql('wedidit');
2930
expect(logArguments.level).to.eql(30);
@@ -35,7 +36,7 @@ describe('Logger', function() {
3536

3637
logger.info('hi');
3738

38-
expect(console.log).not.to.have.been.called;
39+
expect(Logger.config.output).not.to.have.been.called;
3940
});
4041

4142
it('should not call log info method when disabled', function() {
@@ -55,7 +56,7 @@ describe('Logger', function() {
5556

5657
logger.fromError('hi', error, { details: 'here' });
5758

58-
const logArguments = JSON.parse(console.log.args[0]);
59+
const logArguments = JSON.parse(Logger.config.output.args[0]);
5960
expect(logArguments.name).to.eql('mongo');
6061
expect(logArguments.action).to.eql('hi');
6162
expect(logArguments.level).to.eql(50);
@@ -73,7 +74,7 @@ describe('Logger', function() {
7374

7475
logger.warnFromError('hi', error, { details: 'here' });
7576

76-
const logArguments = JSON.parse(console.log.args[0]);
77+
const logArguments = JSON.parse(Logger.config.output.args[0]);
7778
expect(logArguments.name).to.eql('mongo');
7879
expect(logArguments.action).to.eql('hi');
7980
expect(logArguments.level).to.eql(40);
@@ -90,7 +91,7 @@ describe('Logger', function() {
9091

9192
logger.warnFromError('hi', error, { details: 'here' });
9293

93-
const logArguments = JSON.parse(console.log.args[0]);
94+
const logArguments = JSON.parse(Logger.config.output.args[0]);
9495
expect(logArguments.name).to.eql('mongo');
9596
expect(logArguments.action).to.eql('hi');
9697
expect(logArguments.level).to.eql(40);
@@ -108,7 +109,7 @@ describe('Logger', function() {
108109

109110
logger.warnFromError('hi', error, { details: 'here' });
110111

111-
const logArguments = JSON.parse(console.log.args[0]);
112+
const logArguments = JSON.parse(Logger.config.output.args[0]);
112113
expect(logArguments.name).to.eql('mongo');
113114
expect(logArguments.action).to.eql('hi');
114115
expect(logArguments.level).to.eql(40);
@@ -132,7 +133,7 @@ describe('Logger', function() {
132133
logger.info('hi');
133134

134135
expect(formatterStub).to.have.been.called;
135-
expect(console.log).to.have.been.calledWith(formattedOutput);
136+
expect(Logger.config.output).to.have.been.calledWith(formattedOutput);
136137
});
137138

138139
it('should change output method', function() {
@@ -151,7 +152,7 @@ describe('Logger', function() {
151152
invalid: true
152153
});
153154
throw new Error('should throw');
154-
} catch(e) {
155+
} catch (e) {
155156
expect(e.message).to.eql('Only the following keys are allowed: formatter, output');
156157
}
157158
});
@@ -165,7 +166,7 @@ describe('Logger', function() {
165166

166167
logger.info('hi');
167168

168-
const logArguments = JSON.parse(console.log.args[0]);
169+
const logArguments = JSON.parse(Logger.config.output.args[0]);
169170
expect(logArguments.action).to.eql('hi');
170171
expect(logArguments.debug).to.eql(true);
171172
});

0 commit comments

Comments
 (0)