简体   繁体   中英

How can i test “private” methods of an IIFE?

Supose that i have this:

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

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

How could i test the "private1" function?

Check this out How to test private functions in javascript . Summarizing the post, you could achieve what you want by improving your build process. If you use grunt to build your code then you could add some pieces of code that exposes the private elements of your IIFE only at testing time.

Also you may want to try this build tool selectizer . With it you could define your private1 function as a module and then testing it by just loading the function with requireJS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM