Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking XMLHttpRequest on Node.js doesn't work #76

Open
tomasfabry opened this issue Nov 22, 2018 · 1 comment
Open

Mocking XMLHttpRequest on Node.js doesn't work #76

tomasfabry opened this issue Nov 22, 2018 · 1 comment

Comments

@tomasfabry
Copy link

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!

@jameslnewell
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants