How can I test the "private" IIFE methods?

Suppose I have this:

var myObj = (function() {
    function private1() {
        console.log("private");
    }

    return {
        public1: function() {
            private1();
            console.log("public");
        }
    }
})();

      

How can I test the private1 function?

+3


source to share


1 answer


Check this out How to check private functions in javascript . In summary, you can achieve what you want by improving your build process. If you are using grunt to build your code, you can add some code snippets that expose the private elements of your IIFE only during testing.



Also you can try this selectizer build tool . With it, you can define your private1 function as a module and then test it just by loading the function with requireJS.

+4


source







All Articles