jest-environment import { CreateResponse , ISerializedRequest , ISerializedResponse , } from "unmock-core/dist/interfaces" ; import { replaceOpenAndReturnOriginal } from ".." ; param req param sendResponse param emitError const okResponseCreator = ( _ ) : ISerializedResponse => { return { headers : { } , statusCode : 200 , body : JSON . stringify ( { ok : true , } ) , } ; } ; let openCommand ; beforeAll ( ( ) => { openCommand = replaceOpenAndReturnOriginal ( okResponseCreator ) ; } ) ; afterAll ( ( ) => { XMLHttpRequest . prototype . open = openCommand ; } ) ; describe ( "Monkey patched XMLHttpResponse" , ( ) => { it ( "should respond as expected when used with callback" , done => { const request = new XMLHttpRequest ( ) ; request . open ( "GET" , "https://example.com" ) ; request . onload = ( ) => { const asJson = JSON . parse ( request . responseText ) ; expect ( asJson . ok ) . toBe ( true ) ; done ( ) ; } ; request . send ( ) ; } ) ; } ) ;