使用mocha.jstestingjQyery.get不起作用,其他延迟(RSVP)

我正在尝试使用mocha.js运行一些JavaScripttesting。 到目前为止,一切工作都非常好,但是我在testingjQuery请求或其他承诺/延期方面遇到了很大的问题。

我的代码在浏览器中工作得很好,以前大量使用$.Deferred但与摩卡我不能让它运行。

我尝试了不同的方式来编写它,并切换到一个“适当的”Promise库,因为显然jQuery被认为会给它的实现带来麻烦。

正确的RSVP.Promises工作正常, RSVP.Defer还没有,我不能得到任何工作涉及jQuery(如发出请求)。

我确实发现了一些build议,将jQuery包装到适当的Promise中,应该可以做到这一点,但是找不到任何真正做到的方式,这样做不会失败。

这是一个testing用例来显示这个问题:

 'use strict'; var RSVP = require('rsvp'); var $ = require('jquery'); describe('promises', function() { it('works with RSVP.Promise', function(done) { new RSVP.Promise(function(resolve, reject) { resolve(); }).then(function() { done(); }); }); it('works with RSVP.Deferred promise', function(done) { RSVP.defer().done(function() { done(); }).resolve(); }); it('works with $.Deferred', function(done) { $.Deferred().done(function() { done(); }).resolve(); }); it('works with $.get (promise syntax)', function(done) { $.get('http://google.com', function() { done(); }); }); it('works with $.get (defered syntax)', function(done) { $.get('http://google.com').done(function() { done(); }); }); }); 

运行:

$ mocha --compilers jsx:babel/register

给我以下输出:

 promises ✓ works with RSVP.Promise 1) works with RSVP.Deferred promise 2) works with $.Deferred 3) works with $.get (promise syntax) 4) works with $.get (defered syntax) 1 passing (59ms) 4 failing 1) promises works with RSVP.Deferred promise: TypeError: undefined is not a function at Context.<anonymous> (test/test.js:17:26) 2) promises works with $.Deferred: TypeError: undefined is not a function at Context.<anonymous> (test/test.js:23:21) 3) promises works with $.get (promise syntax): TypeError: undefined is not a function at Context.<anonymous> (test/test.js:31:7) 4) promises works with $.get (defered syntax): TypeError: undefined is not a function at Context.<anonymous> (test/test.js:37:7) 

其他的testing运行得非常好,只要没有涉及jQuery Promise / Defer。

有没有更好的解决办法,而不是放弃jQuery的请求,或者我一般做错了什么? testing的目的是testing实际的请求/响应,所以在这里嘲笑请求也是不行的。

就像我说的那样,代码在浏览器中使用Promise / Defer jQuery的方式运行得非常好。 所以我想这是testing运行者或运行的node.js环境(?)的问题。

要testingHTTP请求,您可以使用: https : //github.com/visionmedia/supertest

这是伟大的,或者不使用jQuery来发出HTTP请求,你可以使用: https : //github.com/visionmedia/superagent

我认为这是在nodeJS比使用jQuery更标准的方式。