mosawi
mosawi

Reputation: 1323

How to properly export variables in node.js module to make it visible for unit testing

I am trying to unit test node.js modules, I'd like to know how to export varibles and objects properly? So I can access them in my Unit Test and work on them.

source code:

var someObject = {};

var Array = [189, 347, 497, 632, 750, 900, 995, 1137],
    someNumber = 142,
    sizeOfArray = Allowance.length; 

someObject = { 

    method1 : function (info) {
         //something...
         return {
             someOtherMethod1: someObject.method1(info) && someObject.method2(info)
             someOtherMethod2: someObject.method3(info)
         };
    },

    method2 : function (info) {
        //something...

        return something2;
    } 

    method3 : function (info) {
        //something...

        return something3;
    }
};

module.exports = someObject;

So by doing:

module.exports = someObject;

I am able to access someObject in a unit test file in mocha, but how can I access the other variables like Array, sizeOfArray, someNumber?

I've tried adding this:

module.exports = {

      someObject  : someObject,
      Array       : Array,
      someNumber  : someNumber,
      sizeOfArray : sizeOfArray
};

But my unit test file cannot read it

Edit:

Here is my unit test

var assert = require("assert") 
var expect = require("expect.js") 
var chai   = require("chai")  

var sourceCode = require('../src/sourceCode') 


  describe("Something..", function() { 

      var Info = { 
        //some data...
      };


      describe("Properties", function() { 

          it("has someObject defined", function() {
            expect(someObject).not.to.be.undefined; 
          });

          it("has Array defined", function() {
            expect(Array).not.to.be.undefined; 
          });

          it("has someNumber defined", function() {
            expect(someNumber).not.to.be.undefined; 
          });

          it("has sizeOfArray defined", function() {
            expect(sizeOfArray).not.to.be.undefined; 
          })

        })

  describe("Methods", function() {

      it("should have method1 defined", function() { 
       expect(sourceCode.someObject.method1(Info)).not.to.be.undefined;
      });

      it("should have method2 defined", function() { 
        expect(sourceCode.someObject.method2(Info)).not.to.be.undefined; 
      });
  })

    };

@Augusto

This is what I was talking about for testing Method1

it("should return positive when conditions are met", function() {

        var result = sourceCode.someObject.method1(Info);

        expect(result).not.to.be.undefined;
        expect(result.eligible).not.to.be.undefined;
        expect(sourceCode.result.someOtherMethod2).not.to.be.undefined;

        expect(sourceCode.result.someOtherMethod1).to.equal(true);
}); 

Upvotes: 1

Views: 840

Answers (1)

Augusto Altman Quaranta
Augusto Altman Quaranta

Reputation: 1576

I think that the problem is in your unit tests. You are not accessing the properties in the object

var sourceCode = require('../src/sourceCode');

//CODE CODE CODE

expect(Array).not.to.be.undefined; 

There you were trying to access the Array variable instead of the sourceCode.Array property. As you were not defining an Array variable you were getting the undefined error message. The correct code would've been:

expect(sourceCode.Array).not.to.be.undefined; 

So.. in your unit tests try this:

var assert = require("assert") 
var expect = require("expect.js") 
var chai   = require("chai")  

var sourceCode = require('../src/sourceCode') 


  describe("Something..", function() { 

      var Info = { 
        //some data...
      };


      describe("Properties", function() { 

          it("has someObject defined", function() {
            expect(sourceCode.someObject).not.to.be.undefined; 
          });

          it("has Array defined", function() {
            expect(sourceCode.Array).not.to.be.undefined; 
          });

          it("has someNumber defined", function() {
            expect(sourceCode.someNumber).not.to.be.undefined; 
          });

          it("has sizeOfArray defined", function() {
            expect(sourceCode.sizeOfArray).not.to.be.undefined; 
          })

        })
    };

Upvotes: 1

Related Questions