0
在一個組件中,我發送了一個xhr請求到'/ api/somecall'如何用JS測試的絕對路徑來模擬請求?
我試圖用MochaJS來模擬用於測試目的的HTTP請求。
我進入了nock,但第一個參數是域,由於我們在多個域中使用相同的應用程序,所以無法修復。
我雖然關於用location.href模擬一個DOM,所以xhr會在Mocha的測試會話的開始時以這種方式發送到一個特定的域中(我發現這個代碼的一部分在web ):
nock('http://localhost')
.get('/apv/v2/listings/' + county)
.reply(200, responseData)
所有的測試返回的請求tiemout像以前一樣:
// setup the simplest document possible
var doc = jsdom.jsdom('<!doctype html><html><body></body></html>', {url: "http://localhost"})
// get the window object out of the document
var win = doc.defaultView
// set globals for mocha that make access to document and window feel
// natural in the test environment
global.document = doc
global.window = win
// take all properties of the window object and also attach it to the
// mocha global object
propagateToGlobal(win)
// from mocha-jsdom https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
function propagateToGlobal (window) {
for (let key in window) {
if (!window.hasOwnProperty(key)) continue
if (key in global) continue
global[key] = window[key]
}
}
不幸的是,仍然沒有當我嘗試這種事後工作。
我怎麼可能得到它的工作?