DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Discussion on: Testing API Request Hooks with Jest, Sinon, and react-testing-library

 
pallymore profile image
Yurui Zhang Author

yea I looked into it and I think you are right for the first part.
However I don't think it's incompatibility issue between jest / sinon.

Since jest runs the code in the node environment, not all browser native functions are properly implemented. If you are using fetch - some polyfill libraries work correctly with sinon (e.g. whatwg-fetch), while others don't (e.g. isomorphic-fetch). sinon's fakeServer also does not work with axios correctly if you are using that.

The error you are getting indicates the fake server is not mocking the correct fetch (or XMLHttpRequest) - real requests are being made in the test environment - maybe it's better to investigate how you can mock the request library/method you are using directly with jest.

Thread Thread
 
stevetaylor profile image
Steve Taylor

Yeah, I gave up and mocked my endpoint functions, which are a simple layer atop superagent, which uses XHR.