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

Created on 20 May 2016  ·  5Comments  ·  Source: sinonjs/sinon

  • 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.

Most helpful comment

@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.

All 5 comments

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 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.

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.

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

@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 :stuck_out_tongue_closed_eyes:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andys8 picture andys8  ·  4Comments

stephanwlee picture stephanwlee  ·  3Comments

zimtsui picture zimtsui  ·  3Comments

stevenmusumeche picture stevenmusumeche  ·  3Comments

brettz9 picture brettz9  ·  3Comments