1
1
import { expect } from 'chai' ;
2
2
import * as sinon from 'sinon' ;
3
+ import { Logger } from '@nestjs/common' ;
3
4
import { ExceptionsZone } from '../../../errors/exceptions-zone' ;
4
5
5
6
describe ( 'ExceptionsZone' , ( ) => {
@@ -13,51 +14,103 @@ describe('ExceptionsZone', () => {
13
14
callback = sinon . spy ( ) ;
14
15
} ) ;
15
16
it ( 'should call callback' , ( ) => {
16
- ExceptionsZone . run ( callback as any , rethrow ) ;
17
+ ExceptionsZone . run ( callback as any , rethrow , false ) ;
17
18
expect ( callback . called ) . to . be . true ;
18
19
} ) ;
19
20
describe ( 'when callback throws exception' , ( ) => {
20
21
const exceptionHandler = {
21
22
handle : ( ) => { } ,
22
23
} ;
23
24
let handleSpy : sinon . SinonSpy ;
25
+ let LoggerFlushSpy : sinon . SinonSpy ;
24
26
before ( ( ) => {
25
27
( ExceptionsZone as any ) . exceptionHandler = exceptionHandler ;
26
28
handleSpy = sinon . spy ( exceptionHandler , 'handle' ) ;
29
+ LoggerFlushSpy = sinon . spy ( Logger , 'flush' ) ;
27
30
} ) ;
28
- it ( 'should call "handle" method of exceptionHandler and rethrows' , ( ) => {
29
- const throwsCallback = ( ) => {
30
- throw new Error ( '' ) ;
31
- } ;
32
- expect ( ( ) => ExceptionsZone . run ( throwsCallback , rethrow ) ) . to . throws ( ) ;
33
- expect ( handleSpy . called ) . to . be . true ;
31
+ after ( ( ) => {
32
+ LoggerFlushSpy . restore ( ) ;
33
+ } ) ;
34
+ describe ( 'when callback throws exception and autoFlushLogs is false' , ( ) => {
35
+ it ( 'should call "handle" method of exceptionHandler and rethrows and not flush logs' , ( ) => {
36
+ const throwsCallback = ( ) => {
37
+ throw new Error ( '' ) ;
38
+ } ;
39
+ expect ( ( ) =>
40
+ ExceptionsZone . run ( throwsCallback , rethrow , false ) ,
41
+ ) . to . throws ( ) ;
42
+
43
+ expect ( handleSpy . called ) . to . be . true ;
44
+
45
+ expect ( LoggerFlushSpy . called ) . to . be . false ;
46
+ } ) ;
47
+ } ) ;
48
+
49
+ describe ( 'when callback throws exception and autoFlushLogs is true' , ( ) => {
50
+ it ( 'should call "handle" method of exceptionHandler and rethrows and flush logs' , ( ) => {
51
+ const throwsCallback = ( ) => {
52
+ throw new Error ( '' ) ;
53
+ } ;
54
+ expect ( ( ) =>
55
+ ExceptionsZone . run ( throwsCallback , rethrow , true ) ,
56
+ ) . to . throws ( ) ;
57
+
58
+ expect ( handleSpy . called ) . to . be . true ;
59
+
60
+ expect ( LoggerFlushSpy . called ) . to . be . true ;
61
+ } ) ;
34
62
} ) ;
35
63
} ) ;
36
64
} ) ;
65
+
37
66
describe ( 'asyncRun' , ( ) => {
38
67
let callback : sinon . SinonSpy ;
39
68
beforeEach ( ( ) => {
40
69
callback = sinon . spy ( ) ;
41
70
} ) ;
42
71
it ( 'should call callback' , async ( ) => {
43
- await ExceptionsZone . asyncRun ( callback as any , rethrow ) ;
72
+ await ExceptionsZone . asyncRun ( callback as any , rethrow , false ) ;
44
73
expect ( callback . called ) . to . be . true ;
45
74
} ) ;
46
75
describe ( 'when callback throws exception' , ( ) => {
47
76
const exceptionHandler = {
48
77
handle : ( ) => { } ,
49
78
} ;
50
79
let handleSpy : sinon . SinonSpy ;
80
+ let LoggerFlushSpy : sinon . SinonSpy ;
51
81
before ( ( ) => {
52
82
( ExceptionsZone as any ) . exceptionHandler = exceptionHandler ;
53
83
handleSpy = sinon . spy ( exceptionHandler , 'handle' ) ;
84
+ LoggerFlushSpy = sinon . spy ( Logger , 'flush' ) ;
85
+ } ) ;
86
+ after ( ( ) => {
87
+ LoggerFlushSpy . restore ( ) ;
54
88
} ) ;
55
- it ( 'should call "handle" method of exceptionHandler and rethrows error' , async ( ) => {
56
- const throwsCallback = ( ) => {
57
- throw new Error ( '' ) ;
58
- } ;
59
- expect ( ExceptionsZone . asyncRun ( throwsCallback , rethrow ) ) . to . eventually
60
- . be . rejected ;
89
+ describe ( 'when callback throws exception and autoFlushLogs is false' , ( ) => {
90
+ it ( 'should call "handle" method of exceptionHandler and rethrows error and not flush logs' , async ( ) => {
91
+ const throwsCallback = ( ) => {
92
+ throw new Error ( '' ) ;
93
+ } ;
94
+ expect ( ExceptionsZone . asyncRun ( throwsCallback , rethrow , false ) ) . to
95
+ . eventually . be . rejected ;
96
+
97
+ expect ( handleSpy . called ) . to . be . true ;
98
+
99
+ expect ( LoggerFlushSpy . called ) . to . be . false ;
100
+ } ) ;
101
+ } ) ;
102
+ describe ( 'when callback throws exception and autoFlushLogs is true' , ( ) => {
103
+ it ( 'should call "handle" method of exceptionHandler and rethrows error and flush logs' , async ( ) => {
104
+ const throwsCallback = ( ) => {
105
+ throw new Error ( '' ) ;
106
+ } ;
107
+ expect ( ExceptionsZone . asyncRun ( throwsCallback , rethrow , true ) ) . to
108
+ . eventually . be . rejected ;
109
+
110
+ expect ( handleSpy . called ) . to . be . true ;
111
+
112
+ expect ( LoggerFlushSpy . called ) . to . be . true ;
113
+ } ) ;
61
114
} ) ;
62
115
} ) ;
63
116
} ) ;
0 commit comments