An Appcelerator Titanium Jasmine (Javascript Testing) Module

by Bill Dawson on 15 April 2013

This has likely been done before somewhere, but I’ve gone ahead anyway and created a little CommonJS module called tijasmine which provides a way to run jasmine (Javascript BDD framework) test suites in an Appcelerator Titanium application. I also want to give credit to Aaron Saunders and his ci.behave.test repo at Github, which inspired me to continue with tijasmine.

I usually use Anvil (the testing framework in Titanium) but I’m working on something that requires me to kick-off tests from inside an application after doing something in the app’s UI first. Anvil is not really suited for that. In other words, I wanted to run an app and then, within that app, kick off some tests on demand.

The README.md gives the basic setup and usage instructions, but I’ll repeat some of it here. Basically you’ll want to dump jasmine.js, tijasmine.js and tijasmine-console.js together somewhere under your Resources/ folder, then write some spec files. Here’s a sample spec file (which you’ll also find under example/ in the repo):

require("/tijasmine/tijasmine").infect(this);

describe("My First Suite", function() {
    it("ensures that true is true", function() {
        expect(true).toEqual(true);
    });
    it("shows what a failure does", function() {
        expect(false).toEqual(true);
    });
});

describe("My Second Suite", function() {
    it("ensures that 'a' is still 'a' in this world", function() {
        expect("a").toEqual("a");
    });
    it("ensures that 'b' is still 'b' in this world", function() {
        expect("b").toEqual("b");
    });
});

Note the very important first line which calls infect(this) to “infect” (in a good way!) the spec module with the necessary jasmine functions such as describe, it, etc.

Then in your app code you’ll want to kick off the tests with code like this (see example/app.js:

var tijasmine = require("/tijasmine/tijasmine"),
    reporter = new (require("/tijasmine/tijasmine-console").ConsoleReporter)();

tijasmine.addSpecModules("/specs/myspec", "/specs/myotherspec");
tijasmine.addReporter(reporter);
tijasmine.execute();

tijasmine-console is currently the only reporter availabe. It produces console output, such as this example from Android’s logcat:


I/TiAPI   (27297):  -------------------------
I/TiAPI   (27297):  My First Suite:
I/TiAPI   (27297):    -  ensures that true is true (ok)
E/TiAPI   (27297):    -  shows what a failure does (FAILED)
E/TiAPI   (27297):    -  -  Expected false to equal true.
I/TiAPI   (27297):  -------------------------
I/TiAPI   (27297):  My Second Suite:
I/TiAPI   (27297):    -  ensures that 'a' is still 'a' in this world (ok)
I/TiAPI   (27297):    -  ensures that 'b' is still 'b' in this world (ok)
I/TiAPI   (27297):  -------------------------
I/TiAPI   (27297):  My First Suite of another spec:
I/TiAPI   (27297):    -  ensures that true is true (ok)
E/TiAPI   (27297):    -  shows what a failure does (FAILED)
E/TiAPI   (27297):    -  -  Expected false to equal true.
I/TiAPI   (27297):  -------------------------
I/TiAPI   (27297):  My Second Suite of another spec:
I/TiAPI   (27297):    -  ensures that 'a' is still 'a' in this world (ok)
I/TiAPI   (27297):    -  ensures that 'b' is still 'b' in this world (ok)
I/TiAPI   (27297):  =========================
E/TiAPI   (27297):  THERE WERE FAILURES

I encourage you to create other reporters (like maybe one that fills up a TableView on an app window.) Fork, add your reporter and send me pull requests!

Also feel free to enter issues you might have.

NOTE: I have only run this on Android. It could be that differences in the Javascript engines between iOS and Android might mean there is something about this module that will prevent it from running on iOS. Feel free to test on iOS and file an issue if you experience any problems.

I hope some people might find this module helpful. Happy Monday!

Image: “Jasmine” from Flickr user jonathangill: http://www.flickr.com/photos/jonathangill/5421327175/. License: Attribution-NonCommercial 2.0 Generic (CC BY-NC 2.0).