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).

Dharmik October 8, 2013 at 3:25 pm

Hi,Bill Dawson

Its really helpful for me . Good Work. 🙂

Bill Dawson July 29, 2013 at 4:43 pm

Hi Max, thank you very much for your comment and I’m sorry it took so long to moderate it.

I don’t have any specific examples, but you’ll want to check out my second post on the subject, “tijasmine turns 1.0.0“. It discusses asynchronous tests, which is what you’ll need for testing UI. Come to think of it, there is an example in that posting I believe.

Thank you,
Bill

Max July 26, 2013 at 2:34 am

Hey Bill!
I’ve been looking around on the web to find stuff about unittesting Titanium apps, and I’m surprised how little documentation is available. I found your post and managed to get it working, thanks heaps for sharing your code and knowledge!
Would you have an example to test UI related things? Lets say, I wanna test for the main window to display, the button to be visible, and the second window to show when I click the button. Any ideas on this?
Thanks in advance, Max

Bill Dawson April 15, 2013 at 6:51 pm

Thanks, Aaron. Hopefully it works in iOS. I plan on testing today.

Aaron April 15, 2013 at 5:51 pm

good stuff, nicely integrated

Comments on this entry are closed.

{ 2 trackbacks }

Previous post:

Next post: