ash123
ash123

Reputation: 307

Is this JavaScript unit testable using Jasmine?

Is this javascript code unit testable using Jasmine. If not how do we make it unit testable?

The problem basically is the helloworld function is not accessible inside a test spec js file, since it is not in global scope. How do we refactor the code such that it is accessible within the test spec file?

(function($){
    function helloWorld() {
          return "Hello world!";
        }
})(jQuery);

Upvotes: 2

Views: 96

Answers (1)

user229044
user229044

Reputation: 239302

No, you cannot test a function that is not visible.

You should conditionally export it in your test environment by defining some global constant:

(function ($) {
  var exports = window.TESTING ? window : {}

  exports.helloWorld = function () {
    // ...
  };

})(jQuery);

Upvotes: 4

Related Questions