@@ -5,9 +5,6 @@ import * as errors from '../src/errors.js';
5
5
import { ScrapeConfig } from '../src/scrapeconfig.js' ;
6
6
import { describe , it , expect , jest , beforeEach } from '@jest/globals' ;
7
7
8
- jest . mock ( 'axios' ) ;
9
-
10
- const mockedAxios = axios as jest . Mocked < typeof axios > ;
11
8
12
9
function resultFactory ( params : {
13
10
url ?: string ;
@@ -39,7 +36,7 @@ describe('concurrent scrape', () => {
39
36
// mock axios to return /account data and 2 types of results:
40
37
// - success for /success endpoints
41
38
// - ASP failure for /failure endpoints
42
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
39
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
43
40
if ( config . url . includes ( '/account' ) ) {
44
41
return {
45
42
status : 200 ,
@@ -71,7 +68,7 @@ describe('concurrent scrape', () => {
71
68
} ) ;
72
69
73
70
beforeEach ( ( ) => {
74
- mockedAxios . request . mockClear ( ) ; // clear all mock meta on each test
71
+ jest . spyOn ( axios , ' request' ) . mockClear ( ) ; // clear all mock meta on each test
75
72
} ) ;
76
73
77
74
it ( 'success' , async ( ) => {
@@ -91,7 +88,7 @@ describe('concurrent scrape', () => {
91
88
expect ( results . length ) . toBe ( 5 ) ;
92
89
expect ( errors . length ) . toBe ( 5 ) ;
93
90
// 10 requests and 1 account info
94
- expect ( mockedAxios . request ) . toHaveBeenCalledTimes ( 11 ) ;
91
+ expect ( jest . spyOn ( axios , ' request' ) ) . toHaveBeenCalledTimes ( 11 ) ;
95
92
} , 5_000 ) ;
96
93
97
94
it ( 'success with explicit concurrency' , async ( ) => {
@@ -111,7 +108,7 @@ describe('concurrent scrape', () => {
111
108
expect ( results . length ) . toBe ( 5 ) ;
112
109
expect ( errors . length ) . toBe ( 5 ) ;
113
110
// 10 requests and 1 account info
114
- expect ( mockedAxios . request ) . toHaveBeenCalledTimes ( 10 ) ;
111
+ expect ( jest . spyOn ( axios , ' request' ) ) . toHaveBeenCalledTimes ( 10 ) ;
115
112
} , 2_000 ) ;
116
113
} ) ;
117
114
@@ -120,12 +117,12 @@ describe('scrape', () => {
120
117
const client = new ScrapflyClient ( { key : KEY } ) ;
121
118
122
119
beforeEach ( ( ) => {
123
- mockedAxios . request . mockClear ( ) ; // clear all mock meta on each test
120
+ jest . spyOn ( axios , ' request' ) . mockClear ( ) ; // clear all mock meta on each test
124
121
} ) ;
125
122
126
123
it ( 'GET success' , async ( ) => {
127
124
const url = 'https://httpbin.dev/json' ;
128
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
125
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
129
126
// Ensure the URL matches the pattern
130
127
expect ( config . url ) . toMatch ( client . HOST + '/scrape' ) ;
131
128
expect ( config . method ) . toEqual ( 'GET' ) ;
@@ -144,12 +141,12 @@ describe('scrape', () => {
144
141
expect ( result . context . asp ) . toBe ( false ) ;
145
142
expect ( result . uuid ) . toBe ( '1234' ) ;
146
143
// a single request:
147
- expect ( mockedAxios . request ) . toHaveBeenCalledTimes ( 1 ) ;
144
+ expect ( jest . spyOn ( axios , ' request' ) ) . toHaveBeenCalledTimes ( 1 ) ;
148
145
} ) ;
149
146
150
147
it ( 'GET complex urls' , async ( ) => {
151
148
const url = 'https://httpbin.dev/anything/?website=https://httpbin.dev/anything' ;
152
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
149
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
153
150
// Ensure the URL matches the pattern
154
151
expect ( config . url ) . toMatch ( client . HOST + '/scrape' ) ;
155
152
expect ( config . method ) . toEqual ( 'GET' ) ;
@@ -168,12 +165,12 @@ describe('scrape', () => {
168
165
expect ( result . context . asp ) . toBe ( false ) ;
169
166
expect ( result . uuid ) . toBe ( '1234' ) ;
170
167
// a single request:
171
- expect ( mockedAxios . request ) . toHaveBeenCalledTimes ( 1 ) ;
168
+ expect ( jest . spyOn ( axios , ' request' ) ) . toHaveBeenCalledTimes ( 1 ) ;
172
169
} ) ;
173
170
174
171
it ( 'POST success' , async ( ) => {
175
172
const url = 'https://httpbin.dev/json' ;
176
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
173
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
177
174
// Ensure the URL matches the pattern
178
175
expect ( config . url ) . toMatch ( client . HOST + '/scrape' ) ;
179
176
expect ( config . method ) . toEqual ( 'POST' ) ;
@@ -198,16 +195,17 @@ describe('scrape', () => {
198
195
expect ( result . config . url ) . toBe ( 'https://httpbin.dev/json' ) ;
199
196
expect ( result . context . asp ) . toBe ( false ) ;
200
197
expect ( result . uuid ) . toBe ( '1234' ) ;
201
- expect ( mockedAxios . request ) . toHaveBeenCalledTimes ( 1 ) ;
198
+ expect ( jest . spyOn ( axios , ' request' ) ) . toHaveBeenCalledTimes ( 1 ) ;
202
199
} ) ;
203
200
204
201
it ( 'unhandled errors propagate up' , async ( ) => {
202
+ jest . spyOn ( axios , 'request' ) . mockReset ( ) ;
205
203
const url = 'https://httpbin.dev/json' ;
206
- mockedAxios . request . mockImplementation ( ( ) => Promise . reject ( new Error ( 'Network Error' ) ) ) ;
204
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( ( ) => Promise . reject ( new Error ( 'Foo Error' ) ) ) ;
207
205
208
206
await expect ( async ( ) => {
209
207
await client . scrape ( new ScrapeConfig ( { url } ) ) ;
210
- } ) . rejects . toThrow ( 'Network Error' ) ;
208
+ } ) . rejects . toThrow ( 'Foo Error' ) ;
211
209
} ) ;
212
210
// it('handles ')
213
211
} ) ;
@@ -230,12 +228,12 @@ describe('client errors', () => {
230
228
const client = new ScrapflyClient ( { key : KEY } ) ;
231
229
232
230
beforeEach ( ( ) => {
233
- mockedAxios . request . mockClear ( ) ; // clear all mock meta on each test
231
+ jest . spyOn ( axios , ' request' ) . mockClear ( ) ; // clear all mock meta on each test
234
232
} ) ;
235
233
236
234
it ( 'raises ApiHttpServerError on 500 and success' , async ( ) => {
237
235
const url = 'https://httpbin.dev/json' ;
238
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
236
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
239
237
return resultFactory ( {
240
238
url : config . url ,
241
239
status_code : 500 ,
@@ -249,7 +247,7 @@ describe('client errors', () => {
249
247
250
248
it ( 'raises BadApiKeyError on 401' , async ( ) => {
251
249
const url = 'https://httpbin.dev/json' ;
252
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
250
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
253
251
return resultFactory ( {
254
252
url : config . url ,
255
253
status_code : 401 ,
@@ -262,7 +260,7 @@ describe('client errors', () => {
262
260
} ) ;
263
261
it ( 'raises TooManyRequests on 429 and success' , async ( ) => {
264
262
const url = 'https://httpbin.dev/json' ;
265
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
263
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
266
264
return resultFactory ( {
267
265
url : config . url ,
268
266
status_code : 429 ,
@@ -273,7 +271,7 @@ describe('client errors', () => {
273
271
await expect ( client . scrape ( new ScrapeConfig ( { url } ) ) ) . rejects . toThrow ( errors . TooManyRequests ) ;
274
272
} ) ;
275
273
it ( 'raises ScrapflyScrapeError on ::SCRAPE:: resource and success' , async ( ) => {
276
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
274
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
277
275
return resultFactory ( {
278
276
url : config . url ,
279
277
status : 'ERR::SCRAPE::BAD_PROTOCOL' ,
@@ -286,7 +284,7 @@ describe('client errors', () => {
286
284
} ) ;
287
285
288
286
it ( 'raises ScrapflyWebhookError on ::WEBHOOK:: resource and success' , async ( ) => {
289
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
287
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
290
288
return resultFactory ( {
291
289
url : config . url ,
292
290
status : 'ERR::WEBHOOK::DISABLED ' ,
@@ -298,7 +296,7 @@ describe('client errors', () => {
298
296
) ;
299
297
} ) ;
300
298
it ( 'raises ScrapflyProxyError on ERR::PROXY::POOL_NOT_FOUND resource and success' , async ( ) => {
301
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
299
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
302
300
return resultFactory ( {
303
301
url : config . url ,
304
302
status : 'ERR::PROXY::POOL_NOT_FOUND ' ,
@@ -311,7 +309,7 @@ describe('client errors', () => {
311
309
} ) ;
312
310
313
311
it ( 'raises ScrapflyScheduleError on ERR::SCHEDULE::DISABLED resource and success' , async ( ) => {
314
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
312
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
315
313
return resultFactory ( {
316
314
url : config . url ,
317
315
status : 'ERR::SCHEDULE::DISABLED' ,
@@ -324,7 +322,7 @@ describe('client errors', () => {
324
322
} ) ;
325
323
326
324
it ( 'raises ScrapflyAspError on ERR::ASP::SHIELD_ERROR resource and success' , async ( ) => {
327
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
325
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
328
326
return resultFactory ( {
329
327
url : config . url ,
330
328
status : 'ERR::ASP::SHIELD_ERROR' ,
@@ -337,7 +335,7 @@ describe('client errors', () => {
337
335
} ) ;
338
336
339
337
it ( 'raises ScrapflySessionError on ERR::SESSION::CONCURRENT_ACCESS resource and success' , async ( ) => {
340
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
338
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
341
339
return resultFactory ( {
342
340
url : config . url ,
343
341
status : 'ERR::SESSION::CONCURRENT_ACCESS' ,
@@ -350,7 +348,7 @@ describe('client errors', () => {
350
348
} ) ;
351
349
352
350
it ( 'raises ApiHttpClientError on success and unknown status' , async ( ) => {
353
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
351
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
354
352
return resultFactory ( {
355
353
url : config . url ,
356
354
status : 'ERR::NEW' ,
@@ -362,7 +360,7 @@ describe('client errors', () => {
362
360
) ;
363
361
} ) ;
364
362
it ( 'raises UpstreamHttpServerError on failure, ERR::SCRAPE::BAD_UPSTREAM_RESPONSE and >=500' , async ( ) => {
365
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
363
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
366
364
return resultFactory ( {
367
365
url : config . url ,
368
366
success : false ,
@@ -375,7 +373,7 @@ describe('client errors', () => {
375
373
) ;
376
374
} ) ;
377
375
it ( 'raises UpstreamHttpClientError on failure, ERR::SCRAPE::BAD_UPSTREAM_RESPONSE and 4xx status' , async ( ) => {
378
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
376
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
379
377
return resultFactory ( {
380
378
url : config . url ,
381
379
success : false ,
@@ -398,7 +396,7 @@ describe('client errors', () => {
398
396
SESSION : errors . ScrapflySessionError ,
399
397
} ;
400
398
for ( const [ resource , err ] of Object . entries ( resourceErrMap ) ) {
401
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
399
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
402
400
return resultFactory ( {
403
401
url : config . url ,
404
402
success : false ,
@@ -410,7 +408,7 @@ describe('client errors', () => {
410
408
} ) ;
411
409
412
410
it ( 'raises ScrapflyError on unhandled failure' , async ( ) => {
413
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
411
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
414
412
return resultFactory ( {
415
413
url : config . url ,
416
414
success : false ,
@@ -423,7 +421,7 @@ describe('client errors', () => {
423
421
) ;
424
422
} ) ;
425
423
it ( 'raises on unhandled failure' , async ( ) => {
426
- mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
424
+ jest . spyOn ( axios , ' request' ) . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
427
425
return resultFactory ( {
428
426
url : config . url ,
429
427
success : false ,
@@ -436,19 +434,19 @@ describe('client errors', () => {
436
434
) ;
437
435
} ) ;
438
436
it ( 'account retrieval status unhandled code (e.g. 404)' , async ( ) => {
439
- mockedAxios . request . mockRejectedValue ( {
437
+ jest . spyOn ( axios , ' request' ) . mockRejectedValue ( {
440
438
response : { status : 404 , data : { } } ,
441
439
} ) ;
442
440
await expect ( client . account ( ) ) . rejects . toThrow ( errors . HttpError ) ;
443
441
} ) ;
444
442
it ( 'account retrieval bad api key (status 401)' , async ( ) => {
445
- mockedAxios . request . mockRejectedValue ( {
443
+ jest . spyOn ( axios , ' request' ) . mockRejectedValue ( {
446
444
response : { status : 401 , data : { } } ,
447
445
} ) ;
448
446
await expect ( client . account ( ) ) . rejects . toThrow ( errors . BadApiKeyError ) ;
449
447
} ) ;
450
448
it ( 'scrape bad api key (status 401)' , async ( ) => {
451
- mockedAxios . request . mockRejectedValue ( {
449
+ jest . spyOn ( axios , ' request' ) . mockRejectedValue ( {
452
450
response : { status : 401 , data : { } } ,
453
451
} ) ;
454
452
await expect ( client . scrape ( new ScrapeConfig ( { url : 'https://httpbin.dev/json' } ) ) ) . rejects . toThrow (
0 commit comments