Sinon FakeServer no requests?

2019-02-20 21:32发布

问题:

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?

回答1:

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);
});


回答2:

Just use:

var { JSDOM } = require('jsdom')
var dom = new JSDOM
window = dom.window
window.XMLHttpRequest = sinon.useFakeXMLHttpRequest()