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)

Using asynchronous hooks


Any of these functions that you run before any or all your tests can be asynchronous. If a function is asynchronous, simply accept a callback argument like this one:

describe('some feature', function() {
  function runBeforeEach(done) {
    console.log('running afterEach function...');
    setTimeout(done, 1000);
  }
  beforeEach(runBeforeEach);

  it('should do A', function() {
    console.log('test A');
  });

  it('should do B', function() {
    console.log('test B');
  });
});

When running this test code, you will notice the one-second lag before running each test, which—had we not provided the callback argument—would not have been observed.

How hooks interact with test groups

As we've seen, inside a describe scope, you can have respective before, after, beforeEach, and afterEach hooks. If you have a nested describe scope, that scope can also have hooks. In addition to the hooks on the current scope, Mocha will also call the hooks on all the parent scopes. Consider...