1+ import { jest } from "@jest/globals"
12import {
23 BackendClient ,
34 BackendClientOpts ,
@@ -50,7 +51,9 @@ describe("BackendClient", () => {
5051 url : "https://api.pipedream.com/v1/test-path" ,
5152 } ,
5253 response : {
53- json : { data : "test-response" } ,
54+ json : {
55+ data : "test-response" ,
56+ } ,
5457 } ,
5558 } )
5659
@@ -72,7 +75,9 @@ describe("BackendClient", () => {
7275 } ,
7376 } ,
7477 response : {
75- json : { success : true } ,
78+ json : {
79+ success : true ,
80+ } ,
7681 } ,
7782 } )
7883
@@ -116,7 +121,9 @@ describe("BackendClient", () => {
116121 } ,
117122 } ,
118123 response : {
119- json : { success : true } ,
124+ json : {
125+ success : true ,
126+ } ,
120127 } ,
121128 } )
122129
@@ -144,7 +151,9 @@ describe("BackendClient", () => {
144151 } ,
145152 } ,
146153 response : {
147- json : { success : true } ,
154+ json : {
155+ success : true ,
156+ } ,
148157 } ,
149158 } )
150159
@@ -311,10 +320,12 @@ describe("BackendClient", () => {
311320 url : `https://api.pipedream.com/v1/connect/${ projectId } /accounts?app=app-1` ,
312321 } ,
313322 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+ ] ,
318329 } ,
319330 } ) ;
320331
@@ -339,10 +350,12 @@ describe("BackendClient", () => {
339350 url : `https://api.pipedream.com/v1/connect/${ projectId } /accounts?external_user_id=external-id-1` ,
340351 } ,
341352 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+ ] ,
346359 } ,
347360 } ) ;
348361
@@ -464,7 +477,9 @@ describe("BackendClient", () => {
464477 } ,
465478 } ,
466479 response : {
467- json : { result : "workflow-response" } ,
480+ json : {
481+ result : "workflow-response" ,
482+ } ,
468483 } ,
469484 } )
470485
@@ -480,8 +495,10 @@ describe("BackendClient", () => {
480495 } ) ;
481496
482497 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+ } ) ;
485502 fetchMock . expect ( {
486503 request : {
487504 url : "https://example.com/workflow" ,
@@ -490,7 +507,9 @@ describe("BackendClient", () => {
490507 } ,
491508 } ,
492509 response : {
493- json : { result : "workflow-response" } ,
510+ json : {
511+ result : "workflow-response" ,
512+ } ,
494513 } ,
495514 } )
496515
@@ -502,7 +521,7 @@ describe("BackendClient", () => {
502521 } ) ;
503522
504523 it ( "should invoke a workflow with static bearer auth type" , async ( ) => {
505- const token = "" + Math . random ( )
524+ const token = "" + Math . random ( )
506525 fetchMock . expect ( {
507526 request : {
508527 url : "https://example.com/workflow" ,
@@ -511,7 +530,9 @@ describe("BackendClient", () => {
511530 } ,
512531 } ,
513532 response : {
514- json : { result : "workflow-response" } ,
533+ json : {
534+ result : "workflow-response" ,
535+ } ,
515536 } ,
516537 } )
517538
@@ -537,7 +558,9 @@ describe("BackendClient", () => {
537558 url : "https://api.pipedream.com/v1/test-path" ,
538559 } ,
539560 response : {
540- json : { success : true } ,
561+ json : {
562+ success : true ,
563+ } ,
541564 } ,
542565 } )
543566
@@ -570,7 +593,9 @@ describe("BackendClient", () => {
570593 } ,
571594 } ,
572595 response : {
573- json : { result : "workflow-response" } ,
596+ json : {
597+ result : "workflow-response" ,
598+ } ,
574599 } ,
575600 } )
576601
@@ -696,17 +721,17 @@ describe("BackendClient", () => {
696721type ExpectRequest = {
697722 method ?: string
698723 url ?: string | RegExp
699- json ?: Record < string , any >
724+ json ?: Record < string , unknown >
700725 headersContaining ?: Record < string , string >
701726}
702727type MockResponse =
703728 | Response
704- | { status ?: number ; json ?: any }
729+ | { status ?: number ; json ?: unknown }
705730type IfOpts = {
706731 method : string
707732 url : string
708733 headers : Record < string , string > // NonNullable<RequestInit["headers"]>
709- json ?: any // body json
734+ json ?: unknown // body json
710735 // XXX etc.
711736}
712737function setupFetchMock ( ) {
@@ -715,7 +740,7 @@ function setupFetchMock() {
715740 response : ( ) => Response
716741 } [ ] = [ ]
717742
718- const jsonResponse = ( o : any , opts ?: { status ?: number } ) => {
743+ const jsonResponse = ( o : unknown , opts ?: { status ?: number } ) => {
719744 return new Response ( JSON . stringify ( o ) , {
720745 status : opts ?. status ,
721746 headers : {
@@ -726,17 +751,27 @@ function setupFetchMock() {
726751
727752 beforeEach ( ( ) => {
728753 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" ) {
732762 try {
733763 json = JSON . parse ( init . body )
734- } catch { }
764+ } catch {
765+ // pass
766+ }
767+ }
768+ if ( url instanceof Request ) {
769+ throw new Error ( "not supported" )
735770 }
736771 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 > || { } ,
740775 json,
741776 }
742777 for ( let i = 0 ; i < intercepts . length ; i ++ ) {
@@ -758,7 +793,9 @@ function setupFetchMock() {
758793
759794 // const _expect = (opts: { if: (opts: IfOpts) => boolean, jsonResponse?: any, response?: Response }) => {
760795 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
762799 intercepts . push ( {
763800 if : ( ifOpts ) => {
764801 if ( method && ifOpts . method !== method ) return false
@@ -800,7 +837,7 @@ function setupFetchMock() {
800837 }
801838
802839 const expectAccessTokenSuccess = ( opts ?: { accessToken ?: string ; expiresIn ?: number } ) => {
803- const accessToken = opts ?. accessToken || "" + Math . random ( )
840+ const accessToken = opts ?. accessToken || "" + Math . random ( )
804841 _expect ( {
805842 request : {
806843 url : / \/ v 1 \/ o a u t h \/ t o k e n $ / ,
0 commit comments