Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 14e8854

Browse files
eldreygalindosilva-fabio
authored andcommitted
test: add unit test to consolelogger.ts
1 parent 1831409 commit 14e8854

5 files changed

Lines changed: 234 additions & 18 deletions

File tree

packages/opencensus-core/package-lock.json

Lines changed: 52 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"async_hooks": "^1.0.0",
5858
"continuation-local-storage": "^3.2.1",
5959
"debug": "^3.1.0",
60+
"intercept-stdout": "^0.1.2",
6061
"log-driver": "^1.2.7",
6162
"semver": "^5.5.0",
6263
"shimmer": "^1.2.0",

packages/opencensus-core/src/common/console-logger.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
import {Logger, LoggerOptions} from './types';
1818

19-
let logDriver = require('log-driver');
19+
const logDriver = require('log-driver');
2020

2121

2222
class ConsoleLogger implements Logger {
2323

24+
// tslint:disable:no-any
2425
private logger:any;
2526
static LEVELS = ['error', 'warn', 'info', 'debug', 'silly'];
2627

@@ -29,7 +30,7 @@ let logDriver = require('log-driver');
2930
if (typeof options === "string") {
3031
opt = {
3132
level: options
32-
}
33+
};
3334
} else {
3435
opt = options || {};
3536
}
@@ -40,30 +41,35 @@ let logDriver = require('log-driver');
4041
});
4142
}
4243

44+
// tslint:disable:no-any
4345
error(message: any, ...args: any[]): void {
4446
this.logger.error(arguments);
4547
}
4648

49+
// tslint:disable:no-any
4750
warn(message: any, ...args: any[]): void {
4851
this.logger.warn(arguments);
4952
}
5053

54+
// tslint:disable:no-any
5155
info(message: any, ...args: any[]): void {
5256
this.logger.info(arguments);
5357
}
5458

59+
// tslint:disable:no-any
5560
debug (message: any, ...args: any[]): void {
5661
this.logger.debug(arguments);
5762
}
5863

64+
// tslint:disable:no-any
5965
silly (message: any, ...args: any[]): void {
6066
this.logger.silly(arguments);
6167
}
6268
}
6369

6470
let defaultLogger = null;
6571

66-
let logger = function(options?: LoggerOptions|string){
72+
const logger = (options?: LoggerOptions|string) => {
6773
defaultLogger = new ConsoleLogger(options);
6874
logger['logger'] = defaultLogger;
6975
return defaultLogger;

packages/opencensus-core/src/common/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
// tslint:disable:no-any
1718
export type LogFunction = (message: any, ...args: any[]) => void;
1819

1920
export interface Logger {
Lines changed: 171 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,179 @@
1+
/**
2+
* Copyright 2018 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the 'License');
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an 'AS IS' BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
import * as assert from 'assert';
218
import * as mocha from 'mocha';
3-
419
import * as logger from '../src/common/consolelogger';
520

21+
const LEVELS = ['error', 'warn', 'info', 'debug', 'silly'];
22+
let consoleTxt = '';
23+
24+
25+
626

727
describe('ConsoleLogger', () => {
8-
describe('new RootSpan()', () => {
9-
it('should create a RootSpan instance', () => {
10-
const consoleLogger = logger({level: 'error'});
11-
12-
consoleLogger.info("info");
13-
consoleLogger.error("error");
14-
15-
assert.ok(true);
16-
});
28+
const intercept = require('intercept-stdout');
29+
const unhookIntercept = intercept((txt) => {
30+
consoleTxt = txt;
31+
return txt;
32+
});
33+
describe('new ConsoleLogger()', () => {
34+
it('should levels from consoleLogger equals default levels', () => {
35+
const consoleLogger = logger();
36+
assert.equal(LEVELS.length, consoleLogger.logger.levels.length);
37+
});
38+
39+
it('should level from consoleLogger equal error', () => {
40+
const consoleLogger = logger(LEVELS[0]);
41+
assert.strictEqual(LEVELS[0], consoleLogger.logger.level);
42+
});
43+
});
44+
45+
/**
46+
*
47+
*/
48+
describe('error logger', () => {
49+
const consoleLogger = logger(LEVELS[0]);
50+
51+
it('should logger error in console', () => {
52+
consoleTxt = '';
53+
consoleLogger.error('error test logger in console');
54+
unhookIntercept();
55+
const validateString = consoleTxt.indexOf('error');
56+
57+
assert.ok(validateString >= 0);
58+
});
59+
60+
it('should not logger warn in console', () => {
61+
consoleTxt = '';
62+
consoleLogger.warn('warn test logger in console');
63+
unhookIntercept();
64+
65+
const validateString = consoleTxt.indexOf('warn');
66+
67+
assert.equal(validateString, -1);
68+
});
69+
70+
it('should not logger info in console', () => {
71+
consoleTxt = '';
72+
consoleLogger.info('info test logger in console');
73+
unhookIntercept();
74+
75+
const validateString = consoleTxt.indexOf('info');
76+
77+
assert.equal(validateString, -1);
78+
});
79+
80+
it('should not logger debug in console', () => {
81+
consoleTxt = '';
82+
consoleLogger.debug('debug test logger in console');
83+
unhookIntercept();
84+
const validateString = consoleTxt.indexOf('debug');
85+
86+
assert.equal(validateString, -1);
87+
});
88+
89+
it('should not logger silly in console', () => {
90+
consoleTxt = '';
91+
consoleLogger.silly('silly test logger in console');
92+
unhookIntercept();
93+
const validateString = consoleTxt.indexOf('silly');
94+
95+
assert.equal(validateString, -1);
96+
});
97+
});
98+
99+
describe('info logger', () => {
100+
101+
const consoleLogger = logger(LEVELS[2]);
102+
103+
it('should logger error in console', () => {
104+
const intercept = require('intercept-stdout');
105+
const unhookIntercept = intercept((txt) => {
106+
consoleTxt = txt;
107+
return txt;
108+
});
109+
110+
consoleTxt = '';
111+
consoleLogger.error('error test logger in console');
112+
unhookIntercept();
113+
const validateString = consoleTxt.indexOf('error');
114+
115+
assert.ok(validateString >= 0);
116+
});
117+
118+
it('should not logger warn in console', () => {
119+
const intercept = require('intercept-stdout');
120+
const unhookIntercept = intercept((txt) => {
121+
consoleTxt = txt;
122+
return txt;
123+
});
124+
125+
consoleTxt = '';
126+
consoleLogger.warn('warn test logger in console');
127+
unhookIntercept();
128+
const validateString = consoleTxt.indexOf('warn');
129+
130+
assert.ok(validateString >= 0);
131+
});
132+
133+
it('should logger info in console', () => {
134+
const intercept = require('intercept-stdout');
135+
const unhookIntercept = intercept((txt) => {
136+
consoleTxt = txt;
137+
return txt;
138+
});
139+
140+
consoleTxt = '';
141+
consoleLogger.info('info test logger in console');
142+
unhookIntercept();
143+
const validateString = consoleTxt.indexOf('info');
144+
145+
assert.ok(validateString >= 0);
146+
});
147+
148+
it('should not logger debug in console', () => {
149+
const intercept = require('intercept-stdout');
150+
const unhookIntercept = intercept((txt) => {
151+
consoleTxt = txt;
152+
return txt;
153+
});
154+
155+
consoleTxt = '';
156+
consoleLogger.debug('debug test logger in console');
157+
unhookIntercept();
158+
const validateString = consoleTxt.indexOf('debug');
159+
160+
assert.equal(validateString, -1);
161+
});
162+
163+
it('should not logger silly in console', () => {
164+
const intercept = require('intercept-stdout');
165+
const unhookIntercept = intercept((txt) => {
166+
consoleTxt = txt;
167+
return txt;
17168
});
18-
});
169+
170+
consoleTxt = '';
171+
consoleLogger.silly('silly test logger in console');
172+
unhookIntercept();
173+
const validateString = consoleTxt.indexOf('silly');
19174

175+
assert.equal(validateString, -1);
176+
});
177+
});
178+
179+
});

0 commit comments

Comments
 (0)