[英]jQuery not defined using mocha for testing wordpress javascript
I've set up mocha to run tests in the terminal. 我已经设置了mocha来在终端中运行测试。 It works for basic test like
expect(1).to.equal(1)
. 它适用于基本测试,如
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 });
我遇到的问题是我的脚本包含在jQuery中,就像
jQuery( function( $ ) { // my code here });
and I get an error when running the tests. 我在运行测试时遇到错误。
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? 我的问题是为什么jQuery未定义或我做出了什么错误的假设? Do I need to change the way I think about testing javascript within a wordpress set up?
我是否需要改变我在wordpress设置中测试javascript的方式?
The following two lines worked for me. 以下两行对我有用。 For convenience I just put these at the top of my mocha test file.
为方便起见,我把它们放在我的mocha测试文件的顶部。 I used 'jsdom-global' package instead of 'mocha-jsdom' per the recommendation on the mocha-jsdom github readme.
根据mocha-jsdom github自述文件的建议,我使用'jsdom-global'包而不是'mocha-jsdom'。 https://github.com/rstacruz/jsdom-global
https://github.com/rstacruz/jsdom-global
this.jsdom = require('jsdom-global')()
global.$ = global.jQuery = require('jquery');
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
. 如果您阅读
mocha-jsdom
的文档,您会看到它放入了全局空间符号,如window
和document
。 When you load jquery
, it finds window
and adds itself as window.$
. 当你加载
jquery
,它会找到window
并将自己添加为window.$
。 In a browser, this also makes $
visible in the global space because window
is the global space. 在浏览器中,这也使
$
在全局空间中可见,因为window
是全局空间。 In Node, however, the global space is global
, and so you have to put $
in it yourself. 然而,在Node中,全局空间是
global
,因此您必须自己将$
放入其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.