Velocity is an officially sanctioned test runner framework for Meteor.js. It allows anyone to add a testing framework package that hooks into meteor, easily. Velocity handles things like setting up a separate instance of your app to run tests against, watching files and reactively rerunning tests on changes, and outputting the test results in a standard formatter so things like the velocity-html-reporter can work. The javascript community has a very vibrant set of testing utilities and the goal is to make it easier to drop those into a meteor app.
Where did Velocity come from?
Velocity came out of a joint online meeting focused on testing featuring Sam Hatoum, Arunoda Susiripala, Adrian Lanning, Michael Risse, Ry Walker, and Josh Owens. The meetup went extremely well, you can actually watch it on youtube. At the end of the meetup we started talking about how to work together making Meteor testing awesome. The host, Robert Dickert, decided he would be the organizer of our group and we started meeting weekly with the goal of building something to make testing easier. What followed was an amazing experience of people working together to make the testing landscape 100% different than what exists before this meetup happened. Things have changed so much that I ripped out old chapters of the Testing with Meteor.js book and replaced them with new chapters about frameworks that are velocity-compatible.
How does it work?
Velocity is super simple to get started with. You actually pick the framework you want to work with and install that. For this post, let's give Jasmine-unit a try by type mrt add jasmine-unit in your app. You will also need to install a reporter as well, so mrt add velocity-html-report to get that going.
Let's start with an easy test for the leaderboard example:
(function () {
"use strict";
jasmine.DEFAULT_TIMEOUT_INTERVAL = jasmine.getEnv().defaultTimeoutInterval = 20000;
describe("Template.leaderboard.players", function () {
it(<span class="hljs-string">"asks for the players to be primarily in descending score order, then in alphabetical order and returns as is"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">var</span> someLocalCollectionCursor = {};
Players.find = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(selector, options)</span> </span>{
expect(options.sort.score).toBe(-<span class="hljs-number">1</span>);
expect(options.sort.name).toBe(<span class="hljs-number">1</span>);
<span class="hljs-keyword">return</span> someLocalCollectionCursor;
};
expect(Template.leaderboard.players()).toBe(someLocalCollectionCursor);
});
});});
Since you are doing unit-testing, you need to mock out some of the calls being made inside the template. Jasmine works by grouping code into blocks, in this case we wrote a describe block and put the test inside of it. Each test is denoted by the it block of code.
The test starts off by setting up a blank javascript object as a stub for a cursor. Then we mock out the Players.find call and set up some expectations on the options being passed in, and finished up the mock by returning our stubbed cursor. Lastly, we set up an expectation that Template.leaderboard.players() should be equal to our stub cursor.
The velocity magic kicks in when we put this test into the tests folder in the root of the app and we name is leaderboard-jasmine-unit.js. Velocity recognizes that the jasmine-unit.js ending is a Jasmine unit test and will send our test to the jasmine unit framework to be run. The jasmine-unit framework will then use the test mirror of the leaderboard app and execute it's test suite and return the results back to velocity for reporting. Now you have the magic of a reactive test suite!
I want more!
I would encourage you guys to look in a few spots if you are interested in learning more about Velocity, testing, etc. First, download the velocity-example app which is building up a sample test suite for every velocity compatible framework. Second, check out the Testing with Meteor.js book I've been writing for the last two months. If you really want to dive in deep, you can also join the velocity core mailing list.
You can also watch the talk from the June Devshop: