0

Hi I am new to Mocha Testing.I am trying to test my Javascript code through mocha .It first gave me Reference Error : JQuery not defined but when i added mocha-jsdom package . It throws localstorage is not available for opaque origins.

Below is my Test file.

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);

    });
});

And my Js function :

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
}

And My Error Log :

> 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
LPatil
  • 233
  • 3
  • 15
  • take a look at [this](https://stackoverflow.com/questions/38590957/how-to-fix-is-not-defined-error-when-unit-testing-jquery-with-typescript-usi) question, the OP is also facing the same problem and the question has different answers by which you can handle it. – vikscool Aug 07 '18 at 04:46
  • 1
    Possible duplicate of [How to fix "$ is not defined" error when unit testing Jquery with Typescript using Mocha?](https://stackoverflow.com/questions/38590957/how-to-fix-is-not-defined-error-when-unit-testing-jquery-with-typescript-usi) – Martin Zeitler Aug 07 '18 at 04:48
  • https://stackoverflow.com/questions/51554366/jest-securityerror-localstorage-is-not-available-for-opaque-origins/51702674 ...might be related to `jsdom()`. – Martin Zeitler Aug 07 '18 at 04:50
  • where can i include testURL ? – LPatil Aug 07 '18 at 05:18
  • Thanks Martin it worked – LPatil Aug 07 '18 at 11:52

0 Answers0