Skip to content

Commit 10a9051

Browse files
committed
Add unit test
Signed-off-by: Shubham Sharma <[email protected]>
1 parent d108da9 commit 10a9051

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
"test:e2e:http": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components -- jest --runInBand --detectOpenHandles --testMatch [ '**/test/e2e/http/(client|server).test.ts' ]",
1414
"test:e2e:http:actors": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components -- jest --runInBand --detectOpenHandles --testMatch [ '**/test/e2e/http/actors.test.ts' ]",
1515
"test:unit": "jest --runInBand --detectOpenHandles",
16-
"test:unit:all": "npm run test:unit:main && npm run test:unit:actors && npm run test:unit:utils",
16+
"test:unit:all": "npm run test:unit:main && npm run test:unit:actors && npm run test:unit:logger && npm run test:unit:utils",
1717
"test:unit:main": "NODE_ENV=test npm run test:unit 'test/unit/main/.*\\.test\\.ts'",
1818
"test:unit:actors": "NODE_ENV=test npm run test:unit 'test/unit/actor/.*\\.test\\.ts'",
19+
"test:unit:logger": "NODE_ENV=test npm run test:unit 'test/unit/logger/.*\\.test\\.ts'",
1920
"test:unit:utils": "NODE_ENV=test npm run test:unit 'test/unit/utils/.*\\.test\\.ts'",
2021
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
2122
"build": "./scripts/build.sh",

src/logger/Logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ export class Logger {
2828
* @param options Logger options
2929
*/
3030
constructor(options?: LoggerOptions) {
31-
if (options && options.logLevel) {
31+
if (options !== undefined && options.logLevel !== undefined) {
3232
this.logLevel = options.logLevel;
3333
} else {
3434
this.logLevel = LogLevel.info;
3535
}
3636

37-
if (options && options.logService) {
37+
if (options !== undefined && options.logService !== undefined) {
3838
this.logService = options.logService;
3939
} else {
4040
this.logService = new ConsoleLoggerService();

test/unit/logger/logger.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2022 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import { LoggerService } from "../../../src";
15+
import { Logger } from "../../../src/logger/Logger"
16+
import { LogLevel } from "../../../src/types/logger/LogLevel"
17+
18+
describe('Logger', () => {
19+
it('should not display logs below the set log level', () => {
20+
// arrange
21+
const mockLoggerService = new MockLoggerService();
22+
const errorLogger = new Logger({ logLevel: LogLevel.error, logService: mockLoggerService })
23+
const infoLogger = new Logger({ logLevel: LogLevel.info, logService: mockLoggerService })
24+
const debugLogger = new Logger({ logLevel: LogLevel.debug, logService: mockLoggerService })
25+
26+
// act
27+
callAllLogLevels(errorLogger)
28+
callAllLogLevels(infoLogger)
29+
callAllLogLevels(debugLogger)
30+
31+
// assert
32+
expect(mockLoggerService.errorCounter).toBe(1+1+1)
33+
expect(mockLoggerService.warnCounter).toBe(0+1+1)
34+
expect(mockLoggerService.infoCounter).toBe(0+1+1)
35+
expect(mockLoggerService.verboseCounter).toBe(0+0+1)
36+
expect(mockLoggerService.debugCounter).toBe(0+0+1)
37+
})
38+
})
39+
40+
class MockLoggerService implements LoggerService {
41+
public errorCounter = 0;
42+
public warnCounter = 0;
43+
public infoCounter = 0;
44+
public verboseCounter = 0;
45+
public debugCounter = 0;
46+
47+
error(_message: any, ..._optionalParams: any[]): void {
48+
this.errorCounter++;
49+
}
50+
warn(_message: any, ..._optionalParams: any[]): void {
51+
this.warnCounter++;
52+
}
53+
info(_message: any, ..._optionalParams: any[]): void {
54+
this.infoCounter++;
55+
}
56+
verbose(_message: any, ..._optionalParams: any[]): void {
57+
this.verboseCounter++;
58+
}
59+
debug(_message: any, ..._optionalParams: any[]): void {
60+
this.debugCounter++
61+
}
62+
}
63+
64+
function callAllLogLevels(logger: Logger) {
65+
logger.error("component", "area", "message")
66+
logger.warn("component", "area", "message")
67+
logger.info("component", "area", "message")
68+
logger.verbose("component", "area", "message")
69+
logger.debug("component", "area", "message")
70+
}

0 commit comments

Comments
 (0)