Testing my business logic with Mocha


#1

Hi Meteor folks,

I´ve written a module containing business logic, that I would like to test.

With module, I mean code that fits in one js file, and it only adds one symbol to the global namespace. The module´s interaction with Meteor can be easily mocked.

I´ve tried Velocity but it is not of my taste:

  • not all changes are recongnized.
  • it takes from ten seconds upwards to before I see results.
  • the stack trace shown is not relevant to me.
  • I love the debugger and I´m unsure how to do it.

As Velocity uses mocha, and I wanted to keep the amount of frameworks to a minumun, I choose to use mocha (standalone) for testing this module.

  1. My question now is how do I get access to my module from within a mocha standalone test?

I understand that I need to “import” the module somehow into my test.
The traditional node way would be to use CommonJS and require my module.

Meteor loads js files in some other way. Maybe it would be smarter to use the same mechanism Meteor is using to load my code, rather than using CommonJS?

  1. How does meteor loads the js files?
  2. Can I access the code loading infrastructure from Meteor?
  3. Any other paths that I should consider?

Thanks in advance for you wisdom!


#2

So far this is the solution that I came up with:

// logic.js (My business logic module)
var Doo = function () {};

Doo.prototype.log = function () {
    console.log('doo!');
};

if (typeof Meteor === 'undefined') {
    module.exports = Doo;
};

The if guard is needed to avoid Meteor complaining about module undefined.

In my test I can then require my module with:

var Logic = require('../../logic.js');

#3

I just finished working my way through the labyrinth of Velocity testing a Meteor packaged jQuery plugin and posted a deliberately trivial demo of it on GitHub and AtmosphereJS.

The package is fliptext and the demo is here.

I assume you mean package when you say module. Is that correct?


#4

@warehouseman: No I did not mean a Meteor package when I say module. I just mean a small piece of my code that I want to test. Not big enough to be a standalone package.

Thanks for your skeleton!


#5

I fixed the problem with my demo, (inadvertently deleted file), please try again.

I found it frustrating that the Velocity demos seemed to shy away from clarifying how to interact with different parts of Meteor. I suspect you have found the same, but I am unclear what exactly is giving you difficulty.


#6

My solution is working for my needs now.
I was curious if there was a better solution out there.