jQuery没有定义使用mocha来测试wordpress javascript

时间:2022-11-07 14:02:52

I've set up mocha to run tests in the terminal. It works for basic test like expect(1).to.equal(1). The problem I run into is that my script is wrapped in the jQuery like so jQuery( function( $ ) { // my code here }); and I get an error when running the tests.

我已经设置了mocha来在终端中运行测试。它适用于基本测试,如expect(1).to.equal(1)。我遇到的问题是我的脚本包含在jQuery中,就像jQuery(function($){// my code here});我在运行测试时遇到错误。

evalmachine.<anonymous>:15
jQuery( function ( $ ) {
^

ReferenceError: jQuery is not defined
at evalmachine.<anonymous>:15:1
at Object.exports.runInThisContext (vm.js:54:17)
at Suite.<anonymous> (/Users/grahamlutz/Documents/BBC/poolproof/test/test.js:21:8)
at context.describe.context.context (/usr/local/lib/node_modules/mocha/lib/interfaces/bdd.js:47:10)
at Object.<anonymous> (/Users/grahamlutz/Documents/BBC/poolproof/test/test.js:8:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at /usr/local/lib/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:469:10)
at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha:404:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:141:18)
at node.js:933:3

my test.js looks like this:

我的test.js看起来像这样:

var assert = require('assert');
var chai = require('chai');
var expect = chai.expect;
var fs = require('fs');
var vm = require('vm');
var jsdom = require('mocha-jsdom'); 

describe('mocha tests', function () {

jsdom();

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

var path = __dirname + '/../wp-content/themes/bb-theme-child/myscript.js';
var code = fs.readFileSync(path);
vm.runInThisContext(code);

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

My question is either why is jQuery undefined or what incorrect assumptions am I making? Do I need to change the way I think about testing javascript within a wordpress set up?

我的问题是为什么jQuery未定义或我做出了什么错误的假设?我是否需要改变我在wordpress设置中测试javascript的方式?

2 个解决方案

#1


3  

The following two lines worked for me. For convenience I just put these at the top of my mocha test file. I used 'jsdom-global' package instead of 'mocha-jsdom' per the recommendation on the mocha-jsdom github readme. https://github.com/rstacruz/jsdom-global

以下两行对我有用。为方便起见,我把它们放在我的mocha测试文件的顶部。根据mocha-jsdom github自述文件的建议,我使用'jsdom-global'包而不是'mocha-jsdom'。 https://github.com/rstacruz/jsdom-global

this.jsdom = require('jsdom-global')()
global.$ = global.jQuery = require('jquery');

#2


1  

You have to export $ and jQuery to the global space yourself:

您必须自己将$和jQuery导出到全局空间:

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

If you read mocha-jsdom's documentation you'll see that it puts in the global space symbols like window and document. When you load jquery, it finds window and adds itself as window.$. In a browser, this also makes $ visible in the global space because window is the global space. In Node, however, the global space is global, and so you have to put $ in it yourself.

如果您阅读mocha-jsdom的文档,您会看到它放入了全局空间符号,如窗口和文档。当你加载jquery时,它会找到窗口并将自己添加为窗口。$。在浏览器中,这也使$在全局空间中可见,因为窗口是全局空间。然而,在Node中,全局空间是全局的,因此您必须自己将$放入其中。

#1


3  

The following two lines worked for me. For convenience I just put these at the top of my mocha test file. I used 'jsdom-global' package instead of 'mocha-jsdom' per the recommendation on the mocha-jsdom github readme. https://github.com/rstacruz/jsdom-global

以下两行对我有用。为方便起见,我把它们放在我的mocha测试文件的顶部。根据mocha-jsdom github自述文件的建议,我使用'jsdom-global'包而不是'mocha-jsdom'。 https://github.com/rstacruz/jsdom-global

this.jsdom = require('jsdom-global')()
global.$ = global.jQuery = require('jquery');

#2


1  

You have to export $ and jQuery to the global space yourself:

您必须自己将$和jQuery导出到全局空间:

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

If you read mocha-jsdom's documentation you'll see that it puts in the global space symbols like window and document. When you load jquery, it finds window and adds itself as window.$. In a browser, this also makes $ visible in the global space because window is the global space. In Node, however, the global space is global, and so you have to put $ in it yourself.

如果您阅读mocha-jsdom的文档,您会看到它放入了全局空间符号,如窗口和文档。当你加载jquery时,它会找到窗口并将自己添加为窗口。$。在浏览器中,这也使$在全局空间中可见,因为窗口是全局空间。然而,在Node中,全局空间是全局的,因此您必须自己将$放入其中。