import { Test } from '@nestjs/testing' ; import { throwError , timer } from 'rxjs' ; import { AppService } from './app.service' ; import { RxjsService } from './rxjs/rxjs.service' ; describe ( 'AppService' , ( ) => { let service ; let rxjsService ; beforeEach ( async ( ) => { const module = await Test . createTestingModule ( { providers : [ AppService , { provide : RxjsService , useValue : { genericRetryStrategy : jest . fn ( ) . mockReturnValue ( timer ( 50 ) ) , } , } , ] , } ) . compile ( ) ; service = module . get < AppService > ( AppService ) ; rxjsService = module . get < RxjsService > ( RxjsService ) ; } ) ; describe ( 'playWithRxJS' , ( ) => { it ( 'should return a valid value' , ( done ) => { let counter = 0 ; let finalVal ; service . playWithRxJS ( 50 ) . subscribe ( { next : ( val ) => { expect ( val ) . toBe ( ++ counter ) ; finalVal = val ; } , error : ( err ) => { throw err ; } , complete : ( ) => { expect ( finalVal ) . toBe ( 10 ) ; done ( ) ; } , } ) ; } ) ; it ( 'should return with an error' , ( done ) => { let counter = 0 ; rxjsService . genericRetryStrategy = jest . fn ( ) . mockReturnValueOnce ( ( ) => throwError ( new Error ( 'This should error' ) ) ) ; service . playWithRxJS ( ) . subscribe ( { next : ( val ) => { expect ( val ) . toBe ( ++ counter ) ; } , error : ( err ) => { expect ( err . message ) . toBe ( 'This should error' ) ; expect ( rxjsService . genericRetryStrategy ) . toBeCalledTimes ( 1 ) ; done ( ) ; } , complete : ( ) => done ( ) , } ) ; } ) ; } ) ; } ) ;