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.fakeServer does not work under Node.js because its file isn't wrapped properly #319

Closed
moll opened this issue Aug 21, 2013 · 13 comments

Comments

@moll
Copy link

moll commented Aug 21, 2013

The fake_xml_http_request.js file has an iffy wrapping it for proper global access. The fake_server.js file does not and with the following "check" in its header it never has a chance to get a reference to the global sinon because the var sinon declaration is hoisted to the top of the scope:

if (typeof sinon == "undefined") {
    var sinon = {};
}
@cjohansen
Copy link
Contributor

The Fake XHR/server stuff wasn't intended to work on Node when it was written. Feel free to submit a patch for this, and I'll happily pull.

@moll
Copy link
Author

moll commented Aug 26, 2013

Kay, so how do you envision this? With help from jsdom and some freakingly ugly hacks (without changing Sinon's code) I actually got it all working.

I'd argue for presenting the complete Sinon on Node without messing with global variables and explicit requires, but initializing the server stuff only when explicitly required. Which is how it works in the browser as well, I guess.

@mantoni
Copy link
Member

mantoni commented Aug 26, 2013

Maybe this is something we could address properly in Sinon 2.0 by moving the XHR stuff into a separate module. Not sure whether @moll's jsdom thingy could live in a separate module as well.

@moll
Copy link
Author

moll commented Aug 26, 2013

Well, what I did was hack to get Sinon's fake server code from the outside to use the global reference of XMLHttpRequest as it doesn't use the global reference there even if it exists. JSDOM itself uses Node's xmlhttprequest module and that seems to work out of the box if Sinon would use the correct reference and not depend on globals.

Moving Sinon's XHR stuff to a proper module that would take the real XHR reference and return a fake one, or something equivalent, would be indeed better.

@Fauntleroy
Copy link

@cjohansen I'm currently running into a similar issue using Sinon with https://github.com/substack/node-browserify. Since I'm using the npm version of Sinon and Browserify loads everything as CommonJS modules, it looks like Sinon is leaving out all of the HTTP mocking methods (even though I'm in a browser environment). Is there some other way to check environments that would allow me to use sinon.useFakeXMLHttpRequest() via Browserify?

@greypants
Copy link

Same issue as @cjohansen using browserify.

@roblevintennis
Copy link

Same issue when used with karma and phantomjs:

TypeError: Fake server request processing threw exception: '[object ProgressEventConstructor]' is not a constructor (evaluating 'new ProgressEvent("progress", {loaded: 100, total: 100})')
    at /Users/roblevin/labs/ember/testing-tutsplus/js/libs/sinon-1.8.1.js:3663
PhantomJS 1.9.7 (Mac OS X) Ajax tests Display error message when server responds with error FAILED
    TypeError: Fake server request processing threw exception: '[object ProgressEventConstructor]' is not a constructor (evaluating 'new ProgressEvent("progress", {loaded: 100, total: 100})')
    /Users/roblevin/labs/ember/testing-tutsplus/js/libs/sinon-1.8.1.js:3663

PhantomJS 1.9.7 (Mac OS X): Executed 1 of 0 (1 FAILED) ERROR (0.371 secs / 0.117 secs)

@benfoxall
Copy link

Is this an issue with PhantomJS?

Using the following script:

var page = require('webpage').create();
page.evaluate(function(){
  new ProgressEvent("progress", {loaded: 50, total: 100});
});

I get this output from running phantomjs script.js:

TypeError: '[object ProgressEventConstructor]' is not a constructor (evaluating 'new ProgressEvent("progress", {loaded: 50, total: 100})')

  phantomjs://webpage.evaluate():2
  phantomjs://webpage.evaluate():3
  phantomjs://webpage.evaluate():3

update: I hacked my way around this issue by commenting out this line:

switch (this.readyState) {
    case FakeXMLHttpRequest.DONE:
        this.dispatchEvent(new sinon.Event("load", false, false, this));
        this.dispatchEvent(new sinon.Event("loadend", false, false, this));
        this.upload.dispatchEvent(new sinon.Event("load", false, false, this));
        // this.upload.dispatchEvent(new ProgressEvent("progress", {loaded: 100, total: 100}));
        break;
}

update 2: @nyk0r's suggestion below is a better approach

@nyk0r
Copy link

nyk0r commented Feb 6, 2014

I'm having the same "'[object ProgressEventConstructor]' is not a constructor" issue using grunt-mocha and PhantomJS on my Windows 8 machine. It seems to fall on every second call to the fake XHR.

Currently hacked this issue by adding the following code before running the specs.

if (navigator.userAgent.indexOf('PhantomJS') !== -1){
    window.ProgressEvent = function (type, params) {
        params = params || {};

        this.lengthComputable = params.lengthComputable || false;
        this.loaded = params.loaded || 0;
        this.total = params.total || 0;
    };
}

@dcrockwell
Copy link

Also have the same problem with Karma + Chrome, Firefox, & Safari.

@dcrockwell
Copy link

I was able to solve this problem in Karma by installing the karma-sinon package, and adding it to frameworks in karma.conf.js

@cjohansen
Copy link
Contributor

Node-workable fake server is a goal for 2.0 #600

@hayesmaker
Copy link

that's cool, because it doesn't work in the browser either

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

10 participants