How to unit test internal functions in jquery plugin?

in jQuery plugin I created helper functions like this

(function($) { var someHelperFunction = function(s, d) { return s*d; } var someOtherHelperFunction = function(s) { return s*2; } // here goes the normal plugin code })(jQuery); 

now I want to call someHelperFunction from the outside in order to be able to unit test it, is it possible somehow?

+6
javascript jquery jquery-plugins unit-testing
source share
5 answers

helpers are inside the plugin, which is an anonymous function, and you cannot access the variables declared inside it.
If you want to test it, leave the var keyword in front of the functions. This will declare the functions global (bind them to the window object), allowing them to be visible from the window area (by calling someHelperFunction or window.someHelperFunction ).
therefore for testing:

 (function($) { someHelperFunction = function(s, d) { return s*d; } someOtherHelperFunction = function(s) { return s*2; } // here goes the normal plugin code })(jQuery); 

after testing is complete, add the var keyword again.

Update
I think the best approach would be to group your tested functions in an object and build an api. Then, by the same principle, you can make api visible on a global scale or not:

 (function($, global) { someHelperFunction = function(s, d) { return s*d; } someOtherHelperFunction = function(s) { return s*2; } var api = { someHelperFunction: someHelperFunction, someOtherHelperFunction: someOtherHelperFunction }; // decide whether you want to expose your api or not if(makeGlobal) { global.api = api; } })(jQuery, this); 
0
source share

Per this related question , I would say just test the frontend.

But if you must test these methods in isolation, you will have to test "copies" of them outside the context of their deployment as internal methods. In other words, create an object in which they will not be accessible to the client code, and then copy these versions together with the external script in the preliminary process. It seems like a lot of work, but hey, what's the point of hiding methods, right? (To make them inaccessible.)

+2
source share

If internal functions need testing, this is a good indicator that they should be somewhere in a separate module and introduced as dependencies and used in the implementation of the public interface of your objects. This is a more β€œverifiable” way to do this.

 var myActualImplementationTestTheHellOutOfMe = function(s, d) { return s*d; } (function($, helper) { var someHelperFunction = function(s, d) { return helper(s, d); } var someOtherHelperFunction = function(s) { return s*2; } // here goes the normal plugin code })(jQuery, myActualImplementationTestTheHellOutOfMe); 
+1
source share

You might also like to consider JavaScript suggestions. There are two implementations that I know of: Ian Bicking doctest.js , and my own doctest .

+1
source share

look at qunit , JavaScript unit test lib

-3
source share

All Articles