• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

126
Views
Jest - Unit Testing a Namespaced File

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?

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error