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

Sinon useFakeXMLHttpRequest() and fakeServer.create() don't work on Node.js #1049

Closed
dunpyl opened this issue May 20, 2016 · 5 comments
Closed

Comments

@dunpyl
Copy link

dunpyl commented May 20, 2016

  • Sinon version : 1.17.4 (current latest stable)
  • Environment : Node.js 6.2.0 (current latest stable)

What did you expect to happen?

Per the docs, I expected the following examples to work:

Basic fake request example:

http = require('http');
sinon = require('sinon');

xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = function (req) { requests.push(req); };

http.get('http://example.com');
require('assert')(requests.length > 0);

Basic fake request example:

http = require('http');
sinon = require('sinon');

server = sinon.fakeServer.create();

http.get('http://example.com');
require('assert')(server.requests.length > 0);

What actually happens

Both assertion fail, as if calling sinon.useFakeXMLHttpRequest() and sinon.fakeServer.create() had no effect.

How to reproduce

Run the two examples above on latest Node.js with latest Sinon installed.

@fatso83
Copy link
Contributor

fatso83 commented May 21, 2016

I am not sure why you think this should work? The http library you are using would have to use XHR for the Sinon stub to be used, but that is not the case as Node does not have XHR.

If you want to test browser code you need to be in a browser environment, like PhantomJS.

If you want to unit test code that works in the browser by running the tests in Node then you need to have some network layer that abstract the differences between the underlying platforms, such as superagent.

@fatso83 fatso83 closed this as completed May 21, 2016
@dunpyl
Copy link
Author

dunpyl commented May 21, 2016

@fatso83 Node.js version 0.2 and above is listed on the Sinon.js homepage as a supported platform, so it's reasonable to expect Sinon's major features to work on Node.js. Indeed, the other features, such as spies and stub, do work on the platform out of the box.

Of course Node.js doesn't have a browser's XHR, but it does have its own standard methods of making HTTP requests, and it would be great if those were supported, so Sinon could cover the same scope of testing as it does on the browser. This isn't a far-fetched spec: other libraries, such as Nock, support this exact feature.

If Sinon doesn't support mocking HTTP requests and responses on Node.js the way it does on other platforms, then its support of Node.js is less than complete. If you google "sinon fakeserver node.js" you will see quite a few users stumbling on this lack of coverage. If there's no easy way to get these features to work on Node.js, then you should probably put a note in the docs saying that they're not supported, to avoid new users being confused.

@fatso83
Copy link
Contributor

fatso83 commented May 22, 2016

The Server API is a standalone package that says it

causes Sinon to replace the native XMLHttpRequest object in browsers that support it

It does not say it supports replacing network calls with fakes on all platforms, only browsers.

The home page of Sinon says this about the package

Standalone test spies, stubs and mocks for JavaScript.
No dependencies,

As such, the Server API is an extra utility package that complements the core functionality. For version 2 we are trying to slim down the core further - extracting mocks into its own package. Following that direction, having a sinon-node-http package doing what you described seems reasonable, although I really don't see much point in replicating the work of Nock, which can serve as a fine complement to Sinon.

I do agree with you that stuff like this could be better documented, and there have been plans to create a host of examples, but the project of restructuring the docs seems to be in limbo ATM.

@pjg
Copy link

pjg commented Jul 18, 2017

For posterity, the aforementioned http mocking library is actually Nock (not Nook): https://www.npmjs.com/package/nock

@fatso83
Copy link
Contributor

fatso83 commented Jul 18, 2017

@pjg - yeah, sorry about that. don't know how that happened, but I edited it now just in case. must have been thinking about replacing my Kindle 😝

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

3 participants