What is Velocity?


by Josh Owens

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("asks for the players to be primarily in descending score order, then in alphabetical order and returns as is", function () {
      var someLocalCollectionCursor = {};
      Players.find = function (selector, options) {
        expect(options.sort.score).toBe(-1);
        expect(options.sort.name).toBe(1);
        return 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:

Share Share on Twitter Share on Facebook Share on LinkedIn

How Can We Help?

Reaching out doesn’t mean you’re ready to start a project, but we’d love to learn more about the challenge you’re facing, answer any questions, and see if we might be a good fit for working together.

Contact Us