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

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: 'johndoe@gmail.com'}
      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

Answers


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.


Need Your Help

jquery not firing for button event?

jquery html image button file-upload

I have a situation where I want to display a graphical button for a user to click to initiate a file upload.

What is the easiest way to make a C++ program crash?

c++ crash

I'm trying to make a Python program that interfaces with a different crashy process (that's out of my hands). Unfortunately the program I'm interfacing with doesn't even crash reliably! So I want t...