Last active
February 16, 2016 00:47
-
-
Save thetallweeks/861cd771ff8ad6373fc0 to your computer and use it in GitHub Desktop.
A modified version of Phillip Walton's concept for unit testing private methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// modified version of: http://philipwalton.com/articles/how-to-unit-test-private-functions-in-javascript/ | |
// uses grunt-strip-code npm package during build | |
var myModule = (function() { | |
var api = { | |
bar: bar | |
}; | |
var _bar = 'bar'; | |
function foo() { | |
// private function `foo` inside closure | |
return "foo"; | |
} | |
function bar() { | |
// public function `bar` returned from closure | |
return _bar; // private variable _bar used in public function | |
} | |
/* test-code */ | |
api._foo = foo; | |
api._bar = _bar; | |
/* end-test-code */ | |
return api; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment