Mocking XMLHttpRequest on Node.js doesn't work
tomasfabry opened this issue · 1 comments
Hello,
thanks for creating this awesome package. I like it very much but I am not able to use it on Node.js.
I've tried to use it but it looks like the original implementation of XMLHttpRequest
is not replaced with the mocked one.
I've made a little investigation about this problem and I've figured out that the problem is in setup
function in XHRMock
class:
setup(): XHRMock {
// @ts-ignore: https://github.com/jameslnewell/xhr-mock/issues/45
window.XMLHttpRequest = MockXMLHttpRequest;
this.reset();
return this;
}
On Node.js there is no window
object! It exists only in browsers. When you want to use XMLHttpRequest
in Node.js you have to import it at first like this:
import { XMLHttpRequest } from 'xmlhttprequest'
And that's the reason why the implementation is not replaced. Do you have any idea how to fix it? Thank you very much!
Thanks @tomasfabry! It's likely related to this issue #67 - xhr-mock
is currently assuming a jsdom
environment (we use jest
as the test runner).
You could try changing xhr-mock
to use global/window
from global
or setup jsdom
as per the linked issue.
Would you be able to contribute some docs for using xhr-mock
outside of jest
?