Home > OS >  Jest - Unit Testing a Namespaced File
Jest - Unit Testing a Namespaced File

Time:09-22

Is it possible to write a unit test with Jest for a JavaScript file that is namespaced using the below format?

// utilities.js

var MyApp = MyApp || {};
MyApp.Common = MyApp.Common || {};
MyApp.Common.Utilities = new function(){
    this.getSomething = function(){  // Need to unit test this function
        return 'Something';
    }
}

If not, is there a better way to structure the namespaces to allow for unit testing?

CodePudding user response:

If you export MyApp like following example then in utilities.test.js you can

const {MyApp} = require('./utilities');

it('should work', ()=>{
  expect(MyApp.Common.Utilities.getSomething()).toEqual('something');
});

  • Related