XMLHttpRequest testing in Jest
ajax, javascript, jestjs, xmlhttprequest
Solution
The jest api has changed a bit. This is what I use. It doesn't do anything but it's enough to render my components.
const xhrMockClass = () => ({
open : jest.fn(),
send : jest.fn(),
setRequestHeader: jest.fn()
})
window.XMLHttpRequest = jest.fn().mockImplementation(xhrMockClass)
and in the test file:
`import '../../__mock__/xhr-mock.js'`
Problem
I want to test AJAX methods (vanilla XHR) and I can't find the way to do it with Jest framework. I found `mock-ajax.js` for Jasmine. The problem is I can't find the way to install it. Is there maybe a better way to unit test Ajax functions in Jest?