@@ -3,7 +3,7 @@ import { promises } from "fs"
3
3
import * as http from "http"
4
4
import * as https from "https"
5
5
import * as path from "path"
6
- import { createApp , ensureAddress , handleArgsSocketCatchError , handleServerError } from "../../../src/node/app"
6
+ import { createApp , ensureAddress , handleArgsSocketCatchError , handleServerError , listen } from "../../../src/node/app"
7
7
import { OptionalString , setDefaults } from "../../../src/node/cli"
8
8
import { generateCertificate } from "../../../src/node/util"
9
9
import { clean , mockLogger , getAvailablePort , tmpdir } from "../../utils/helpers"
@@ -201,31 +201,33 @@ describe("handleArgsSocketCatchError", () => {
201
201
} )
202
202
203
203
it ( "should log an error if its not an NodeJS.ErrnoException" , ( ) => {
204
- const error = new Error ( )
204
+ const message = "other message"
205
+ const error = new Error ( message )
205
206
206
- handleArgsSocketCatchError ( error )
207
-
208
- expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
209
- expect ( logger . error ) . toHaveBeenCalledWith ( error )
207
+ expect ( ( ) => {
208
+ handleArgsSocketCatchError ( error )
209
+ } ) . toThrowError ( error )
210
210
} )
211
211
212
212
it ( "should log an error if its not an NodeJS.ErrnoException (and the error has a message)" , ( ) => {
213
213
const errorMessage = "handleArgsSocketCatchError Error"
214
214
const error = new Error ( errorMessage )
215
215
216
- handleArgsSocketCatchError ( error )
217
-
218
- expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
219
- expect ( logger . error ) . toHaveBeenCalledWith ( errorMessage )
216
+ expect ( ( ) => {
217
+ handleArgsSocketCatchError ( error )
218
+ } ) . toThrowError ( error )
220
219
} )
221
220
222
- it ( "should not log an error if its a iNodeJS.ErrnoException" , ( ) => {
223
- const error : NodeJS . ErrnoException = new Error ( )
224
- error . code = "ENOENT"
221
+ it ( "should not log an error if its a NodeJS.ErrnoException" , ( ) => {
222
+ const code = "ENOENT"
223
+ const error : NodeJS . ErrnoException = new Error ( code )
224
+ error . code = code
225
225
226
226
handleArgsSocketCatchError ( error )
227
227
228
- expect ( logger . error ) . toHaveBeenCalledTimes ( 0 )
228
+ expect ( ( ) => {
229
+ handleArgsSocketCatchError ( error )
230
+ } ) . not . toThrowError ( )
229
231
} )
230
232
231
233
it ( "should log an error if the code is not ENOENT (and the error has a message)" , ( ) => {
@@ -234,19 +236,50 @@ describe("handleArgsSocketCatchError", () => {
234
236
error . code = "EACCESS"
235
237
error . message = errorMessage
236
238
237
- handleArgsSocketCatchError ( error )
238
-
239
- expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
240
- expect ( logger . error ) . toHaveBeenCalledWith ( errorMessage )
239
+ expect ( ( ) => {
240
+ handleArgsSocketCatchError ( error )
241
+ } ) . toThrowError ( error )
241
242
} )
242
243
243
244
it ( "should log an error if the code is not ENOENT" , ( ) => {
244
- const error : NodeJS . ErrnoException = new Error ( )
245
- error . code = "EACCESS"
245
+ const code = "EACCESS"
246
+ const error : NodeJS . ErrnoException = new Error ( code )
247
+ error . code = code
246
248
247
- handleArgsSocketCatchError ( error )
249
+ expect ( ( ) => {
250
+ handleArgsSocketCatchError ( error )
251
+ } ) . toThrowError ( error )
252
+ } )
253
+ } )
248
254
249
- expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
250
- expect ( logger . error ) . toHaveBeenCalledWith ( error )
255
+ describe ( "listen" , ( ) => {
256
+ let tmpDirPath : string
257
+ let mockServer : http . Server
258
+
259
+ const testName = "listen"
260
+
261
+ beforeEach ( async ( ) => {
262
+ await clean ( testName )
263
+ mockLogger ( )
264
+ tmpDirPath = await tmpdir ( testName )
265
+ mockServer = http . createServer ( )
266
+ } )
267
+
268
+ afterEach ( ( ) => {
269
+ mockServer . close ( )
270
+ jest . clearAllMocks ( )
271
+ } )
272
+
273
+ it ( "should throw an error if a directory is passed in instead of a file" , async ( ) => {
274
+ const errorMessage = "EISDIR: illegal operation on a directory, unlink"
275
+ const port = await getAvailablePort ( )
276
+ const mockArgs = { port, host : "0.0.0.0" , socket : tmpDirPath }
277
+
278
+ try {
279
+ await listen ( mockServer , mockArgs )
280
+ } catch ( error ) {
281
+ expect ( error ) . toBeInstanceOf ( Error )
282
+ expect ( ( error as any ) . message ) . toMatch ( errorMessage )
283
+ }
251
284
} )
252
285
} )
0 commit comments