Need help in passing this simple mocha test in javascript

549 views Asked by At

Here, I wrote a simple utilities class in Javascript that parses and stringify Json data.

//public/js/utilities.js
function UtilitiesClass(){
    var parserJson = function(obj){
        if (typeof(obj) !== "undefined") {
            return JSON.parse(obj);
        };
    }
    var stringifyJson = function(obj){
        if (typeof(obj) !== "undefined") {
            return JSON.stringify(obj);
        };
    }
}

module.exports = UtilitiesClass

Then in my test.js

require('../public/js/utilities.js');

describe('JS Utilities Classs Tests', function () {
    var jsonObjToStr, strtojsonObj;

    beforeEach(function () {
        this.jsonObjToStr = [1, 2, 3, 4];
        this.strtojsonObj = "[1, 2, 3, 4]";
    });

    it('should parse a string into JSON ', function () {
          expect(parserJson(this.strtojsonObj)).to.not.be.undefined;
    });

    it('should stringify JSON into a string', function () {
        expect(stringifyJson(this.jsonObjToStr)).to.not.be.undefined;
    });

});

Then when I tried running mocha, I got the following error output.

andy@LINUXAWCM:~/Projects/Javascript/e-shop-gadgets$ mocha

JS Utilities Classs Tests
      1) should parse a string into JSON 
      2) should stringify JSON into a string


    0 passing (12ms)
    2 failing

    1) JS Utilities Classs Tests should parse a string into JSON :
       ReferenceError: parserJson is not defined
        at Context.<anonymous> (test/test.js:12:18)

    2) JS Utilities Classs Tests should stringify JSON into a string:
       ReferenceError: stringifyJson is not defined
        at Context.<anonymous> (test/test.js:16:16)

Why wouldn't this simply work? The require statement on public/js/utilities.js comes out fine. But it's saying the parserJson and stringifyJson are not defined or found when really it's already loaded.

Isn't it?

1

There are 1 answers

1
Ben Fortune On BEST ANSWER

Simply declaring a variable inside a function is not enough, you need to return these since they aren't available outside the function's scope.

You're also exporting a function and not calling it.

Also requiring a file doesn't magically make your variables available to your scope.

//public/js/utilities.js
function UtilitiesClass(){
    var parserJson = function(obj){
        if (typeof(obj) !== "undefined") {
            return JSON.parse(obj);
        };
    }
    var stringifyJson = function(obj){
        if (typeof(obj) !== "undefined") {
            return JSON.stringify(obj);
        };
    }
    return {
        parserJson: parserJson,
        stringifyJson: stringifyJson
    };
}

module.exports = UtilitiesClass;
var util = require('../public/js/utilities.js')();

util.parserJson(...) //etc

Though you probably don't need to export a function, just each method.

//public/js/utilities.js
var parserJson = function(obj){
    if (typeof(obj) !== "undefined") {
        return JSON.parse(obj);
    };
}
var stringifyJson = function(obj){
    if (typeof(obj) !== "undefined") {
        return JSON.stringify(obj);
    };
}

module.exports = {
    parserJson: parserJson,
    stringifyJson: stringifyJson
};

Then you'd use

var utils = require('../public/js/utilities.js');