{"id":36259,"date":"2016-06-22T10:19:38","date_gmt":"2016-06-22T17:19:38","guid":{"rendered":"http:\/\/webdesignledger.com\/?p=36259"},"modified":"2016-06-22T10:19:38","modified_gmt":"2016-06-22T17:19:38","slug":"unit-testing-private-methods-javascript","status":"publish","type":"post","link":"https:\/\/webdesignledger.com\/unit-testing-private-methods-javascript\/","title":{"rendered":"Unit Testing Private Methods in Javascript"},"content":{"rendered":"<p>JavaScript is growing up. \u00a0As it matures, we see more and more improvement associated with JavaScript testing and deployments. \u00a0One area I get a lot of questions about is unit testing, more specifically, unit testing private methods.<\/p>\n<div id=\"attachment_36260\" style=\"width: 946px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-36260\" class=\"wp-image-36260 size-full lazyload\" data-src=\"https:\/\/storage.googleapis.com\/webdesignledger.pub.network\/WDL\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM.png\" alt=\"Screen Shot 2016-06-10 at 3.26.18 PM\" width=\"936\" height=\"520\" data-srcset=\"https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM.png 936w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM-500x278.png 500w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM-768x427.png 768w\" data-sizes=\"(max-width: 936px) 100vw, 936px\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" style=\"--smush-placeholder-width: 936px; --smush-placeholder-aspect-ratio: 936\/520;\" \/><noscript><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-36260\" class=\"wp-image-36260 size-full\" src=\"https:\/\/storage.googleapis.com\/webdesignledger.pub.network\/WDL\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM.png\" alt=\"Screen Shot 2016-06-10 at 3.26.18 PM\" width=\"936\" height=\"520\" srcset=\"https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM.png 936w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM-500x278.png 500w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.26.18-PM-768x427.png 768w\" sizes=\"(max-width: 936px) 100vw, 936px\" \/><\/noscript><p id=\"caption-attachment-36260\" class=\"wp-caption-text\">Traditional revealing module pattern.<\/p><\/div>\n<p>When dealing with this issue, there are two main approaches commonly used. \u00a0 The first is to test the method through another public method that calls it. \u00a0The other would be to expose it publicly even if the only thing that needs access to it are the unit tests. \u00a0Neither 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\" style=\"width: 896px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-36261\" class=\"wp-image-36261 size-full lazyload\" data-src=\"https:\/\/storage.googleapis.com\/webdesignledger.pub.network\/WDL\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM.png\" alt=\"Screen Shot 2016-06-10 at 3.27.03 PM\" width=\"886\" height=\"534\" data-srcset=\"https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM.png 886w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM-500x301.png 500w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM-768x463.png 768w\" data-sizes=\"(max-width: 886px) 100vw, 886px\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" style=\"--smush-placeholder-width: 886px; --smush-placeholder-aspect-ratio: 886\/534;\" \/><noscript><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-36261\" class=\"wp-image-36261 size-full\" src=\"https:\/\/storage.googleapis.com\/webdesignledger.pub.network\/WDL\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM.png\" alt=\"Screen Shot 2016-06-10 at 3.27.03 PM\" width=\"886\" height=\"534\" srcset=\"https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM.png 886w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM-500x301.png 500w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.27.03-PM-768x463.png 768w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/noscript><p id=\"caption-attachment-36261\" class=\"wp-caption-text\">Testing your private method by calling a public method that calls it.<\/p><\/div>\n<p>Start off by considering your objective. \u00a0My objective is to be able to make changes to the code-base with confidence. \u00a0Additionally, 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. \u00a0If there\u2019s a problem with the logic, it can be cumbersome to track down the issue. \u00a0On 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. \u00a0All of the methods doing their one job adds up to a complete workflow. \u00a0If one of the methods fails to do its job, it\u2019s quick and easy to identify where the issue lies. \u00a0 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. \u00a0I will follow a classic module pattern and expose publicly only those methods that are called externally from outside the class. \u00a0Then I create a \u2018testObject\u2019 and expose it publicly. \u00a0The properties of my testObject are all the private methods that I otherwise would not be able to test. \u00a0Now all my private methods are exposed publicly. \u00a0While it\u2019s not nearly as safe as keeping them private, at least I know that \u00a0I should only ever see \u2018class.testOject.somePrivateMethod\u2019 being called from within my unit tests. \u00a0If 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\" style=\"width: 912px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-36262\" class=\"wp-image-36262 size-full lazyload\" data-src=\"https:\/\/storage.googleapis.com\/webdesignledger.pub.network\/WDL\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM.png\" alt=\"Screen Shot 2016-06-10 at 3.28.18 PM\" width=\"902\" height=\"646\" data-srcset=\"https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM.png 902w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM-500x358.png 500w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM-768x550.png 768w\" data-sizes=\"(max-width: 902px) 100vw, 902px\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" style=\"--smush-placeholder-width: 902px; --smush-placeholder-aspect-ratio: 902\/646;\" \/><noscript><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-36262\" class=\"wp-image-36262 size-full\" src=\"https:\/\/storage.googleapis.com\/webdesignledger.pub.network\/WDL\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM.png\" alt=\"Screen Shot 2016-06-10 at 3.28.18 PM\" width=\"902\" height=\"646\" srcset=\"https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM.png 902w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM-500x358.png 500w, https:\/\/webdesignledger.com\/wp-content\/uploads\/2016\/06\/Screen-Shot-2016-06-10-at-3.28.18-PM-768x550.png 768w\" sizes=\"(max-width: 902px) 100vw, 902px\" \/><\/noscript><p id=\"caption-attachment-36262\" class=\"wp-caption-text\">Hybrid testing pattern. Exposing your private methods through a single test object.<\/p><\/div>\n<p>It\u2019s a great pattern and has worked very well for several enterprise\u00a0implementations. \u00a0It\u2019s not perfect but is a good compromise in most cases. \u00a0You\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. \u00a0As it matures, we see more and more improvement associated with JavaScript testing and deployments. \u00a0One area I get a lot of questions about is unit testing, more specifically, unit testing private methods. When dealing with this issue, there are two main approaches commonly used. \u00a0 The first is to test [&hellip;]<\/p>\n","protected":false},"author":1917,"featured_media":36262,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476,490],"tags":[8818,8819,8817],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/posts\/36259"}],"collection":[{"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/users\/1917"}],"replies":[{"embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/comments?post=36259"}],"version-history":[{"count":0,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/posts\/36259\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/media\/36262"}],"wp:attachment":[{"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/media?parent=36259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/categories?post=36259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webdesignledger.com\/wp-json\/wp\/v2\/tags?post=36259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}