Learning Behavior-driven Development with JavaScript

Book description

Create powerful yet simple-to-code BDD test suites in JavaScript using the most popular tools in the community

In Detail

JavaScript is not only widely used to create attractive user interfaces for the Web, but with the advent of Node.js, it is also becoming a very popular and powerful language to write server-side applications. Using behavior-driven development and some common testing patterns and best practices, you will be able to avoid these traps.

This book will show you how to do BDD in a practical way. We will start with a basic introduction of what BDD is and why the classical approach to testing has failed. Afterwards, we will dive directly into an introduction to Node.js, Mocha, and Sinon.JS. Finally, we will cover more advanced subjects such as how to write a fast and effective test suite for a RESTful web API, and how to do the same with a rich UI using Cucumber.js and Protractor.

This book will give you great insight into how to architect your system to make it more testable and modular, but at the same time avoid the nightmare of abusing mock objects.

What You Will Learn

  • Understand the basic concepts of BDD and how it is different from classic unit testing
  • Divide your system into different modules that can be tested separately, but at the same time not falling into the trap of unit testing
  • Use Mocha, Sinon.JS, and Chai to write expressive BDD features
  • Implement Cucumber.js to automate tests written in Gherkin so that your stakeholders can understand them
  • Discover how to test asynchronous systems, either based on callbacks or promises
  • Test a RESTful web API and a rich UI using WebDriverJS and Protractor
  • Refactor and keep your test code base maintainable using best practices and patterns such as PageObject

Table of contents

  1. Learning Behavior-driven Development with JavaScript
    1. Table of Contents
    2. Learning Behavior-driven Development with JavaScript
    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. 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. Welcome to BDD
      1. The test-first approach
        1. The test-first cycle
          1. Write a failing test
          2. Make the test pass
          3. Clean the code
          4. Repeat!
        2. Consequences of the test-first cycle
      2. BDD versus TDD
      3. Exploring unit testing
      4. The structure of a test
        1. Test doubles
      5. What is a good test?
      6. Summary
    9. 2. Automating Tests with Mocha, Chai, and Sinon
      1. Node and NPM as development platforms
        1. Installing Node and NPM
      2. Configuring your project with NPM
      3. Introducing Mocha
        1. Useful options in Mocha
        2. Our first test-first cycle
      4. More expressive assertions with Chai
        1. Working with the "should" interface
      5. Red/Green/Refactor
        1. Parameterized tests
        2. Organizing your setup
        3. Defining test scenarios
      6. Test doubles with Sinon
        1. Is it traditional TDD or BDD?
        2. Welcome Sinon!
        3. Integrating Sinon and Chai
      7. Summary
    10. 3. Writing BDD Features
      1. Introducing myCafé
      2. Writing features
        1. Displaying a customer's order
        2. Tips for writing features
        3. Starting to code the scenarios
      3. Testing asynchronous features
        1. Testing a callback-based API
        2. Testing a promise-based API
          1. Interlude – promises 101
          2. Mocha and promises
            1. Chai-as-Promised
            2. Test doubles with promises
      4. Organizing our test code
        1. The storage object pattern
        2. The example factory pattern
          1. Finishing the scenario
        3. Parameterized scenarios
      5. Finishing our feature
      6. Summary
    11. 4. Cucumber.js and Gherkin
      1. Getting started with Gherkin and Cucumber.js
        1. Preparing your project
        2. Writing your first scenario in Gherkin
        3. Executing Gherkin
        4. The World object pattern
        5. Better step handlers
        6. Better reporting
      2. Writing advanced scenarios
        1. Gherkin example tables
        2. Consolidating steps
        3. Advanced setup
          1. Gherkin-driven example factory
          2. Implicit versus explicit setup
          3. The Background section
        4. Parameterized scenarios
          1. Finishing the feature
      3. Useful Cucumber.js features
        1. Tagging features and scenarios
        2. Hooks
          1. The before hook
          2. The after hook
          3. The around hook
        3. Non-English Gherkin
      4. Cucumber.js or Mocha?
      5. Summary
    12. 5. Testing a REST Web API
      1. The approach
        1. A strategy to test web APIs
        2. Mocha or Cucumber.js?
        3. The plan
      2. Testing the GET order feature
        1. Exploring our feature a bit
        2. Starting, stopping, and setting up our server
        3. Testing whether the API responds with 200 Ok
          1. Should we use a realistic order object?
          2. Implementing the test
        4. Testing our HAL resource for orders
          1. The contract with the business layer
          2. Finishing the scenario
      3. Testing slave resources
        1. The order actions
        2. Testing embedded resources
        3. Extracting cross-cutting scenarios
        4. Homework!
      4. Summary
    13. 6. Testing a UI Using WebDriverJS
      1. Our strategy for UI testing
        1. Choosing the right tests for the UI
        2. The testing architecture
      2. WebDriverJS
        1. Finding and interacting with elements
        2. Complex UI interaction
        3. Injecting scripts
        4. Command control flows
        5. Taking screenshots
        6. Working with several tabs and frames
      3. Testing a rich Internet application
        1. The setup
          1. The test HTML page
          2. Serving the HTML page and scripts
          3. Using browserify to pack our code
          4. Creating a WebDriver session
        2. Testing whether our view updates the HTML
        3. Testing whether our view reacts with the user
        4. What about our UI control logic?
      4. Summary
    14. 7. The Page Object Pattern
      1. Introducing the Page Object pattern
        1. Best practices for page objects
      2. A page object for a rich UI
        1. Building a page object that reads the DOM
        2. Building a page object that interacts with the DOM
        3. Testing the navigation
      3. Summary
    15. 8. Testing in Several Browsers with Protractor and WebDriver
      1. Testing in several browsers with WebDriver
        1. Testing with PhantomJS
        2. Running in several browsers
        3. The Selenium Server
      2. Welcome Protractor!
        1. Running the tests in parallel
        2. Other useful configuration options
        3. Using the Protractor API
      3. Summary
    16. 9. Testing Against External Systems
      1. Writing good test doubles
      2. Testing against external systems
        1. Testing against a database
          1. Accessing the DB directly
          2. Treating the DAO as a collection
        2. Testing against a third-party system
          1. The record-and-replay testing pattern
      3. Summary
    17. 10. Final Thoughts
      1. TDD versus BDD
      2. A roadmap to BDD
        1. BDD versus integration testing
        2. BDD is for testing problem domains
      3. Concluding the book
      4. Next steps?
      5. Summary
    18. Index

Product information

  • Title: Learning Behavior-driven Development with JavaScript
  • Author(s): Enrique Amodeo
  • Release date: February 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781784392642