in a jQuery plugin i have 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, to be able to unit test it, is that possible somehow?
Per this related question, I'd say just test the external interface.
But if you must test these methods in isolation, you'll have to test "copies" of them outside of the context of their deployment as internal methods. In other words, create an object in which they are not inaccessible to client code, and then cobble those versions together with your outer script in a pre-process. Sounds like a lot of work, but hey, that's the point of hiding methods, right? (To make them unreachable.)
If the internal functions need testing that's a good indicator that they should maybe be in a separate module somewhere and injected as dependencies and used within your objects public interface implementation. That's the more "testable" way to do it.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With