端末でテストを実行するようにmochaを設定しました。それはexpect(1).to.equal(1)
のような基本的なテストのために働く。私が遭遇する問題は、スクリプトがjQueryに包まれているので、jQuery(function($) { // my code here });
のようになり、テストを実行するときにエラーが発生します。jQueryは、wordpress javascriptをテストするためのmochaを使用して定義されていません
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
私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();
});
});
});
私の質問のいずれかである理由のjQueryは未定義であるか、私が間違って何の仮定を作っていますか?私は、WordPressのセットアップ内でJavaScriptをテストする方法を変更する必要がありますか?
提案していただきありがとうございますが、これは問題を解決するようには見えませんでした。それでも同じターミナルエラーが発生します。 – BigBadBigBad
私が報告したことは間違いなく問題の一部です。ここで答えられる人からより正確なものを得る最良の方法は、あなたの質問を[mcve]にすることです。 – Louis