DailyJS

Totally Testacular

18 Oct 2012 | By Alex Young | spacer libraries testing node browser

Vojta Jina created Testacular (GitHub: vojtajina / testacular, License: MIT, npm: testacular) to write better tests for AngularJS, Google’s client-side library that’s rapidly gaining support from the wider web development community.

While established projects like Selenium are currently more complete than Testacular, the goal of bringing 100% of Angular’s tests to Testacular is an important milestone that should bring more attention to the project.

spacer

The design of Testacular itself is actually fairly lightweight. It uses a built-in Node-powered web server to send a test harness to a browser, and then Socket.IO is used for communication between the browser and client. A special page, /context.html, is used to contain tests, and a Node file watcher automatically runs tests when the associated test files change.

A configuration file is used to pull all of this together. It’s basically JavaScript, but uses special variables to set configuration options. For example, files is an array that holds a list of files to load in the browser, and reporters controls how test results are displayed.

Testacular automatically runs a browser instance when testacular start is invoked on the command-line. The entire workflow is optimised around development, so you shouldn’t have to leave your editor to see results. Also, a new instance of a browser is loaded – if you set up Chrome to run tests then your existing Chrome session won’t be interfered with.

Browsers are scripted through “launcher” files. The Chrome launcher includes command-line invocations suitable for Linux, Windows, and Mac OS. It’s also possible to use Internet Explorer and PhantomJS.

Testing cross-platform is also supported through custom launcher files. I found a custom browser example by digging through the project’s documentation. This is intended for invoking browsers through a virtual machine on a Continuous Integration (CI) server, but this could be adapted for local development. In fact, supporting Travis CI was one of Vojta’s initial goals for the project.

Trying Testacular

The Testacular readme has some details on how to get started writing Testacular tests. It’ll work with both Jasmine and Mocha. Let’s say you’re using Jasmine to test an existing project, and have added testacular to your package.json or installed it. All you need to do is run testacular init to create a configuration file, edit it with suitable values for your project, and then start up the Testacular server with testacular start.

Assuming you’ve created a Jasmine Testacular config file, change the files argument as follows:

files = [
  JASMINE,
  JASMINE_ADAPTER,
  'test/*.js'
];

And then add a test file to test/, anything will do:

describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
      expect([1, 2, 3].indexOf(5)).toEqual(-1);
    });
  });
});

Now running testacular start will start up the tests. You should see something like the following screenshot – if not, check your basePath.

spacer

Conclusion

It’s good to see interesting spin-offs coming from MVC projects. Vojta has been working on Testacular for a while now, and it seems like his original plans are coming to fruition. If you’re frustrated with Selenium and work with Node, it might be a natural fit.

P.S. Writing this article without accidentally typing "testicular" was a formidable challenge.

blog comments powered by Disqus
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.