@@ -5,6 +5,8 @@ import { Signer, Credentials, DateUtils } from '@aws-amplify/core';
55
66jest . mock ( 'axios' ) ;
77
8+ const mockAxios = jest . spyOn ( axios as any , 'default' ) ;
9+
810axios . CancelToken = < CancelTokenStatic > {
911 source : ( ) => ( { token : null , cancel : null } ) ,
1012} ;
@@ -28,6 +30,7 @@ const config = {
2830
2931afterEach ( ( ) => {
3032 jest . restoreAllMocks ( ) ;
33+ mockAxios . mockClear ( ) ;
3134} ) ;
3235
3336describe ( 'Rest API test' , ( ) => {
@@ -166,19 +169,11 @@ describe('Rest API test', () => {
166169 api . configure ( custom_config ) ;
167170 const spyon = jest
168171 . spyOn ( Credentials , 'get' )
169- . mockImplementationOnce ( ( ) => {
170- return new Promise ( ( res , rej ) => {
171- res ( 'cred' ) ;
172- } ) ;
173- } ) ;
172+ . mockResolvedValueOnce ( 'cred' ) ;
174173
175174 const spyonRequest = jest
176175 . spyOn ( RestClient . prototype as any , '_request' )
177- . mockImplementationOnce ( ( ) => {
178- return new Promise ( ( res , rej ) => {
179- res ( { } ) ;
180- } ) ;
181- } ) ;
176+ . mockResolvedValueOnce ( { } ) ;
182177 await api . get ( 'apiName' , 'path' , { } ) ;
183178
184179 expect ( spyonRequest ) . toBeCalledWith (
@@ -221,25 +216,15 @@ describe('Rest API test', () => {
221216 session_token : 'token' ,
222217 } ;
223218
224- const spyon = jest . spyOn ( Credentials , 'get' ) . mockImplementation ( ( ) => {
225- return new Promise ( ( res , rej ) => {
226- res ( creds ) ;
227- } ) ;
228- } ) ;
219+ const spyon = jest . spyOn ( Credentials , 'get' ) . mockResolvedValue ( creds ) ;
229220
230221 const spyonSigner = jest
231222 . spyOn ( Signer , 'sign' )
232223 . mockImplementationOnce ( ( ) => {
233224 return { headers : { } } ;
234225 } ) ;
235226
236- const spyAxios = jest
237- . spyOn ( axios as any , 'default' )
238- . mockImplementationOnce ( ( ) => {
239- return new Promise ( ( res , rej ) => {
240- res ( resp ) ;
241- } ) ;
242- } ) ;
227+ mockAxios . mockResolvedValue ( resp ) ;
243228
244229 const init = {
245230 timeout : 2500 ,
@@ -297,13 +282,7 @@ describe('Rest API test', () => {
297282 return { headers : { } } ;
298283 } ) ;
299284
300- const spyAxios = jest
301- . spyOn ( axios as any , 'default' )
302- . mockImplementationOnce ( ( ) => {
303- return new Promise ( ( res , rej ) => {
304- res ( resp ) ;
305- } ) ;
306- } ) ;
285+ mockAxios . mockResolvedValue ( resp ) ;
307286
308287 const init = {
309288 queryStringParameters : {
@@ -363,13 +342,7 @@ describe('Rest API test', () => {
363342 return { headers : { } } ;
364343 } ) ;
365344
366- const spyAxios = jest
367- . spyOn ( axios as any , 'default' )
368- . mockImplementationOnce ( ( ) => {
369- return new Promise ( ( res , rej ) => {
370- res ( resp ) ;
371- } ) ;
372- } ) ;
345+ mockAxios . mockResolvedValue ( resp ) ;
373346
374347 const init = {
375348 queryStringParameters : {
@@ -429,13 +402,7 @@ describe('Rest API test', () => {
429402 return { headers : { } } ;
430403 } ) ;
431404
432- const spyAxios = jest
433- . spyOn ( axios as any , 'default' )
434- . mockImplementationOnce ( ( ) => {
435- return new Promise ( ( res , rej ) => {
436- res ( resp ) ;
437- } ) ;
438- } ) ;
405+ mockAxios . mockResolvedValue ( resp ) ;
439406
440407 const init = {
441408 queryStringParameters : {
@@ -557,19 +524,31 @@ describe('Rest API test', () => {
557524 . mockImplementation ( ( ) => 'endpoint' ) ;
558525
559526 jest . spyOn ( Credentials , 'get' ) . mockResolvedValue ( 'creds' ) ;
560-
561- jest
562- . spyOn ( RestClient . prototype as any , '_signed' )
563- . mockRejectedValueOnce ( normalError ) ;
527+ jest . spyOn ( RestClient . prototype as any , '_sign' ) . mockReturnValue ( {
528+ ...init ,
529+ headers : { ...init . headers , Authorization : 'signed' } ,
530+ } ) ;
531+ mockAxios . mockImplementationOnce ( ( ) => {
532+ return new Promise ( ( _ , rej ) => {
533+ rej ( normalError ) ;
534+ } ) ;
535+ } ) ;
564536
565537 await expect ( api . post ( 'url' , 'path' , init ) ) . rejects . toThrow ( normalError ) ;
566538
567539 // Clock should not be skewed from normal errors
568540 expect ( DateUtils . getClockOffset ( ) ) . toBe ( 0 ) ;
569541
570- jest
571- . spyOn ( RestClient . prototype as any , '_signed' )
572- . mockRejectedValueOnce ( clockSkewError ) ;
542+ // mock clock skew error response and successful response after retry
543+ mockAxios
544+ . mockImplementationOnce ( ( ) => {
545+ return new Promise ( ( _ , rej ) => {
546+ rej ( clockSkewError ) ;
547+ } ) ;
548+ } )
549+ . mockResolvedValue ( {
550+ data : [ { name : 'Bob' } ] ,
551+ } ) ;
573552
574553 await expect ( api . post ( 'url' , 'path' , init ) ) . resolves . toEqual ( [
575554 { name : 'Bob' } ,
0 commit comments