thetallweeks
2/16/2016 - 12:46 AM

A modified version of Phillip Walton's concept for unit testing private methods

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;
}());