无法在Mocha测试中加载Jquery

时间:2018-08-07 04:24:43

标签: javascript jquery unit-testing mocha

嗨,我是Mocha Testing的新手。我试图通过mocha测试我的Javascript代码。它首先给了我Reference Error : JQuery not defined,但是当我添加mocha-jsdom包时。它会抛出localstorage is not available for opaque origins

下面是我的测试文件。

var assert = require('chai').assert;
var jsdom = require('mocha-jsdom');
var extChart = require('../../../../../../../webui/common/js/DE/APP/portal/extChart');

describe('ExtChart.js', function(){

    var $
      jsdom()

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


    it('Add Config Data Method', function(){
        var w_chartConfig = {};
        w_chartConfig.chartSummaryData = [{
            "Average": "<span style='font-weight:bold;color:#464646'>26.5</span>"
        },{
            "Last 3": "<span style='font-weight:bold;color:#464646'>0</span>"
        },{
            "Best 3": "<span style='font-weight:bold;color:#464646'>82.67</span>"
        },{
            "Worst 3": "<span style='font-weight:bold;color:#464646'>0</span>"
        }];


        var w_expectedResult = [{
            "Average": "26.5"
        },{
            "Last 3": "0"
        },{
            "Best 3": "82.67"
        },{
            "Worst 3": "0"
        }];

        assert.equal(extChart.addConfigData(undefined, w_chartConfig), w_expectedResult);

    });
});

还有我的Js函数:

var addConfigData = function addConfigData(w_chartDivs, a_chartConfig){
    var w_isMaxemaNewDashBoard = true;
    var w_chartSummary;

    if (a_chartConfig && a_chartConfig.chartSummaryData && w_isMaxemaNewDashBoard) {                    //New DashBoard
        var w_chartData = JSON.parse(JSON.stringify(a_chartConfig.chartSummaryData));

        for (var i = 0; i < w_chartData.length; i++) {
            var w_objectData = w_chartData[i];
            var w_data ;

            for (var key in w_objectData){
                w_data = w_objectData[key];
                var w_intValue = $(w_data)[0].innerHTML;

                w_objectData[key] = w_intValue;
            }
        }

        w_chartSummary = w_chartData;
    } else if (w_chartDivs.length > 0) {                                                                //Old DashBoard
        var w_summaryArr = [];

        for (var i = 0 ; i < w_chartDivs.length; i++) {
            var w_summaryObj = {};
            var w_chartDiv = w_chartDivs[i];
            var w_divVal = $(w_chartDiv).text().split(':');

            w_summaryObj[w_divVal[0].trim()] = w_divVal[1].trim();

            w_summaryArr.push(w_summaryObj);
        }

        w_chartSummary = w_summaryArr;
    }

    return w_chartSummary;
}

module.exports = {
            addConfigData: addConfigData
}

我的错误日志:

> mochatest@1.0.0 test D:\salm\projects\swiftalm_jboss6\Mocha\common\js\DE\APP\portal
> mocha



  ExtChart.js
    1) "before all" hook


  0 passing (757ms)
  1 failing

  1) ExtChart.js
       "before all" hook:
     Uncaught SecurityError: localStorage is not available for opaque origins
      at Window.get localStorage [as localStorage] (node_modules\jsdom\lib\jsdom\browser\Window.js:257:15)
      at propagateToGlobal (node_modules\mocha-jsdom\index.js:108:27)
      at Object.done (node_modules\mocha-jsdom\index.js:59:9)
      at process.nextTick (node_modules\jsdom\lib\old-api.js:366:18)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)



npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! mochatest@1.0.0 test: `mocha`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the mochatest@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     D:\nodejs\npm-cache\_logs\2018-08-07T04_14_13_866Z-debug.log

0 个答案:

没有答案