14

I am currently looking into the meteor framework and this question immediately jumps to mind. Is code which I write (for example Template.xxx code or Template.xxx.events) actually testable in any way?

Of course you can test code which is not bound to the meteor runtime as you would any other code, but my impression is that most code you will write inside of meteor is somehow scoped to meteor and its functions.

xen
  • 625
  • 1
  • 7
  • 18
  • Here's another example of unit testing with Meteor http://stackoverflow.com/questions/12987525/meteor-test-driven-development/15471731#15471731 – Xolv.io Mar 18 '13 at 07:35

5 Answers5

6

There doesn't seem to be any official test framework yet apart from the undocumented Tinytest (see the video tutorial) and its helpers, but you can always stub/mock out the Meteor framework API like I've done in this trivial example on github.

I imagine it could get a lot harder for non-trivial applications, so it's probably a good idea to separate core application logic away from Meteor API calls.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
dmayo3
  • 73
  • 5
  • Thank you for that sample :) seems like that's what I'm looking for. But it still troubles me a bit that there is no 'easy' way to do this. Especially since you can't always seperate the logic. – xen Sep 03 '12 at 06:35
5

As of February 2014, Meteor code is unit-testable using the built-in Tinytest framework, as long as you move all your application code into packages, which you should do anyway. The quick-and-dirty way amounts to adding a package.js file. That file serves to:

  1. Declare your exports. It's good practice for clean namespacing to have one global object for your app
  2. Declare your test files

Here is an example by Eventedmind - https://github.com/EventedMind/meteor-file

You can see in meteor-file-test.js that it accesses MeteorFile, which is declared as an export in package.js.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • 1
    Thank you for your answer. I have updated the accepted answer to reflect the current state. – xen Mar 03 '14 at 07:35
1

I think it is testable although I haven't looked into it too deeply.

If you open up the liveui package ($METEOR_HOME/packages/liveui) there seems to be quite a few unit tests written using TinyTest and testing the rendering. I think that would be a good place to start:-

  • liveui_tests.js
  • liveui_tests.html

etc.

Hope that helps

Jabbslad
  • 546
  • 3
  • 10
  • Please see my answer for some more insight, here: http://stackoverflow.com/questions/12987525/meteor-test-driven-development/14049725#14049725. tl;dr This is something that the meteor core team already knows is an issue. I am helping bring the issue out into the front, and help them implement some definitive ways to address TDD with meteor. :) – zealoushacker Dec 27 '12 at 05:26
1

I've created a blog post here showing how to do effective unit testing in Meteor, along with an example project on GitHub. Hope it helps.

http://blog.xolv.io/2013/04/unit-testing-with-meteor.html

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
Xolv.io
  • 2,483
  • 1
  • 15
  • 17
1

Velocity has been selected as the official testing framework for meteor 1.0. The announcement has been made in the last meteor devshop (june 2014).

Packages developed with velocity:

Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236