javascript - Testing XHR with Jest -
i have simple test case:
describe ('request', function () { ('should perform xhr "get" request', function (done) { var xhr = new xmlhttprequest; xhr.open('get', 'http://google.com', true); xhr.send(); }); });
the point is: when jest
in terminal, seems it's not requesting http://google.com
. think happening because jest uses jsdom under hood , haven't capability reproduce http request out of browser itself.
suggestions?
note: errors appearing, test passing, request isn't being made.
there're number of problems:
- in code example don't create instance of xmlhttprequest. should this:
var xhr = new xmlhttprequest()
- making real requests in tests bad idea. if module depends on data remote server should mock xmlhttpsrequest or use "fake server" (e.g. sinon). recommend don't make real requests in tests.
Comments
Post a Comment