@@ -5,10 +5,16 @@ import { jest } from "@jest/globals";
55import  {  requestLog  }  from  "../src/index.js" ; 
66
77describe ( "logging" ,  ( )  =>  { 
8-   it ( "logs sucessful 'GET /'" ,  async  ( )  =>  { 
9-     const  mock  =  fetchMock 
10-       . sandbox ( ) 
11-       . getOnce ( "https://api.github.com/" ,  {  ok : true  } ) ; 
8+   it ( "logs successful 'GET /'" ,  async  ( )  =>  { 
9+     const  mock  =  fetchMock . sandbox ( ) . getOnce ( "https://api.github.com/" ,  { 
10+       status : 200 , 
11+       body : { 
12+         ok : true , 
13+       } , 
14+       headers : { 
15+         "x-github-request-id" : "1234" , 
16+       } , 
17+     } ) ; 
1218
1319    const  mockLogInfo  =  jest . fn ( ) ; 
1420    const  mockDebugInfo  =  jest . fn ( ) ; 
@@ -31,15 +37,22 @@ describe("logging", () => {
3137      method : "GET" , 
3238      url : "/" , 
3339    } ) ; 
34-     expect ( mockLogInfo . mock . calls [ 0 ] [ 0 ] ) . toMatch ( / G E T   \/   -   2 0 0   i n   \d + m s / ) ; 
40+     expect ( mockLogInfo . mock . calls [ 0 ] [ 0 ] ) . toMatch ( 
41+       / G E T   \/   -   2 0 0   w i t h   i d   1 2 3 4   i n   \d + m s / , 
42+     ) ; 
3543  } ) ; 
3644
3745  it ( "logs 404 for 'GET /unknown'" ,  async  ( )  =>  { 
38-     const  mock  =  fetchMock 
39-       . sandbox ( ) 
40-       . getOnce ( "https://api.github.com/unknown" ,  404 ) ; 
46+     const  mock  =  fetchMock . sandbox ( ) . getOnce ( "https://api.github.com/unknown" ,  { 
47+       status : 404 , 
48+       body : {  message : "Not Found"  } , 
49+       headers : { 
50+         "x-github-request-id" : "1234" , 
51+       } , 
52+     } ) ; 
4153
4254    const  mockLogInfo  =  jest . fn ( ) ; 
55+     const  mockErrorInfo  =  jest . fn ( ) ; 
4356    const  mockDebugInfo  =  jest . fn ( ) ; 
4457    const  MyOctokit  =  Octokit . plugin ( requestLog ) ; 
4558    const  octokit  =  new  MyOctokit ( { 
@@ -50,15 +63,49 @@ describe("logging", () => {
5063        debug : mockDebugInfo , 
5164        info : mockLogInfo , 
5265        warn ( )  { } , 
53-         error ( )  { } , 
66+         error : mockErrorInfo , 
67+       } , 
68+     } ) ; 
69+ 
70+     try  { 
71+       await  octokit . request ( "GET /unknown" ) ; 
72+     }  catch  ( error )  { 
73+       expect ( mockErrorInfo . mock . calls [ 0 ] [ 0 ] ) . toMatch ( 
74+         / G E T   \/ u n k n o w n   -   4 0 4   w i t h   i d   1 2 3 4   i n   \d + m s / , 
75+       ) ; 
76+       return ; 
77+     } 
78+ 
79+     throw  new  Error ( '"GET /unknown" should not resolve' ) ; 
80+   } ) ; 
81+ 
82+   it ( "logs malformed error response for 'GET /unknown'" ,  async  ( )  =>  { 
83+     const  mock  =  fetchMock . sandbox ( ) . getOnce ( "https://api.github.com/unknown" ,  { 
84+       status : 500 , 
85+       body : "Internal Server Error" , 
86+     } ) ; 
87+ 
88+     const  mockLogInfo  =  jest . fn ( ) ; 
89+     const  mockErrorInfo  =  jest . fn ( ) ; 
90+     const  mockDebugInfo  =  jest . fn ( ) ; 
91+     const  MyOctokit  =  Octokit . plugin ( requestLog ) ; 
92+     const  octokit  =  new  MyOctokit ( { 
93+       request : { 
94+         fetch : mock , 
95+       } , 
96+       log : { 
97+         debug : mockDebugInfo , 
98+         info : mockLogInfo , 
99+         warn ( )  { } , 
100+         error : mockErrorInfo , 
54101      } , 
55102    } ) ; 
56103
57104    try  { 
58105      await  octokit . request ( "GET /unknown" ) ; 
59106    }  catch  ( error )  { 
60-       expect ( mockLogInfo . mock . calls [ 0 ] [ 0 ] ) . toMatch ( 
61-         / G E T   \/ u n k n o w n   -   4 0 4   i n   \d + m s / , 
107+       expect ( mockErrorInfo . mock . calls [ 0 ] [ 0 ] ) . toMatch ( 
108+         / G E T   \/ u n k n o w n   -   5 0 0   w i t h   i d   U N K N O W N   i n   \d + m s / , 
62109      ) ; 
63110      return ; 
64111    } 
0 commit comments