简体   繁体   English

使用jQuery运行命令行Mocha时出错

[英]Error running command line Mocha using jQuery

So I have this function that uses jQuery in a file 所以我有这个在文件中使用jQuery的函数

account.js: account.js:

function getSession() {
  var session = null;
  $.ajax({
    url: "/ajax/get_session",
    async: false
  }).done(function(data, textStatus, jqXHR){
    session = data;
  });
  return session;
}

and I want to test it on the command line using Mocha. 我想使用Mocha在命令行上测试它。 for this I have another file: 为此,我有另一个文件:

test-account.js: 测试account.js:

var assert = require('assert');
var fs = require('fs');
var vm = require('vm');

// includes minified & uglified version, assuming mocha is run in repo root dir
var path = '../public/js/account.min.js';
var code = fs.readFileSync(path);
vm.runInThisContext(code);

describe('getSession', function() {
  it('should return the empty string because it fails', function () {
      assert.equal('', getSession());
  });
});

But when I run mocha on the command line I get the error 但是当我在命令行上运行mocha时,我得到了错误

1 failing 1失败

1) getSession should return the empty string because it fails: 1)getSession应返回空字符串,因为它失败:

ReferenceError: $ is not defined ReferenceError:$未定义

at getSession (evalmachine.:1:34) 在getSession(evalmachine。:1:34)

at Context. 在上下文。 (test/test-account.js:19:24) (试验/测试account.js:19:24)

I'm a Mocha n00b, so I wasn't exactly sure the correct direction to go in to solve this. 我是摩卡n00b,所以我不确定正确的方向去解决这个问题。 I tried adding 我尝试添加

var jqueryLocalPath = 'jquery.min.js';
var jqueryCode = fs.readFileSync(jqueryLocalPath);
vm.runInThisContext(jqueryCode);

before the describe call in test-account.js without success. test-account.js describe调用之前没有成功。 I then tried to require it via Node.js by running 然后我尝试通过运行来通过Node.js来require

npm install jquery

and adding 并添加

var $ = require('jquery');

to the top of test-account.js before running mocha . 在运行mocha之前到test-account.js的顶部。 This also did not work and I get the same error. 这也没有用,我得到了同样的错误。 This second method has the added disadvantage of running a newer version of jQuery (2.2.0) the the one I want to test on (1.11.3). 第二种方法的另一个缺点就是运行了我想要测试的jQuery(2.2.0)的新版本(1.11.3)。

How can I make this test work? 我怎样才能使这个测试工作?

I believe the answer is to use the correct Node modules , for testing. 我相信答案是使用正确的Node模块进行测试。

var assert = require('assert');
var fs = require('fs');
var vm = require('vm');
var jsdom = require('mocha-jsdom'); // This is necessary for testing jQuery in Mocha

describe('mocha tests', function () {

    jsdom()

    before(function () {
        $ = require('jquery')
    });

    // includes minified & uglified version, assuming mocha is run in repo root dir
    var path = 'account.js';
    var code = fs.readFileSync(path);
    vm.runInThisContext(code);

    describe('getSession', function() {
        it('should return the empty string because it fails', function () {
            assert.equal('', getSession());
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM