Book Image

AngularJS Test-driven Development

By : Tim Chaplin
Book Image

AngularJS Test-driven Development

By: Tim Chaplin

Overview of this book

<p>Starting with reviewing the test-driven development (TDD) life cycle, you will learn how Karma and Protractor make your life easier while running JavaScript unit tests. You will learn how Protractor is different from Selenium and how to test it entirely. This book is a walk-through to using TDD to build an AngularJS application containing a controller, model, and scope.</p> <p>Building on the initial foundational aspects, you will expand to include testing for multiple controllers, partial views, location references, CSS, and the HTML element. In addition, you will explore using a headless browser with Karma. You will also configure Karma file watching to automate testing and tackle components of AngularJS (controller, service, model, and broadcasting) using TDD. At the end of this book, you will extend explore how to pull data using an external API, setting up and configuring Protractor to use a standalone Selenium server, and setting up Travis CI and Karma to test your application.</p> <p>This book is a complete guide to testing techniques using Karma for unit testing and performing end-to-end testing with Protractor.</p>
Table of Contents (17 chapters)
AngularJS Test-driven Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Test setup


In order to run Karma properly, we will need to add the following development dependencies:

  • karma: The base Karma installation

  • karma-jasmine: The test runner

  • karma-phantomjs-launcher: The PhantomJS headless browser plugin we discussed and set up in Chapter 5, Flip Flop

Install the following Karma dev dependencies:

$ npm install karma --save-dev
$ npm install karma-jasmine --save-dev
$ npm install karma-phantomjs-launcher --save-dev

Test scripts

When using Travis CI, a script to run the tests needs to be defined. The best place to define a script is in the package.json file. The package.json file is used in several ways by node.js. Here are the steps to run the test:

  1. The test script can then be run when you type the following command in the command prompt:

    $ npm test
    
  2. Update the package.json scripts section as shown in the following code snippet:

    "scripts": {
        "start": "node app.js",
        "test" : "karma start --single-run --browsers PhantomJS"
    }
  3. Confirm that the test script works:

    $ npm test
    

PhantomJS allows tests to run on the Travis CI servers without the need for a UI. The following is a sample output:

The application setup is now configured to run unit tests via the npm test command. This will be used by Travis CI to run the tests.