UNPKG

3.5 kBPlain TextView Raw
1/*
2 * MIT License
3 *
4 * Copyright (c) 2019 nest-mods
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25import { Injectable, LoggerService } from '@nestjs/common';
26/*
27 * Created by Diluka on 2019-02-25.
28 *
29 *
30 * ----------- 神 兽 佑 我 -----------
31 * ┏┓ ┏┓+ +
32 * ┏┛┻━━━━━━┛┻┓ + +
33 * ┃ ┃
34 * ┣ ━ ┃ ++ + + +
35 * ████━████ ┃+
36 * ┃ ┃ +
37 * ┃ ┴ ┃
38 * ┃ ┃ + +
39 * ┗━┓ ┏━┛ Code is far away from bug
40 * ┃ ┃ with the animal protecting
41 * ┃ ┃ + + + +
42 * ┃ ┃
43 * ┃ ┃ +
44 * ┃ ┃ + +
45 * ┃ ┃ +
46 * ┃ ┗━━━┓ + +
47 * ┃ ┣┓
48 * ┃ ┏┛
49 * ┗┓┓┏━━━━┳┓┏┛ + + + +
50 * ┃┫┫ ┃┫┫
51 * ┗┻┛ ┗┻┛+ + + +
52 * ----------- 永 无 BUG ------------
53 */
54import { Test } from '@nestjs/testing';
55import { Log, LogInvoke, LogModule } from '../src';
56
57@Injectable()
58class DemoService {
59
60 @Log() private logger: LoggerService;
61
62 test1(p1: string) {
63 this.logger.log(`print p1 ${p1}`);
64 }
65
66 test2() {
67 this.logger.error(['test2 %s', 'OK'], new Error('test2') as any);
68 }
69
70 test3() {
71 this.logger.warn('test3');
72 }
73
74 @LogInvoke({ afterLevel: 'info', showParams: true })
75 test4(a: string, b: number) {
76 this.logger.debug('test4');
77 }
78
79 @LogInvoke({ message: 'calling test5', printString: true, showReturns: true })
80 test5() {
81 this.logger.log({ data: 'ok' });
82 return { test: 'ok' };
83 }
84
85 @LogInvoke({ message: 'log error' })
86 async test6() {
87 throw new Error('oops!');
88 }
89}
90
91describe('日志测试', function() {
92
93 let service: DemoService;
94
95 beforeAll(async () => {
96 const module = await Test.createTestingModule({
97 imports: [LogModule.forRoot('test')],
98 providers: [DemoService],
99 }).compile();
100
101 service = module.get(DemoService);
102 });
103
104 it('test1', function() {
105 service.test1('test1');
106 });
107
108 it('test2', function() {
109 service.test2();
110 });
111
112 it('test3', function() {
113 service.test3();
114 });
115
116 it('test4', function() {
117 service.test4('test', 123);
118 });
119
120 it('test5', function() {
121 service.test5();
122 });
123
124 it('test6', function() {
125 service.test6().catch(e => null);
126 });
127});