XMLHttpRequest is not detected when testing an adaptive application with a joke

I am trying to test apiwrapper in a reaction based application using jest (integration testing). When I run it in the iOS simulator everything works fine, but it does not run my jest tests correctly - I always get:

ReferenceError: XMLHttpRequest is not defined 

when I try to run tests using my api shell, for example:

 it('Login successful with correct data', () => { let api = Api.getInstance(); return api.login("test", "testpass") .then(result => expect(result).toEqual('login_successful')); }); 

The api class I'm trying to check here uses the fetch api (and not vanilla xhr). I suppose this has something to do with a joke trying to mock something, but has not yet found a way to make it work.

Thanks in advance.

+5
source share
2 answers

I had a similar problem with lokka using XMLHttpRequest . I made a mock for lokka , which the api shell class depends on. You can try to taunt your api wrapper.

This is what my lokka layout looks like now. I will add even more when I start testing error handling.

 export default class { query() { return new Promise(resolve => { resolve(200); }); } } 

You could make fun of your api wrapper with something similar:

 export default class Api { getInstance() { \\ However you implemented getInstance } login(user, password) { return new Promise(resolve => { resolve('login_successful'); }); } } 
+2
source

In my case, I am not testing the search code, but importing it somewhere in my code, so I decided to just mock it at the global level in the setup.js file (it was loaded using --require when testing the run). The following works for me:

 // needed in react-instantsearch class XMLHttpRequest {} global.XMLHttpRequest = XMLHttpRequest; 
0
source

All Articles