1
1
import axios from 'axios' ;
2
+ import { AxiosRequestConfig } from 'axios' ;
2
3
import { ScrapflyClient } from '../src/client.js' ;
3
4
import * as errors from '../src/errors.js' ;
4
5
import { ScrapeConfig } from '../src/scrapeconfig.js' ;
6
+ import { describe , it , expect , jest , beforeEach } from '@jest/globals' ;
5
7
6
8
jest . mock ( 'axios' ) ;
7
9
@@ -37,7 +39,7 @@ describe('concurrent scrape', () => {
37
39
// mock axios to return /account data and 2 types of results:
38
40
// - success for /success endpoints
39
41
// - ASP failure for /failure endpoints
40
- mockedAxios . request . mockImplementation ( async ( config ) => {
42
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
41
43
if ( config . url . includes ( '/account' ) ) {
42
44
return {
43
45
status : 200 ,
@@ -123,7 +125,7 @@ describe('scrape', () => {
123
125
124
126
it ( 'GET success' , async ( ) => {
125
127
const url = 'https://httpbin.dev/json' ;
126
- mockedAxios . request . mockImplementation ( async ( config ) => {
128
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
127
129
// Ensure the URL matches the pattern
128
130
expect ( config . url ) . toMatch ( client . HOST + '/scrape' ) ;
129
131
expect ( config . method ) . toEqual ( 'GET' ) ;
@@ -147,7 +149,7 @@ describe('scrape', () => {
147
149
148
150
it ( 'GET complex urls' , async ( ) => {
149
151
const url = 'https://httpbin.dev/anything/?website=https://httpbin.dev/anything' ;
150
- mockedAxios . request . mockImplementation ( async ( config ) => {
152
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
151
153
// Ensure the URL matches the pattern
152
154
expect ( config . url ) . toMatch ( client . HOST + '/scrape' ) ;
153
155
expect ( config . method ) . toEqual ( 'GET' ) ;
@@ -169,11 +171,9 @@ describe('scrape', () => {
169
171
expect ( mockedAxios . request ) . toHaveBeenCalledTimes ( 1 ) ;
170
172
} ) ;
171
173
172
-
173
-
174
174
it ( 'POST success' , async ( ) => {
175
175
const url = 'https://httpbin.dev/json' ;
176
- mockedAxios . request . mockImplementation ( async ( config ) => {
176
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
177
177
// Ensure the URL matches the pattern
178
178
expect ( config . url ) . toMatch ( client . HOST + '/scrape' ) ;
179
179
expect ( config . method ) . toEqual ( 'POST' ) ;
@@ -235,7 +235,7 @@ describe('client errors', () => {
235
235
236
236
it ( 'raises ApiHttpServerError on 500 and success' , async ( ) => {
237
237
const url = 'https://httpbin.dev/json' ;
238
- mockedAxios . request . mockImplementation ( async ( config ) => {
238
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
239
239
return resultFactory ( {
240
240
url : config . url ,
241
241
status_code : 500 ,
@@ -249,7 +249,7 @@ describe('client errors', () => {
249
249
250
250
it ( 'raises BadApiKeyError on 401' , async ( ) => {
251
251
const url = 'https://httpbin.dev/json' ;
252
- mockedAxios . request . mockImplementation ( async ( config ) => {
252
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
253
253
return resultFactory ( {
254
254
url : config . url ,
255
255
status_code : 401 ,
@@ -262,7 +262,7 @@ describe('client errors', () => {
262
262
} ) ;
263
263
it ( 'raises TooManyRequests on 429 and success' , async ( ) => {
264
264
const url = 'https://httpbin.dev/json' ;
265
- mockedAxios . request . mockImplementation ( async ( config ) => {
265
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
266
266
return resultFactory ( {
267
267
url : config . url ,
268
268
status_code : 429 ,
@@ -273,7 +273,7 @@ describe('client errors', () => {
273
273
await expect ( client . scrape ( new ScrapeConfig ( { url } ) ) ) . rejects . toThrow ( errors . TooManyRequests ) ;
274
274
} ) ;
275
275
it ( 'raises ScrapflyScrapeError on ::SCRAPE:: resource and success' , async ( ) => {
276
- mockedAxios . request . mockImplementation ( async ( config ) => {
276
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
277
277
return resultFactory ( {
278
278
url : config . url ,
279
279
status : 'ERR::SCRAPE::BAD_PROTOCOL' ,
@@ -286,7 +286,7 @@ describe('client errors', () => {
286
286
} ) ;
287
287
288
288
it ( 'raises ScrapflyWebhookError on ::WEBHOOK:: resource and success' , async ( ) => {
289
- mockedAxios . request . mockImplementation ( async ( config ) => {
289
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
290
290
return resultFactory ( {
291
291
url : config . url ,
292
292
status : 'ERR::WEBHOOK::DISABLED ' ,
@@ -298,7 +298,7 @@ describe('client errors', () => {
298
298
) ;
299
299
} ) ;
300
300
it ( 'raises ScrapflyProxyError on ERR::PROXY::POOL_NOT_FOUND resource and success' , async ( ) => {
301
- mockedAxios . request . mockImplementation ( async ( config ) => {
301
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
302
302
return resultFactory ( {
303
303
url : config . url ,
304
304
status : 'ERR::PROXY::POOL_NOT_FOUND ' ,
@@ -311,7 +311,7 @@ describe('client errors', () => {
311
311
} ) ;
312
312
313
313
it ( 'raises ScrapflyScheduleError on ERR::SCHEDULE::DISABLED resource and success' , async ( ) => {
314
- mockedAxios . request . mockImplementation ( async ( config ) => {
314
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
315
315
return resultFactory ( {
316
316
url : config . url ,
317
317
status : 'ERR::SCHEDULE::DISABLED' ,
@@ -324,7 +324,7 @@ describe('client errors', () => {
324
324
} ) ;
325
325
326
326
it ( 'raises ScrapflyAspError on ERR::ASP::SHIELD_ERROR resource and success' , async ( ) => {
327
- mockedAxios . request . mockImplementation ( async ( config ) => {
327
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
328
328
return resultFactory ( {
329
329
url : config . url ,
330
330
status : 'ERR::ASP::SHIELD_ERROR' ,
@@ -337,7 +337,7 @@ describe('client errors', () => {
337
337
} ) ;
338
338
339
339
it ( 'raises ScrapflySessionError on ERR::SESSION::CONCURRENT_ACCESS resource and success' , async ( ) => {
340
- mockedAxios . request . mockImplementation ( async ( config ) => {
340
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
341
341
return resultFactory ( {
342
342
url : config . url ,
343
343
status : 'ERR::SESSION::CONCURRENT_ACCESS' ,
@@ -350,7 +350,7 @@ describe('client errors', () => {
350
350
} ) ;
351
351
352
352
it ( 'raises ApiHttpClientError on success and unknown status' , async ( ) => {
353
- mockedAxios . request . mockImplementation ( async ( config ) => {
353
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
354
354
return resultFactory ( {
355
355
url : config . url ,
356
356
status : 'ERR::NEW' ,
@@ -362,7 +362,7 @@ describe('client errors', () => {
362
362
) ;
363
363
} ) ;
364
364
it ( 'raises UpstreamHttpServerError on failure, ERR::SCRAPE::BAD_UPSTREAM_RESPONSE and >=500' , async ( ) => {
365
- mockedAxios . request . mockImplementation ( async ( config ) => {
365
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
366
366
return resultFactory ( {
367
367
url : config . url ,
368
368
success : false ,
@@ -375,7 +375,7 @@ describe('client errors', () => {
375
375
) ;
376
376
} ) ;
377
377
it ( 'raises UpstreamHttpClientError on failure, ERR::SCRAPE::BAD_UPSTREAM_RESPONSE and 4xx status' , async ( ) => {
378
- mockedAxios . request . mockImplementation ( async ( config ) => {
378
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
379
379
return resultFactory ( {
380
380
url : config . url ,
381
381
success : false ,
@@ -398,7 +398,7 @@ describe('client errors', () => {
398
398
SESSION : errors . ScrapflySessionError ,
399
399
} ;
400
400
for ( const [ resource , err ] of Object . entries ( resourceErrMap ) ) {
401
- mockedAxios . request . mockImplementation ( async ( config ) => {
401
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
402
402
return resultFactory ( {
403
403
url : config . url ,
404
404
success : false ,
@@ -410,7 +410,7 @@ describe('client errors', () => {
410
410
} ) ;
411
411
412
412
it ( 'raises ScrapflyError on unhandled failure' , async ( ) => {
413
- mockedAxios . request . mockImplementation ( async ( config ) => {
413
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
414
414
return resultFactory ( {
415
415
url : config . url ,
416
416
success : false ,
@@ -423,7 +423,7 @@ describe('client errors', () => {
423
423
) ;
424
424
} ) ;
425
425
it ( 'raises on unhandled failure' , async ( ) => {
426
- mockedAxios . request . mockImplementation ( async ( config ) => {
426
+ mockedAxios . request . mockImplementation ( async ( config : AxiosRequestConfig ) : Promise < any > => {
427
427
return resultFactory ( {
428
428
url : config . url ,
429
429
success : false ,
0 commit comments