Test-Driven JavaScript Development

Book description

Learn JavaScript test-driven development using popular frameworks and tools

About This Book

  • Learn the life cycle of TDD and its importance in real-world application
  • Gain knowledge about popular tools and analyze features, syntax, and how they help in JavaScript testing
  • Implement test-driven programming exercises using the practical code examples

Who This Book Is For

If you have an intermediate knowledge of HTML, CSS, and JavaScript and want to learn how and why the test-driven development approach is better for your assignments, then this book is for you.

What You Will Learn

  • Basic TDD fundamentals, life cycle, and benefits
  • Become acquainted with the concepts and elements of unit testing and writing basic unit tests for JavaScript
  • Understand the way JsUnit, Qunit, Karma and DalekJs work
  • Use the Jasmine framework
  • Interpret feature detection and devise tests specific to cross-browser compatibility
  • Integrate jsTestDriver with Eclipse and run tests with jsTestDriver
  • Explore re-factoring, adding and notifying observers
  • Understand test-driven development in case of server-side JS

In Detail

Initially, all processing used to happen on the server-side and simple output was the response to web browsers. Nowadays, there are so many JavaScript frameworks and libraries created that help readers to create charts, animations, simulations, and so on. By the time a project finishes or reaches a stable state, so much JavaScript code has already been written that changing and maintaining it further is tedious. Here comes the importance of automated testing and more specifically, developing all that code in a test-driven environment. Test-driven development is a methodology that makes testing the central part of the design process ? before writing code developers decide upon the conditions that code must meet to pass a test. The end goal is to help the readers understand the importance and process of using TDD as a part of development.

This book starts with the details about test-driven development, its importance, need, and benefits. Later the book introduces popular tools and frameworks like YUI, Karma, QUnit, DalekJS, JsUnit and goes on to utilize Jasmine, Mocha, Karma for advanced concepts like feature detection, server-side testing, and patterns. We are going to understand, write, and run tests, and further debug our programs. The book concludes with best practices in JavaScript testing. By the end of the book, the readers will know why they should test, how to do it most efficiently, and will have a number of versatile tests (and methods for devising new tests) to get to work immediately.

Style and approach

Easy-to-follow guide with suitable examples for developing JavaScript code in the test-Driven environment, with popular tools and frameworks. User experience and statements are also included to help readers make a better choice of tool for real-world projects.

Table of contents

  1. Test-Driven JavaScript Development
    1. Table of Contents
    2. Test-Driven JavaScript Development
    3. Credits
    4. About the Authors
    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. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Overview of TDD
      1. Complexity of web pages
      2. Understanding test-driven development
        1. The need for testing
        2. Types of testing
        3. The life cycle of TDD
        4. TDD microcycle
      3. Agile and TDD
      4. Benefits of TDD and common myths
        1. Benefits
        2. Myths
      5. Summary
    9. 2. Testing Concepts
      1. Unit testing
        1. Unit testing frameworks
        2. YUI Tests
        3. Following the process
        4. Preparing the environment
        5. Following the life cycle
          1. Writing a test
          2. Running the test and seeing if test fails
          3. Writing a production code
          4. Running all tests
          5. Cleaning up the code
          6. Repeat
        6. Using the browser console
        7. setUp() and tearDown()
        8. Test suites
        9. Actions and assertions
          1. Actions
          2. Assertions
      2. Benefits and pitfalls
        1. Benefits of unit testing
        2. Pitfalls of unit testing
      3. Summary
    10. 3. Testing Tools
      1. JsUnit
        1. Getting started
        2. Writing tests
        3. Running tests
      2. QUnit
        1. Getting started
        2. Writing tests
        3. Running tests
      3. Karma with Jasmine
        1. Getting started
        2. Writing tests
        3. Running tests
      4. DalekJS
        1. Getting started
        2. Writing tests
          1. Actions
          2. Assertions
        3. Running tests
      5. Summary
    11. 4. Jasmine
      1. Understanding behavior-driven development
      2. Setting up Jasmine
      3. describe and specs
      4. Expectations
      5. Matchers
      6. Set up and tear down
      7. Spies
        1. Tracking spies using calls
        2. Creating a custom spy
      8. Jasmine clock
      9. Creating a custom matcher
      10. Creating a custom equality tester
        1. Asynchronous calls
          1. The Jasmine Ajax plugin
      11. Nesting suites
        1. Disabling suites and specs
      12. Summary
    12. 5. JsTestDriver
      1. JsTestDriver
        1. Overview
        2. Getting started
        3. Writing tests
        4. Assertions
        5. Capturing the browser
        6. Restructuring your project
        7. The configuration file
        8. Running tests using the command prompt
      2. Setting up JsTestDriver with IDE
        1. Running the tests
        2. Running Jasmine specs
      3. Code coverage
      4. Summary
    13. 6. Feature Detection
      1. Understanding feature detection
        1. Available methods and libraries
      2. has.js
        1. Writing custom tests
      3. Modernizr
        1. Downloading and setting up Modernizr
        2. How it works
        3. Using Modernizr
        4. Polyfills
        5. Loading polyfills
        6. Supported browsers
      4. Browser detection
        1. User agent sniffing
        2. Object detection
      5. Features testing with Modernizr
        1. CSS features
        2. HTML5 features
        3. Miscellaneous features
        4. Plugins for additional tests
      6. Modernizr methods
        1. Modernizr.prefixed()
        2. Modernizr.prefixedCSS()
        3. Modernizr.mq()
        4. Modernizr.on()
        5. Modernizr.atRule()
        6. Modernizr.addTest()
        7. Modernizr.testStyles()
        8. Modernizr.testProp()
        9. Modernizr.testAllProps()
        10. Modernizr.hasEvent()
        11. Modernizr._prefixes and Modernizr._domPrefixes
      7. Undetectable features
      8. Summary
    14. 7. Observer Design Pattern
      1. Overview
      2. When to use it
      3. Advantages
      4. Disadvantages
      5. Different implementations of the observer
        1. Event dispatcher/listener
        2. Publish/subscribe
          1. Push versus pull
          2. Sample code
          3. Understanding patterns in API
            1. The notify method
            2. Subscribe
            3. Unsubscribe
      6. Observer with topics
      7. Observer pattern using jQuery.Callbacks
      8. Implementing the observer pattern using TDD
        1. Red step (code with error)
        2. Green step (with running code)
        3. Refactoring
          1. Hot swapping components
          2. Browser compatibility of the observer API
      9. Summary
    15. 8. Testing with Server-Side JS
      1. Setting up the environment
        1. Installing Node.js
        2. Setting up the application
        3. Choosing a test runner
        4. Mocha and Chai
      2. Server-side unit testing
        1. Implementing the web server
        2. Helpdesk – sample application
        3. Setting up the MongoDB database
        4. The Mocha test
      3. Summary
    16. 9. Best Practices
      1. TDD best practices
      2. Follow proper rules to define test cases
        1. Make test case names more readable
        2. Keep the same name for test files and source files
        3. Keep the name descriptive
      3. Applying proper processes
        1. Making sure that tests are written before starting implementation
        2. Modifying/writing new code only when the test is failing
        3. Running all the tests when we modify anything in the existing code
        4. Existing tests should pass before new tests are written
        5. Cleaning up code once all test cases are passed
      4. Follow right development practices
        1. Simple code that can be easily understood
        2. Writing assertions first
        3. One assertion per test is enough
        4. Keep your focus on findability
        5. Reducing duplication
        6. Tests should run fast
        7. Using mocks
        8. Using stubs
        9. Using setUp and tearDown methods
      5. Choosing the right tool
        1. Feature detection tools
        2. Server-side testing tools
        3. Asynchronous testing
        4. Running time of unit tests
        5. Browser support
        6. Other features
        7. JsLint
      6. Summary
    17. Index

Product information

  • Title: Test-Driven JavaScript Development
  • Author(s): Ravi Kumar Gupta, Hetal Prajapati, Harmeet Singh
  • Release date: December 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781782174929