nlemza
Last Updated: March 07, 2016
·
2.326K
· mutahhir
2034643609a3d51207127b00dfacce5f

Tesacular + Jasmine for high-productivity testing

Testacular (inspite of the somewhat unfortunate naming imo) is one of the best things to happen to javascript TDD/BDD. It's not a testing library, just a test suite runner. You can view the project page here for a nice video introduction here.

Why should you be bothered? Here's why:

  • Testacular can run your test suite simultaneously on all browsers present on your system

  • Testacular watches your project and whenever you change any file, it re-runs your test suite automatically. (No Refresh buttons required all the time)

  • It's javascript testing framework agnostic. However, supports Jasmine, Mocha, and AngularJS's scenario runner out of the box. (Means you don't have to download/install these separately within your project)

  • If you've got JetBrains WebStorm, it integrates tightly with that. (I don't have it and hence don't know how well it works)

Words don't explain how amazing it is. It takes seconds to get up and running. Here's what you do: (Assuming you've got node.js installed, if not, go do that now. Binary installs for almost all platforms are available. Takes a second)

npm -g install testacular

This will install everything you need including the Jasmine Testing Framework. I use Jasmine for all my testing needs and find it amazing. You can use your favourite testing framework.

When I'm using jasmine for a new project, I find it cumbersome to download and set up the html file for running specs. With Testacular, I don't have to worry about this.

In your project file, just run

testacular init

It asks you a few questions regarding browsers and which files to look for and which files to skip. You can add entire subdirectories using the js/**/*.js filter which basically means that all files with the .js extension under the js folder hierarchy should be included in the tests

You should add your source files (files containing code to be tested) before the test files (files containing jasmine specs). I think the other way around might cause problems, though I haven't tested it yet.

That's it, just write your specs as normal and then code them out. You can run testacular with the command:

testacular start

which will run in the background and will re-run the tests automatically whenever a file is saved.

If you're new to Jasmine, it has a nice simple way of writing tests. A test is simply written like this:

it('should know how to say hello', function() {
    expect('hello'.length).toEqual(5);
});

if you need to group tests (which you always should), you can group them under a describe clause:

describe('Conversation', function() {
    it('should greet', function() { ... });
    it('should say bye', function() { ... });
});

You can nest describe clauses. There's a lot of cool functionality available within the jasmine library, and you should definitely check it out.

Some interesting tips if you're using Testacular with Jasmine, you can force Testacular to run a specific test by editing it to:

iit('should know how to say hello', function() { ... });

Notice the iit instead of it. Similarly if you want to run an entire group, you can modify describe to ddescribe. Similarly if you want some tests not to run, you can prepend an x before them. So, specs that shouldn't be run become xit and entire suites can be suspended by changing describe to xdescribe.

I was a little skeptical when I first found out about Testacular (through AngularJS, which is the parent project), and whether I can use it with my own non-angular projects; once I started using it, I found it a real bother to go back to the standard way of refreshing multiple browsers and waiting for the results. Try it out and let me know how you find it.