Jasmine JavaScript Testing

Book description

If you want to take your JavaScript coding to a new level of reliability and proficiency, this book is for you. Discover how to use Jasmine for unit testing whilst learning best practice for the whole JavaScript universe.

  • Learn the power of test-driven development while creating a fully-featured web application
  • Understand the best practices for modularization and code organization while putting your application to scale
  • Leverage the power of frameworks such as BackboneJS and jQuery while maintaining the code quality
  • Automate everything from spec execution to build; leave repetition to the monkeys

In Detail

From a little renegade scripting language to the de facto standard platform of today, JavaScript has become a universal language available in the widest range of devices; it is truly the 'write once, run everywhere’ language. However, as JavaScript applications become more complicated, testing and applying sustainable software engineering practices also become mandatory.

Jasmine JavaScript Testing is a practical guide to a more sustainable JavaScript development process. You will learn by example how to drive the development of a web application using tests and best practices.

This book is about becoming a better JavaScript developer. So, throughout the chapters, you will not only learn about writing tests, but also about the best practices for writing software in the JavaScript language. This book is about acknowledging JavaScript as a real platform for application development and leveraging all of its potential. You will also learn about tooling and automation and how to make your life easier and more productive.

You will learn how to create a sustainable codebase with the help of Jasmine. We will take a look at integrated testing (with a backend NodeJS server) and how you can speed this process up by faking AJAX requests. As you progress through the book, you will learn about the challenges of testing an application built on top of a framework and how you can prevent your application from suffering from dependency management hell. Also, since your applications need to get into production, you will learn about optimizing the code to reduce the number of requests the browser needs to make while loading your application.

With this book, you will learn everything you need to know to become a real professional in the ever-demanding JavaScript universe.

Table of contents

  1. Jasmine JavaScript Testing
    1. Table of Contents
    2. Jasmine JavaScript Testing
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. Who this book is for
      3. Conventions
      4. Reader feedback
      5. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Getting Started with Jasmine
      1. JavaScript – the bad parts
      2. Jasmine and Behavior-Driven Development
      3. Downloading and first run
      4. Summary
    9. 2. Your First Spec
      1. The Investment Tracker application
      2. Jasmine basics and thinking in BDD
      3. Setup and teardown
      4. Nested describes
        1. Setup and teardown
        2. Coding a spec with shared behavior
      5. Understanding matchers
        1. Custom matchers
        2. Built-in matchers
          1. The toEqual built-in matcher
          2. The toBe built-in matcher
          3. The toBeTruthy and toBeFalsy matchers
          4. The toBeUndefined, toBeNull, and toBeNaN built-in matchers
          5. The toBeDefined built-in matcher
          6. The toContain built-in matcher
          7. The toMatch built-in matcher
          8. The toBeLessThan and toBeGreaterThan built-in matchers
          9. The toThrow built-in matcher
      6. Summary
    10. 3. Testing Frontend Code
      1. Thinking in terms of components (Views)
      2. The module pattern
      3. Using HTML fixtures
      4. Basic View coding rules
        1. The View should encapsulate a DOM element
        2. Integrate Views with observers
      5. Testing Views with jQuery matchers
        1. The toBe jQuery matcher
        2. The toContainHtml jQuery matcher
        3. The toContain jQuery matcher
        4. The toHaveValue jQuery matcher
        5. The toHaveAttr jQuery matcher
        6. The toBeFocused jQuery matcher
        7. The toBeDisabled jQuery matcher
        8. More matchers
      6. Summary
    11. 4. Asynchronous Testing – AJAX
      1. Setting up the Scenario
        1. Installing Node.js
        2. Coding the server
        3. Running the server
      2. Writing the spec
        1. The waitsFor() function
        2. The runs() function
      3. Summary
    12. 5. Jasmine Spies
      1. The "bare" Spy
      2. Spy an object's functions
      3. Testing DOM events
      4. Summary
    13. 6. Light Speed Unit Testing
      1. Jasmine Stubs
      2. Sinon.JS
        1. Installing Sinon.JS
        2. A Fake XMLHttpRequest
        3. A Fake server
      3. Summary
    14. 7. Testing Backbone.js Applications
      1. The Backbone model
        1. Declaring a new model
        2. The model attributes
        3. Default attribute values
        4. Events
        5. Sync and AJAX requests
      2. Testing Backbone collections
        1. Declaring a new collection
        2. Sync and AJAX requests
      3. Testing Backbone Views
        1. Declaring a new View
        2. The el property
        3. Rendering
        4. Updating the View on model changes
        5. Binding DOM events
      4. Testing Backbone routers
        1. Defining a new router
        2. Defining routes
        3. Using routers
        4. Routers should only route
      5. Summary
    15. 8. Build Automation
      1. RequireJS
        1. Module definition
        2. Project setup
          1. The new SpecRunner.JS file
          2. The RequireJS configuration
            1. Using non AMD dependencies with Shim
        3. Testing a module
        4. Optimizing for production
      2. Headless testing with PhantomJS
        1. Downloading and installing PhantomJS
        2. Running your tests without opening a browser
      3. Grunt
        1. Installation
        2. Project setup
        3. A RequireJS optimizer task
        4. A Jasmine task
        5. Watch for changes and run the specs
      4. Managing NPM dependencies
      5. Summary
    16. 9. Conclusion
    17. Index

Product information

  • Title: Jasmine JavaScript Testing
  • Author(s):
  • Release date: August 2013
  • Publisher(s): Packt Publishing
  • ISBN: 9781782167204