Book Image

Using Node.js for UI Testing

By : Pedro Teixeira
Book Image

Using Node.js for UI Testing

By: Pedro Teixeira

Overview of this book

<p>Automating tests for your user interfaces has always been the holy grail of programming. Now, using Zombie.js and Mocha you can create and quickly run your tests, allowing you to test even small changes. Increase your confidence in the code and minimize the number of times you have to use a real browser while you develop.</p> <p>"Using Node.js for UI Testing" is a quick and thorough guide on how to automatically test your web app, keeping it rock solid and bug-free. You will learn how to simulate complex user behaviour and verify that your application behaves correctly.</p> <p>You will create a web app in Node.js that uses complex user interactions and AJAX; by the end you will be able to fully test it from the command-line. Then you will start creating the user interface tests for this application using Mocha as a framework and Zombie.js as a headless browser.</p> <p>You will also create a complete test suite, module by module, testing simple and complex user interactions.</p>
Table of Contents (15 chapters)

Performing asynchronous tests


Mocha runs all the tests in series, where each test can be synchronous or asynchronous. For a synchronous test, the test callback function should not accept any argument, as in the previous examples. But since Node.js doesn't block I/O operations, and we need to perform I/O operations for each of our tests (making at least an HTTP request to our server), our tests need to be asynchronous.

To make a test asynchronous, the test function should accept a callback function such as this:

it('tests something asynchronous', function(done) {
  doSomethingAsynchronous(function(err) {
    assert.ok(! err);
    done();
  });
});

The done callback function also accepts an error as the first argument, which means that instead of throwing an error, you can just call done directly:

it('tests something asynchronous', function(done) {
  doSomethingAsynchronous(function(err) {
    done(err);
  });
});

If you don't need to test the asynchronous function return value, you can pass the...