frogbandit frogbandit - 5 months ago 128
Javascript Question

Sinon FakeServer no requests?

I'm following the SinonJS Fake Server tutorial and I'm running this simple code:

var server;

before(function () { server = sinon.fakeServer.create(); });
after(function () { server.restore(); });

it("calls callback with deserialized data", function () {
var callback = sinon.spy();
getTodos(42, callback);

// This is part of the FakeXMLHttpRequest API
server.requests[0].respond(
200,
{ "Content-Type": "application/json" },
JSON.stringify([{ id: 1, text: "Provide examples", done: true }])
);

assert(callback.calledOnce);
});


I'm including the getTodos function in the same file. Here it is as well:

function getTodos(listId, callback) {
$.ajax({
url: "/todo/" + listId + "/items",
success: function (data) {
// Node-style CPS: callback(err, data)
callback(null, data);
}
});
}


However, I'm getting an error
TypeError: Cannot read property 'respond' of undefined
. It looks like
server.requests
is empty -- why is this? How can I make sure the requests show up?

UPDATE: I was able to narrow down the issue. I added an "error" callback to the getTodos function, and did console.log for the error. It turns out there's a Syntax error coming from the jsdom node module trying to do
urlObj = new URL(uri, documentBaseURLSerialized(this._ownerDocument));
which is then causing
$.ajax({
to fail. Anyone have any ideas on this?

UPDATE 2: Now I'm testing another AJAX request in a different React component (different file) and I'm getting this error again. If I move the function into the same file, I don't have the error. What's going on here?

Answer

The one issue I can think of is that you did not load Sinon in your JSDom instance. Here is a test file that works here:

var jsdom = require("jsdom");
var assert = require("assert");

var vc = jsdom.createVirtualConsole();
vc.on("log", console.log.bind(console.log));
vc.on("jsdomError", function jsdomError(er) {
    throw er;
});

var window;
var $;
var sinon;
before(function (done) {
    jsdom.env({
        html: "",
        scripts: ["node_modules/jquery/dist/jquery.js",
                  "node_modules/sinon/pkg/sinon.js"],
        features: {
            ProcessExternalResources: ["script"],
            FetchExternalResources: ["script", "link"],
        },
        virtualConsole: vc,
        done: function _done(error, w) {
            if (error) {
                throw error;
            }
            window = w;
            $ = w.$;
            sinon = w.sinon;
            done();
        },

    });
});


function getTodos(listId, callback) {
    $.ajax({
         url: "/todo/" + listId + "/items",
         success: function (data) {
             // Node-style CPS: callback(err, data)
             callback(null, data);
         }
    });
}

var server;

before(function () { server = sinon.fakeServer.create(); });
after(function () { server.restore(); });

it("calls callback with deserialized data", function () {
    var callback = sinon.spy();
    getTodos(42, callback);

    // This is part of the FakeXMLHttpRequest API
    server.requests[0].respond(
        200,
        { "Content-Type": "application/json" },
        JSON.stringify([{ id: 1, text: "Provide examples", done: true }])
    );

    assert(callback.calledOnce);
});
Comments