Skip to content

Instantly share code, notes, and snippets.

@thetallweeks
Last active February 16, 2016 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thetallweeks/861cd771ff8ad6373fc0 to your computer and use it in GitHub Desktop.
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
// 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