1
+ import { logger } from "../../logger" ;
1
2
import { APIError } from "../../parse" ;
2
3
import { retryOnAPIFailure } from "../../utils/retry" ;
3
4
import { mockConsoleMethods } from "../helpers/mock-console" ;
4
5
5
6
describe ( "retryOnAPIFailure" , ( ) => {
6
7
const std = mockConsoleMethods ( ) ;
7
8
9
+ beforeEach ( ( ) => {
10
+ const level = logger . loggerLevel ;
11
+ logger . loggerLevel = "debug" ;
12
+ return ( ) => ( logger . loggerLevel = level ) ;
13
+ } ) ;
14
+
8
15
it ( "should retry 5xx errors and succeed if the 3rd try succeeds" , async ( ) => {
9
16
let attempts = 0 ;
10
17
@@ -15,15 +22,13 @@ describe("retryOnAPIFailure", () => {
15
22
}
16
23
} ) ;
17
24
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
+ ]
27
32
` ) ;
28
33
} ) ;
29
34
@@ -37,16 +42,15 @@ describe("retryOnAPIFailure", () => {
37
42
} )
38
43
) . rejects . toMatchInlineSnapshot ( `[APIError: 500 error]` ) ;
39
44
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
+ ]
50
54
` ) ;
51
55
} ) ;
52
56
@@ -60,15 +64,7 @@ describe("retryOnAPIFailure", () => {
60
64
} )
61
65
) . rejects . toMatchInlineSnapshot ( `[APIError: 401 error]` ) ;
62
66
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 []` ) ;
72
68
} ) ;
73
69
74
70
it ( "should retry TypeError" , async ( ) => {
@@ -81,16 +77,12 @@ describe("retryOnAPIFailure", () => {
81
77
} )
82
78
) . rejects . toMatchInlineSnapshot ( `[TypeError: type error]` ) ;
83
79
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
+ ]
94
86
` ) ;
95
87
} ) ;
96
88
@@ -104,15 +96,7 @@ describe("retryOnAPIFailure", () => {
104
96
} )
105
97
) . rejects . toMatchInlineSnapshot ( `[Error: some error]` ) ;
106
98
expect ( attempts ) . toBe ( 1 ) ;
107
- expect ( std ) . toMatchInlineSnapshot ( `
108
- Object {
109
- "debug": "",
110
- "err": "",
111
- "info": "",
112
- "out": "",
113
- "warn": "",
114
- }
7F2
115
- ` ) ;
99
+ expect ( getRetryAndErrorLogs ( std . debug ) ) . toMatchInlineSnapshot ( `Array []` ) ;
116
100
} ) ;
117
101
118
102
it ( "should retry custom APIError implementation with non-5xx error" , async ( ) => {
@@ -134,16 +118,21 @@ describe("retryOnAPIFailure", () => {
134
118
) . rejects . toMatchInlineSnapshot ( `[CustomAPIError: 401 error]` ) ;
135
119
expect ( attempts ) . toBe ( 3 ) ;
136
120
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
+ ex
98A7
pect ( 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
+ ]
147
130
` ) ;
148
131
} ) ;
149
132
} ) ;
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