How to test a server side function in Meteor without including it in global namespace

123 Views Asked by At

In my server side file I have two functions defined for which I want to write test cases which are residing in a file in tests directory.

~PRJ_DIR/server/file1.coffee

calcSha1Hash = (params) ->
    .... logic...
anotherFunc = () ->
    ..somelogic..
    x = calcSha1Hash(params)

~PRJ_DIR/tests/server/file1.coffee

MochaWeb?.testOnly () ->
  describe.only("Hash generation.  ", () ->
    it(" calcSha1Hash returns Hash.", (dn) ->
      dataDict = {email: '[email protected]'}
      hash = calcSha1Hash (dataDict)
      chai.assert.isDefined(hash)
      dn()
    )
  )

How is it possible to call the server side func(calcSha1Hash) in my test case in Meteor

1

There are 1 best solutions below

0
On BEST ANSWER

Unless there is a meteor-specific way to do this, you can implement the solution from How to Unit Test Private Functions in JavaScript

You will need to have a single global variable (something like TestAPI). Then add your functions to it inside of a closure so you can access them from anywhere.

Here's an example from the article:

var myModule = (function() {

  function foo() {
    // private function `foo` inside closure
    return "foo"
  }

  var api = {
    bar: function() {
      // public function `bar` returned from closure
      return "bar"
    }
  }

  /* test-code */
  api._foo = foo
  /* end-test-code */

  return api
}())

Someone might have something better, but that's a start.