Lately these days the new release of Meteor reaches the shelves and I was inspired to check for the actual test environment. This because I would like to drive the test-first direction for my further development with Meteor.

The Start

First get the new release. The shell is your friend. So open a command shell and wait for the things to come up.

I hope you are working with a real development environement like Mac-OS or Linux — so update to the current release of Meteor with the following command:

Otherwise use the downloader for windows ;-)

After that we will create a meteor application skeleton with:

Then delete everything — we only want to write a simple unit test.

Okay here we are — with nothing.

The Driver

Meteor runs test using a test driver. A test driver is a test environment running your tests and gives you feedback about the test results and nothing else.

As always, you have to add this driver to your project. We use Mocha, so the following command should do, but …

… it does not get you ready. You will get errors, when using. A little help from stackoverflow shows it would be better to use

First hurdle taken.

The Test

Now it is time to write our first test.

I suggest we write a simple function, calculating the maximum for an array of values, called max.

Create a file max.js with the following content.

At the moment it simply does nothing.

The first test is placed in max.spec.js. Meteor automatically executes all tests given in test files that matches *.test[s].* or *.spec[s].*.

First we have to import Chai installed with Mocha, our test driver, to be able to check the test results against our expectations. Naturally we should import our function under test.

Okay, and then our first test.

The Run

Let’s see. We first have to start the test driver:

After a while you should see

Fire up the browser on port 3000.

Tata. We are in the game.

The test runs on the server and on the client, because it was placed in the root directory. Gotcha.

Click on the test description. Neat, it shows the source and the line number.

And what does the play icon do? It runs this test again and only this test. Okay.

The Playground

Let`s move the files into a server directory created upfront.

Bam. Test is only executed on the server side of life.

Move the file max.spec.js into the root, leaving the max.js in the server directory. Test is not executed. Adjust the import path to:

It runs again. That means use always relative paths with a / or a ./ if you are not in the same path as the module under test.

Adding a second tests leads to

Still the implementation is sufficient. Okay, real values.

Gives us

Yeah, we have to implement something more useful in max.js.

We are in the game again.

The Errors

So let’s try to produce some errors. First we add an error to the module max.js.

The test driver gives us nothing — no hint what’s wrong.

The same within the spec. That’s bad.

So always write tests in minimal steps, because you will not get much help from the environment.

The Conclusion

Metor adds a nice environment for testing but be aware about the caveats it contains.

Developer since the 80'th, working at Forschungszentrum Jülich (http://www.fz-juelich.de) …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store