11'use strict'
22
3- const { serverFactory , TIME , unmockTime , mockTime } = require ( './helpers ' )
3+ const { before , beforeEach , after , test , describe , afterEach } = require ( 'node:test ' )
44const pretty = require ( 'pino-pretty' )
5- const { before, beforeEach, after, test } = require ( 'node:test' )
6-
7- const messages = [ ]
8- let server = serverFactory ( messages , { colorize : false } )
5+ const { serverFactory, TIME , unmockTime, mockTime } = require ( './helpers' )
96
107before ( ( ) => {
118 mockTime ( )
@@ -15,30 +12,22 @@ after(() => {
1512 unmockTime ( )
1613} )
1714
18- beforeEach ( ( ) => {
19- // empty messages array
20- messages . splice ( 0 , messages . length )
15+ describe ( 'should log server started messages' , ( ) => {
16+ let messages
17+ let server
2118
22- server = serverFactory ( messages )
23- } )
24-
25- test ( 'should log server started messages' , async ( t ) => {
26- t . beforeEach ( async ( ) => {
27- await server . listen ( { port : 63995 } )
28- t . afterEach ( async ( ) => await server . close ( ) )
19+ beforeEach ( async ( ) => {
20+ server = serverFactory ( messages = [ ] )
21+ await server . listen ( { host : '127.0.0.1' , port : 63995 } )
2922 } )
23+ afterEach ( async ( ) => { await server . close ( ) } )
3024
31- await t . test ( 'colors supported in TTY' , { skip : ! pretty . isColorSupported } , ( t ) => {
32- const messagesExpected = [
33- `${ TIME } - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://127.0.0.1:63995\x1B[39m\n` ,
34- `${ TIME } - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://[::1]:63995\x1B[39m\n`
35- ]
36-
25+ test ( 'colors supported in TTY' , { skip : ! pretty . isColorSupported } , ( t ) => {
3726 // sort because the order of the messages is not guaranteed
38- t . assert . deepStrictEqual ( messages . sort ( ) , messagesExpected . sort ( ) )
27+ t . assert . deepStrictEqual ( messages [ 0 ] , ` ${ TIME } - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://127.0.0.1:63995\x1B[39m\n` )
3928 } )
4029
41- await t . test (
30+ test (
4231 'colors not supported in TTY' ,
4332 { skip : pretty . isColorSupported } ,
4433 ( t ) => {
@@ -51,12 +40,16 @@ test('should log server started messages', async (t) => {
5140 t . assert . deepStrictEqual ( messages . sort ( ) , messagesExpected . sort ( ) )
5241 }
5342 )
54- } )
43+ } ) ;
44+
45+ [ 'GET' , 'POST' , 'PUT' , 'DELETE' , 'PATCH' , 'OPTIONS' , 'HEAD' ] . forEach ( ( method ) => {
46+ describe ( `should log request and response messages for ${ method } ` , ( ) => {
47+ let messages
48+ let server
49+
50+ beforeEach ( async ( ) => {
51+ server = serverFactory ( messages = [ ] )
5552
56- const methods = [ 'GET' , 'POST' , 'PUT' , 'DELETE' , 'PATCH' , 'OPTIONS' , 'HEAD' ]
57- methods . forEach ( ( method ) => {
58- test ( 'should log request and response messages for %p' , async ( t ) => {
59- t . beforeEach ( async ( ) => {
6053 const serverMethod = method === 'HEAD' ? 'GET' : method
6154 server [ serverMethod . toLowerCase ( ) ] ( '/path' , ( _ , req ) => {
6255 req . send ( )
@@ -68,7 +61,7 @@ methods.forEach((method) => {
6861 } )
6962 } )
7063
71- await t . test (
64+ test (
7265 'colors supported in TTY' ,
7366 { skip : ! pretty . isColorSupported } ,
7467 ( t ) => {
@@ -80,7 +73,7 @@ methods.forEach((method) => {
8073 }
8174 )
8275
83- await t . test (
76+ test (
8477 'colors not supported in TTY' ,
8578 { skip : pretty . isColorSupported } ,
8679 ( t ) => {
@@ -94,9 +87,11 @@ methods.forEach((method) => {
9487 } )
9588} )
9689
97- test ( 'should handle user defined log' , async ( t ) => {
98- t . beforeEach ( async ( ) => {
99- server = serverFactory ( messages , { minimumLevel : 'trace' } )
90+ describe ( 'should handle user defined log' , ( ) => {
91+ let messages
92+ let server
93+ beforeEach ( async ( ) => {
94+ server = serverFactory ( messages = [ ] , { minimumLevel : 'trace' } )
10095
10196 server . get ( '/a-path-with-user-defined-log' , ( res , req ) => {
10297 res . log . fatal ( 'a user defined fatal log' )
@@ -112,7 +107,7 @@ test('should handle user defined log', async (t) => {
112107 await server . inject ( '/a-path-with-user-defined-log' )
113108 } )
114109
115- await t . test ( 'colors supported in TTY' , { skip : ! pretty . isColorSupported } , ( t ) => {
110+ test ( 'colors supported in TTY' , { skip : ! pretty . isColorSupported } , ( t ) => {
116111 const messagesExpected = [
117112 `${ TIME } - \x1B[32minfo\x1B[39m - GET /a-path-with-user-defined-log - \x1B[36mincoming request\x1B[39m\n` ,
118113 `${ TIME } - \x1B[41mfatal\x1B[49m - \x1B[36ma user defined fatal log\x1B[39m\n` ,
@@ -126,7 +121,7 @@ test('should handle user defined log', async (t) => {
126121 t . assert . deepStrictEqual ( messages , messagesExpected )
127122 } )
128123
129- await t . test (
124+ test (
130125 'colors not supported in TTY' ,
131126 { skip : pretty . isColorSupported } ,
132127 ( t ) => {
0 commit comments