1
+ const express = require ( 'express' ) ;
1
2
const axios = require ( 'axios' ) ;
2
3
const sinon = require ( 'sinon' ) ;
3
4
const nock = require ( 'nock' ) ;
@@ -61,10 +62,12 @@ describe('Error Handling', () => {
61
62
productId : 2 ,
62
63
mode : 'approved' ,
63
64
} ;
64
-
65
+
65
66
sinon
66
67
. stub ( OrderRepository . prototype , 'addOrder' )
67
- . rejects ( new AppError ( 'saving-failed' , 'Order could not be saved' , 500 ) ) ;
68
+ . rejects (
69
+ new AppError ( 'saving-failed' , 'Order could not be saved' , 500 )
70
+ ) ;
68
71
const loggerDouble = sinon . stub ( logger , 'error' ) ;
69
72
70
73
//Act
@@ -87,7 +90,11 @@ describe('Error Handling', () => {
87
90
mode : 'approved' ,
88
91
} ;
89
92
90
- const errorToThrow = new AppError ( 'example-error' , 'some example message' , 500 ) ;
93
+ const errorToThrow = new AppError (
94
+ 'example-error' ,
95
+ 'some example message' ,
96
+ 500
97
+ ) ;
91
98
sinon . stub ( OrderRepository . prototype , 'addOrder' ) . throws ( errorToThrow ) ;
92
99
const metricsExporterDouble = sinon . stub ( metricsExporter , 'fireMetric' ) ;
93
100
@@ -126,7 +133,7 @@ describe('Error Handling', () => {
126
133
expect ( processExitListener . called ) . toBe ( true ) ;
127
134
} ) ;
128
135
129
- test ( 'When unknown exception is throw during request, Then its treated as trusted error and the process stays alive' , async ( ) => {
136
+ test ( 'When unknown exception is throw during request, Then the process stays alive' , async ( ) => {
130
137
//Arrange
131
138
const orderToAdd = {
132
139
userId : 1 ,
@@ -146,11 +153,37 @@ describe('Error Handling', () => {
146
153
expect ( processExitListener . called ) . toBe ( false ) ;
147
154
} ) ;
148
155
} ) ;
149
-
150
156
describe ( 'Various Throwing Scenarios And Locations' , ( ) => {
151
- test . todo (
152
- "When an error is thrown during startup, then it's handled correctly"
153
- ) ;
157
+ test ( 'When unhandled exception is throw, Then the logger reports correctly' , async ( ) => {
158
+ //Arrange
159
+ const loggerDouble = sinon . stub ( logger , 'error' ) ;
160
+ const errorToThrow = new Error ( 'An error that wont be caught 😳' ) ;
161
+
162
+ //Act
163
+ process . emit ( 'uncaughtException' , errorToThrow ) ;
164
+
165
+ // Assert
166
+ expect ( loggerDouble . lastCall . firstArg ) . toMatchObject ( errorToThrow ) ;
167
+ } ) ;
168
+
169
+ test . skip ( 'When an error is thrown during startup for more than 3 times, then the process exits' , async ( ) => {
170
+ // Arrange
171
+ const noneExistingPort = 80 ;
172
+ sinon . restore ( ) ;
173
+ //const app = express();
174
+ const http = require ( 'http' ) ;
175
+ sinon . stub ( http , 'createServer' ) . throws ( new Error ( 'Can not start' ) ) ;
176
+ //sinon.stub(app, 'listen').throws(new Error());
177
+ //process.env.PORT = noneExistingPort;
178
+ const processExitListener = sinon . stub ( process , 'exit' ) ;
179
+
180
+ // Act
181
+ await initializeWebServer ( ) ;
182
+
183
+ // Assert
184
+ expect ( processExitListener . called ) . toBe ( true ) ;
185
+ } ) ;
186
+
154
187
test . todo (
155
188
"When an error is thrown during web request, then it's handled correctly"
156
189
) ;
@@ -187,14 +220,14 @@ describe('Error Handling', () => {
187
220
188
221
sinon . stub ( OrderRepository . prototype , 'addOrder' ) . throws ( errorInstance ) ;
189
222
const metricsExporterDouble = sinon . stub ( metricsExporter , 'fireMetric' ) ;
190
- const consoleErrorDouble = sinon . stub ( console , 'error' ) ;
223
+ const loggerDouble = sinon . stub ( logger , 'error' ) ;
191
224
192
225
//Act
193
226
await axiosAPIClient . post ( '/order' , orderToAdd ) ;
194
227
195
228
//Assert
196
229
expect ( metricsExporterDouble . called ) . toBe ( true ) ;
197
- expect ( consoleErrorDouble . called ) . toBe ( true ) ;
230
+ expect ( loggerDouble . called ) . toBe ( true ) ;
198
231
}
199
232
) ;
200
233
} ) ;
0 commit comments