如何让Sinon的fakeServer在Mocha和Node中响应jQuery.ajax调用?

我在这里看了很多答案,那里有博客文章,在Sinon自己的问题跟踪器中发布的问题,还没有发现任何明确的。 我到目前为止大概是:

import XMLHttpRequest from "xhr2"; ... beforeEach((done) => { jsdom.env("<!doctype html><html><body><div></div></body></html>", (error, window) => { for (var i in require.cache) {delete require.cache[i];} Object.assign(global, _.pick(window, "document", "navigator")); global.window = window; window.console = global.console; document.body.dataset = {}; ... global.$ = require("jquery"); global.$(() => { global.$.support.cors = true; global.$.ajaxSettings.xhr = () => {return new XMLHttpRequest();}; const fakeServer = require("sinon/lib/sinon/util/fake_server").fakeServer; global.server = fakeServer.create({autoRespond: true}); done(); }); }); }); ... it("should bloody work!", () => { server.respondWith([401, {}, ""]); ... [some test code that triggers an $.ajax call]; }); 

在$ .ajaxerror handling程序中,我将参数logging到$ .ajax失败处理程序,并参阅:

 jqXHR = { readyState: 0, getResponseHeader: [Function], getAllResponseHeaders: [Function], setRequestHeader: [Function], overrideMimeType: [Function], statusCode: [Function], abort: [Function], state: [Function], always: [Function], then: [Function], promise: [Function], pipe: [Function], done: [Function], fail: [Function], progress: [Function], complete: [Function], success: [Function], error: [Function], status: 0, statusText: 'Error' } textStatus = error errorThrown = [Error] 

我设置假服务器以200和“OK”响应,或者调用.ajax调用,所有的道路导致状态为0,文本为“错误”,这是没有什么区别的。

       

网上收集的解决方案 "如何让Sinon的fakeServer在Mocha和Node中响应jQuery.ajax调用?"