XMLHttpRequest is not defined when testing react-native app with jest -


i trying test apiwrapper in react-native based app using jest (integration testing). when run in ios simulator runs fine wont run jest tests correctly - get:

referenceerror: xmlhttprequest not defined 

when try run tests using api wrapper, eg.:

it('login successful correct data', () => {   let api = api.getinstance();   return api.login("test", "testpass")          .then(result => expect(result).toequal('login_successful')); }); 

the api class trying test here use fetch api (not vanilla xhr). assume related jest trying mock have not found way make work yet.

thanks in advance.

i had similar problem lokka using xmlhttprequest. made mock lokka, api wrapper class depends on. try mocking api wrapper.

this lokka mock looks now. i'll add more when start testing error handling.

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

you might able mock api wrapper similar:

export default class api {         getinstance() {     \\ implemented getinstance         }    login(user, password) {     return new promise(resolve => {       resolve('login_successful');     });   } } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -