import * as httpMocks from 'node-mocks-http' ; import * as nock from 'nock' ; import { getPosts } from './posts' ; describe ( 'getPosts' , ( ) => { it ( 'should return the posts' , ( done ) => { const posts = [ { userId : '1' , id : '1' , title : 'title1' , body : 'body1' } , { userId : '2' , id : '2' , title : 'title2' , body : 'body2' } ] ; nock ( 'https://jsonplaceholder.typicode.com' ) . get ( '/posts' ) . reply ( 200 , posts ) ; const req = httpMocks . createRequest ( ) ; const res = httpMocks . createResponse ( ) ; getPosts ( req , res ) . then ( ( ) => { expect ( res . _getStatusCode ( ) ) . toBe ( 200 ) ; expect ( res . _isEndCalled ( ) ) . toBeTruthy ( ) ; expect ( res . _isJSON ( ) ) . toBeTruthy ( ) ; expect ( JSON . parse ( res . _getData ( ) ) . data ) . toEqual ( posts ) ; done ( ) ; } ) ; } ) ; it ( 'should return error if destination not reachable' , ( done ) => { const req = httpMocks . createRequest ( ) ; const res = httpMocks . createResponse ( ) ; getPosts ( req , res ) . then ( ( ) => { expect ( res . _getStatusCode ( ) ) . toBe ( 500 ) ; expect ( res . _isEndCalled ( ) ) . toBeTruthy ( ) ; expect ( res . _isJSON ( ) ) . toBeTruthy ( ) ; expect ( JSON . parse ( res . _getData ( ) ) . error ) . toBe ( 'Error' ) ; done ( ) ; } ) ; } ) ; } ) ;