TDD With Meteor: Mocha-web

I been doing research recently on test driven development practices and tools for developing web applications with Meteor platform. While there is no official TDD approach, at least at the moment of writing, I was able to find several techniques contributed by growing community.

Here I’ll start with mocha-web which is smart package available through the Atmosphere repository.


meteor-mocha-web allows you to easily and safely run mocha tests within the Meteor framework. This means you can write tests that use and examine Meteor collections.

And if you already familiar with the Mocha you’ll be able to start TDD in no time. The process fairly the same as you usually do on node and the browser.

Refer to the official repository for the further instructions and examples, there even the full Meteor project with tests available. Here I just leave few practical tips you might be interested in.

Tip 1: Custom database name for tests

As you might be noticed the mocha-web examples running on the same database as your development instance of Meteor, so, the same database name is used for the development and testing, but this is not always useful especially as your project grow. At some point you might need to reset your test database while keep development data untouched.

Here is quick workaround on using different database name for testing:

  1. Firstly, run development instance as usually:

  2. And then, in a different terminal, run another instance of Meteor like this:

     METEOR_MOCHA_TEST_DIR=tests MONGO_URL='mongodb://' mrt -p 8000

    As you can see, with this instance we’re connecting to the mongodb bootstraped by the first instance, this way we can use the same mongodb server, but with a different database names for testing and development purposes;

  3. Finally, tests will be available on http://localhost:8000, so, open it in a browser or use with PhantomJS for tests execution.

NOTE: mrt is a shortcut for the Meteorite which is version manager and package manager for Meteor, which is required for those samples to work.

Tip 2: Continuous feedback with Grunt

Running the tests manually or in a browser might become tedious, especially during active development phase, lucky we’ve a Grunt - The JavaScript Task Runner. Basically we could setup it to watch project directories and tests files for changes and then run the actual tests whenever something is changed.

I modified leaderboard-mocha example to make it work with Grunt, see README file and recent commits on the grunt branch on my fork

Also, it would be nice to add Growl notifications as well as run tests on modified files only. I didn’t looked how well Grunt works with Growl, but there seems still a problems to trigger change event on individual files.


In the next post I’m going to cover Karma - Spectacular Test Runner for JavaScript.