1
+ import { jest } from "@jest/globals"
1
2
import {
2
3
BackendClient ,
3
4
BackendClientOpts ,
@@ -50,7 +51,9 @@ describe("BackendClient", () => {
50
51
url : "https://api.pipedream.com/v1/test-path" ,
51
52
} ,
52
53
response : {
53
- json : { data : "test-response" } ,
54
+ json : {
55
+ data : "test-response" ,
56
+ } ,
54
57
} ,
55
58
} )
56
59
@@ -72,7 +75,9 @@ describe("BackendClient", () => {
72
75
} ,
73
76
} ,
74
77
response : {
75
- json : { success : true } ,
78
+ json : {
79
+ success : true ,
80
+ } ,
76
81
} ,
77
82
} )
78
83
@@ -116,7 +121,9 @@ describe("BackendClient", () => {
116
121
} ,
117
122
} ,
118
123
response : {
119
- json : { success : true } ,
124
+ json : {
125
+ success : true ,
126
+ } ,
120
127
} ,
121
128
} )
122
129
@@ -144,7 +151,9 @@ describe("BackendClient", () => {
144
151
} ,
145
152
} ,
146
153
response : {
147
- json : { success : true } ,
154
+ json : {
155
+ success : true ,
156
+ } ,
148
157
} ,
149
158
} )
150
159
@@ -311,10 +320,12 @@ describe("BackendClient", () => {
311
320
url : `https://api.pipedream.com/v1/connect/${ projectId } /accounts?app=app-1` ,
312
321
} ,
313
322
response : {
314
- json : [ {
315
- id : "account-1" ,
316
- name : "Test Account" ,
317
- } ] ,
323
+ json : [
324
+ {
325
+ id : "account-1" ,
326
+ name : "Test Account" ,
327
+ } ,
328
+ ] ,
318
329
} ,
319
330
} ) ;
320
331
@@ -339,10 +350,12 @@ describe("BackendClient", () => {
339
350
url : `https://api.pipedream.com/v1/connect/${ projectId } /accounts?external_user_id=external-id-1` ,
340
351
} ,
341
352
response : {
342
- json : [ {
343
- id : "account-1" ,
344
- name : "Test Account" ,
345
- } ] ,
353
+ json : [
354
+ {
355
+ id : "account-1" ,
356
+ name : "Test Account" ,
357
+ } ,
358
+ ] ,
346
359
} ,
347
360
} ) ;
348
361
@@ -464,7 +477,9 @@ describe("BackendClient", () => {
464
477
} ,
465
478
} ,
466
479
response : {
467
- json : { result : "workflow-response" } ,
480
+ json : {
481
+ result : "workflow-response" ,
482
+ } ,
468
483
} ,
469
484
} )
470
485
@@ -480,8 +495,10 @@ describe("BackendClient", () => {
480
495
} ) ;
481
496
482
497
it ( "should invoke a workflow with OAuth auth type" , async ( ) => {
483
- const token = "" + Math . random ( )
484
- fetchMock . expectAccessTokenSuccess ( { accessToken : token } ) ;
498
+ const token = "" + Math . random ( )
499
+ fetchMock . expectAccessTokenSuccess ( {
500
+ accessToken : token ,
501
+ } ) ;
485
502
fetchMock . expect ( {
486
503
request : {
487
504
url : "https://example.com/workflow" ,
@@ -490,7 +507,9 @@ describe("BackendClient", () => {
490
507
} ,
491
508
} ,
492
509
response : {
493
- json : { result : "workflow-response" } ,
510
+ json : {
511
+ result : "workflow-response" ,
512
+ } ,
494
513
} ,
495
514
} )
496
515
@@ -502,7 +521,7 @@ describe("BackendClient", () => {
502
521
} ) ;
503
522
504
523
it ( "should invoke a workflow with static bearer auth type" , async ( ) => {
505
- const token = "" + Math . random ( )
524
+ const token = "" + Math . random ( )
506
525
fetchMock . expect ( {
507
526
request : {
508
527
url : "https://example.com/workflow" ,
@@ -511,7 +530,9 @@ describe("BackendClient", () => {
511
530
} ,
512
531
} ,
513
532
response : {
514
- json : { result : "workflow-response" } ,
533
+ json : {
534
+ result : "workflow-response" ,
535
+ } ,
515
536
} ,
516
537
} )
517
538
@@ -537,7 +558,9 @@ describe("BackendClient", () => {
537
558
url : "https://api.pipedream.com/v1/test-path" ,
538
559
} ,
539
560
response : {
540
- json : { success : true } ,
561
+ json : {
562
+ success : true ,
563
+ } ,
541
564
} ,
542
565
} )
543
566
@@ -570,7 +593,9 @@ describe("BackendClient", () => {
570
593
} ,
571
594
} ,
572
595
response : {
573
- json : { result : "workflow-response" } ,
596
+ json : {
597
+ result : "workflow-response" ,
598
+ } ,
574
599
} ,
575
600
} )
576
601
@@ -696,17 +721,17 @@ describe("BackendClient", () => {
696
721
type ExpectRequest = {
697
722
method ?: string
698
723
url ?: string | RegExp
699
- json ?: Record < string , any >
724
+ json ?: Record < string , unknown >
700
725
headersContaining ?: Record < string , string >
701
726
}
702
727
type MockResponse =
703
728
| Response
704
- | { status ?: number ; json ?: any }
729
+ | { status ?: number ; json ?: unknown }
705
730
type IfOpts = {
706
731
method : string
707
732
url : string
708
733
headers : Record < string , string > // NonNullable<RequestInit["headers"]>
709
- json ?: any // body json
734
+ json ?: unknown // body json
710
735
// XXX etc.
711
736
}
712
737
function setupFetchMock ( ) {
@@ -715,7 +740,7 @@ function setupFetchMock() {
715
740
response : ( ) => Response
716
741
} [ ] = [ ]
717
742
718
- const jsonResponse = ( o : any , opts ?: { status ?: number } ) => {
743
+ const jsonResponse = ( o : unknown , opts ?: { status ?: number } ) => {
719
744
return new Response ( JSON . stringify ( o ) , {
720
745
status : opts ?. status ,
721
746
headers : {
@@ -726,17 +751,27 @@ function setupFetchMock() {
726
751
727
752
beforeEach ( ( ) => {
728
753
intercepts = [ ] ;
729
- jest . spyOn ( global , "fetch" ) . mockImplementation ( jest . fn ( ( url : string , init : RequestInit ) => {
730
- let json : any
731
- if ( init . body && typeof init . body === "string" ) {
754
+ // without these generics this fails typecheck and can't figure out why
755
+ jest . spyOn < any , any , any > ( global , "fetch" ) . mockImplementation ( jest . fn < typeof fetch > ( async ( ...args : Parameters < typeof fetch > ) => { // eslint-disable-line @typescript-eslint/no-explicit-any
756
+ const [
757
+ url ,
758
+ init ,
759
+ ] = args
760
+ let json : unknown
761
+ if ( init ?. body && typeof init . body === "string" ) {
732
762
try {
733
763
json = JSON . parse ( init . body )
734
- } catch { }
764
+ } catch {
765
+ // pass
766
+ }
767
+ }
768
+ if ( url instanceof Request ) {
769
+ throw new Error ( "not supported" )
735
770
}
736
771
const ifOpts : IfOpts = {
737
- method : init . method || "GET" ,
738
- url,
739
- headers : init . headers as Record < string , string > || { } ,
772
+ method : init ? .method || "GET" ,
773
+ url : url . toString ( ) ,
774
+ headers : init ? .headers as Record < string , string > || { } ,
740
775
json,
741
776
}
742
777
for ( let i = 0 ; i < intercepts . length ; i ++ ) {
@@ -758,7 +793,9 @@ function setupFetchMock() {
758
793
759
794
// const _expect = (opts: { if: (opts: IfOpts) => boolean, jsonResponse?: any, response?: Response }) => {
760
795
const _expect = ( opts : { request : ExpectRequest , response : MockResponse } ) => {
761
- const { method, url, headersContaining, json } = opts . request
796
+ const {
797
+ method, url, headersContaining, json,
798
+ } = opts . request
762
799
intercepts . push ( {
763
800
if : ( ifOpts ) => {
764
801
if ( method && ifOpts . method !== method ) return false
@@ -800,7 +837,7 @@ function setupFetchMock() {
800
837
}
801
838
802
839
const expectAccessTokenSuccess = ( opts ?: { accessToken ?: string ; expiresIn ?: number } ) => {
803
- const accessToken = opts ?. accessToken || "" + Math . random ( )
840
+ const accessToken = opts ?. accessToken || "" + Math . random ( )
804
841
_expect ( {
805
842
request : {
806
843
url : / \/ v 1 \/ o a u t h \/ t o k e n $ / ,
0 commit comments