{"id":987,"date":"2017-08-07T21:01:29","date_gmt":"2017-08-07T21:01:29","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=987"},"modified":"2018-11-27T05:55:36","modified_gmt":"2018-11-27T05:55:36","slug":"unit-testing-private-methods-in-javascript","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/javascript\/unit-testing-private-methods-in-javascript\/","title":{"rendered":"Unit Testing Private Methods in Javascript"},"content":{"rendered":"<p>JavaScript is growing up. &nbsp;As it matures, we see more and more improvement associated with JavaScript testing and deployments. &nbsp;One area I get a lot of questions about is unit testing, more specifically, unit testing private methods.<\/p>\n<div id=\"attachment_36260\" class=\"wp-caption alignnone\"><img loading=\"lazy\" class=\"wp-image-36260 size-full\" src=\"https:\/\/i2.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM.png?resize=936%2C520\" sizes=\"(max-width: 936px) 100vw, 936px\" srcset=\"https:\/\/i2.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM.png?w=936&amp;ssl=1 936w, https:\/\/i2.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM.png?resize=500%2C278&amp;ssl=1 500w, https:\/\/i2.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM.png?resize=768%2C427&amp;ssl=1 768w\" alt=\"Screen Shot 2016-06-10 at 3.26.18 PM\" width=\"936\" height=\"520\"><\/p>\n<p class=\"wp-caption-text\">Traditional revealing module pattern.<\/p>\n<\/div>\n<p>When dealing with this issue, there are two main approaches commonly used. &nbsp; The first is to test the method through another public method that calls it. &nbsp;The other would be to expose it publicly even if the only thing that needs access to it are the unit tests. &nbsp;Neither strategy is perfect or ideal but here are a couple of things to consider when choosing which is best for you.<\/p>\n<div id=\"attachment_36261\" class=\"wp-caption alignnone\"><img loading=\"lazy\" class=\"wp-image-36261 size-full\" src=\"https:\/\/i0.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM.png?resize=886%2C534\" sizes=\"(max-width: 886px) 100vw, 886px\" srcset=\"https:\/\/i0.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM.png?w=886&amp;ssl=1 886w, https:\/\/i0.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM.png?resize=500%2C301&amp;ssl=1 500w, https:\/\/i0.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM.png?resize=768%2C463&amp;ssl=1 768w\" alt=\"Screen Shot 2016-06-10 at 3.27.03 PM\" width=\"886\" height=\"534\"><\/p>\n<p class=\"wp-caption-text\">Testing your private method by calling a public method that calls it.<\/p>\n<\/div>\n<p>Start off by considering your objective. &nbsp;My objective is to be able to make changes to the code-base with confidence. &nbsp;Additionally, if an issue presents itself, I want to be able to find and address the issue quickly.<\/p>\n<p>When you have several private methods chained together, it becomes no different to test than one big super method. &nbsp;If there\u2019s a problem with the logic, it can be cumbersome to track down the issue. &nbsp;On the flip side of that, if every method were public, you could test each method and ensure that it\u2019s doing its little bit of work correctly. &nbsp;All of the methods doing their one job adds up to a complete workflow. &nbsp;If one of the methods fails to do its job, it\u2019s quick and easy to identify where the issue lies. &nbsp; The drawback is that we totally destroyed the notion of a public vs. private methods and all the good things that come along with it.<\/p>\n<p>One approach that I\u2019ve taken with several companies is a hybrid. &nbsp;I will follow a classic module pattern and expose publicly only those methods that are called externally from outside the class. &nbsp;Then I create a \u2018testObject\u2019 and expose it publicly. &nbsp;The properties of my testObject are all the private methods that I otherwise would not be able to test. &nbsp;Now all my private methods are exposed publicly. &nbsp;While it\u2019s not nearly as safe as keeping them private, at least I know that &nbsp;I should only ever see \u2018class.testOject.somePrivateMethod\u2019 being called from within my unit tests. &nbsp;If I ever do a code review and see someone calling \u2018class.testObject.*\u2019 in the core code, I know someone is calling the private method in a way it\u2019s not intended.<\/p>\n<div id=\"attachment_36262\" class=\"wp-caption alignnone\"><img loading=\"lazy\" class=\"wp-image-36262 size-full\" src=\"https:\/\/i1.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM.png?resize=902%2C646\" sizes=\"(max-width: 902px) 100vw, 902px\" srcset=\"https:\/\/i1.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM.png?w=902&amp;ssl=1 902w, https:\/\/i1.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM.png?resize=500%2C358&amp;ssl=1 500w, https:\/\/i1.wp.com\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM.png?resize=768%2C550&amp;ssl=1 768w\" alt=\"Screen Shot 2016-06-10 at 3.28.18 PM\" width=\"902\" height=\"646\"><\/p>\n<p class=\"wp-caption-text\">Hybrid testing pattern. Exposing your private methods through a single test object.<\/p>\n<\/div>\n<p>It\u2019s a great pattern and has worked very well for several enterprise&nbsp;implementations. &nbsp;It\u2019s not perfect but is a good compromise in most cases. &nbsp;You\u2019ll want to consider your application and the pros and cons of the various strategies before deciding which is best for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript is growing up. &nbsp;As it matures, we see more and more improvement associated with JavaScript testing and deployments. &nbsp;One area I get a lot of questions about is unit testing, more specifically, unit testing private methods. Traditional revealing module pattern. When dealing with this issue, there are two main approaches commonly used. &nbsp; The [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[12],"tags":[87,91],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2018\/02\/UnitTesting.png","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-fV","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/987"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=987"}],"version-history":[{"count":2,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/987\/revisions"}],"predecessor-version":[{"id":1577,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/987\/revisions\/1577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/988"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}