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 assertions


Now that you have a place for testing your code, you need some way of verifying that your code runs as expected. For this, you need an assertion testing library.

There are many assertion testing libraries for many programming styles, but here we're going to use the one that already comes bundled with Node.js, the assert module. It contains the smallest set of utility functions you need to describe what expectations you have for each test. At the top of each testing file, you need the assertion library using require:

var assert = require('assert');

Note

You can assert the "truthiness" of any expression. "Truthy" and "falsy" are concepts in JavaScript (and other languages), where type coercion allows certain values to equate to Boolean true or false, respectively. Some examples are as follows:

var a = true;
assert.ok(a, 'a should be truthy');

The falsy values are:

  • false

  • null

  • undefined

  • the empty string

  • 0 (the number zero)

  • NaN

All other values are truthy.

You can also test for equality...