mardi 28 juin 2016

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?


Aucun commentaire:

Enregistrer un commentaire