DEV Community

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

Collapse
 
pallymore profile image
Yurui Zhang

Hi - it sounds like your test is making real requests - which means the fakeServer did not catch it. it could be a lot of things but the most likely explaination is the url in the setup is wrong.

could you try using a regex instead of a string for respondWith setup? could you share what you have?

Collapse
 
stevetaylor profile image
Steve Taylor

Sinon’s fake server doesn’t fall back to real requests. The issue is possibly that the current versions of sinon and jest are incompatible.

Thread Thread
 
pallymore profile image
Yurui Zhang

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.