1+ import { logger } from "../../logger" ;
12import { APIError } from "../../parse" ;
23import { retryOnAPIFailure } from "../../utils/retry" ;
34import { mockConsoleMethods } from "../helpers/mock-console" ;
45
56describe ( "retryOnAPIFailure" , ( ) => {
67 const std = mockConsoleMethods ( ) ;
78
9+ beforeEach ( ( ) => {
10+ const level = logger . loggerLevel ;
11+ logger . loggerLevel = "debug" ;
12+ return ( ) => ( logger . loggerLevel = level ) ;
13+ } ) ;
14+
815 it ( "should retry 5xx errors and succeed if the 3rd try succeeds" , async ( ) => {
916 let attempts = 0 ;
1017
@@ -15,15 +22,13 @@ describe("retryOnAPIFailure", () => {
1522 }
1623 } ) ;
1724 expect ( attempts ) . toBe ( 3 ) ;
18- expect ( std ) . toMatchInlineSnapshot ( `
19- Object {
20- "debug": "",
21- "err": "",
22- "info": "Retrying API call after error...
23- Retrying API call after error...",
24- "out": "",
25- "warn": "",
26- }
25+ expect ( getRetryAndErrorLogs ( std . debug ) ) . toMatchInlineSnapshot ( `
26+ Array [
27+ "Retrying API call after error...",
28+ "APIError: 500 error",
29+ "Retrying API call after error...",
30+ "APIError: 500 error",
31+ ]
2732 ` ) ;
2833 } ) ;
2934
@@ -37,16 +42,15 @@ describe("retryOnAPIFailure", () => {
3742 } )
3843 ) . rejects . toMatchInlineSnapshot ( `[APIError: 500 error]` ) ;
3944 expect ( attempts ) . toBe ( 3 ) ;
40- expect ( std ) . toMatchInlineSnapshot ( `
41- Object {
42- "debug": "",
43- "err": "",
44- "info": "Retrying API call after error...
45- Retrying API call after error...
46- Retrying API call after error...",
47- "out": "",
48- "warn": "",
49- }
45+ expect ( getRetryAndErrorLogs ( std . debug ) ) . toMatchInlineSnapshot ( `
46+ Array [
47+ "Retrying API call after error...",
48+ "APIError: 500 error",
49+ "Retrying API call after error...",
50+ "APIError: 500 error",
51+ "Retrying API call after error...",
52+ "APIError: 500 error",
53+ ]
5054 ` ) ;
5155 } ) ;
5256
@@ -60,15 +64,7 @@ describe("retryOnAPIFailure", () => {
6064 } )
6165 ) . rejects . toMatchInlineSnapshot ( `[APIError: 401 error]` ) ;
6266 expect ( attempts ) . toBe ( 1 ) ;
63- expect ( std ) . toMatchInlineSnapshot ( `
64- Object {
65- "debug": "",
66- "err": "",
67- "info": "",
68- "out": "",
69- "warn": "",
70- }
71- ` ) ;
67+ expect ( getRetryAndErrorLogs ( std . debug ) ) . toMatchInlineSnapshot ( `Array []` ) ;
7268 } ) ;
7369
7470 it ( "should retry TypeError" , async ( ) => {
@@ -81,16 +77,12 @@ describe("retryOnAPIFailure", () => {
8177 } )
8278 ) . rejects . toMatchInlineSnapshot ( `[TypeError: type error]` ) ;
8379 expect ( attempts ) . toBe ( 3 ) ;
84- expect ( std ) . toMatchInlineSnapshot ( `
85- Object {
86- "debug": "",
87- "err": "",
88- "info": "Retrying API call after error...
89- Retrying API call after error...
90- Retrying API call after error...",
91- "out": "",
92- "warn": "",
93- }
80+ expect ( getRetryAndErrorLogs ( std . debug ) ) . toMatchInlineSnapshot ( `
81+ Array [
82+ "Retrying API call after error...",
83+ "Retrying API call after error...",
84+ "Retrying API call after error...",
85+ ]
9486 ` ) ;
9587 } ) ;
9688
@@ -104,15 +96,7 @@ describe("retryOnAPIFailure", () => {
10496 } )
10597 ) . rejects . toMatchInlineSnapshot ( `[Error: some error]` ) ;
10698 expect ( attempts ) . toBe ( 1 ) ;
107- expect ( std ) . toMatchInlineSnapshot ( `
108- Object {
109- "debug": "",
110- "err": "",
111- "info": "",
112- "out": "",
113- "warn": "",
114- }
115- ` ) ;
99+ expect ( getRetryAndErrorLogs ( std . debug ) ) . toMatchInlineSnapshot ( `Array []` ) ;
116100 } ) ;
117101
118102 it ( "should retry custom APIError implementation with non-5xx error" , async ( ) => {
@@ -134,16 +118,21 @@ describe("retryOnAPIFailure", () => {
134118 ) . rejects . toMatchInlineSnapshot ( `[CustomAPIError: 401 error]` ) ;
135119 expect ( attempts ) . toBe ( 3 ) ;
136120 expect ( checkedCustomIsRetryable ) . toBe ( true ) ;
137- expect ( std ) . toMatchInlineSnapshot ( `
138- Object {
139- "debug": "",
140- "err": "",
141- "info": "Retrying API call after error...
142- Retrying API call after error...
143- Retrying API call after error...",
144- "out": "",
145- "warn": "",
146- }
121+ expect ( getRetryAndErrorLogs ( std . debug ) ) . toMatchInlineSnapshot ( `
122+ Array [
123+ "Retrying API call after error...",
124+ "CustomAPIError: 401 error",
125+ "Retrying API call after error...",
126+ "CustomAPIError: 401 error",
127+ "Retrying API call after error...",
128+ "CustomAPIError: 401 error",
129+ ]
147130 ` ) ;
148131 } ) ;
149132} ) ;
133+
134+ function getRetryAndErrorLogs ( debugOutput : string ) : string [ ] {
135+ return debugOutput
136+ . split ( "\n" )
137+ . filter ( ( line ) => line . includes ( "Retrying" ) || line . includes ( "APIError" ) ) ;
138+ }
0 commit comments