Testing JavaScript Applications

Book description

Automated testing will help you write high-quality software in less time, with more confidence, fewer bugs, and without constant manual oversight. Testing JavaScript Applications is a guide to building a comprehensive and reliable JS application testing suite, covering both how to write tests and how JS testing tools work under the hood. You’ll learn from Lucas de Costa, a core contributor to popular JS testing libraries, as he shares a quality mindset for making testing decisions that deliver a real contribution to your business. You’ll benefit from informative explanations and diagrams, easily-transferable code samples, and useful tips on using the latest and most consolidated libraries and frameworks of the JavaScript ecosystem.

About the Technology
Automated testing is essential to delivering good JavaScript applications every time. A complete testing strategy needs to cover functions in isolation, integration between different parts of your code, and correctness from the end user’s perspective. This book will teach you how to deliver reliable software quickly and confidently.

About the Book
Testing JavaScript Applications teaches you how to implement an automated testing plan for JavaScript-based web applications. It describes practical testing strategies, covers useful tools and libraries, and explains how to foster a culture of quality. In this clearly-written, example-rich book, you’ll explore approaches for both backend and frontend applications and learn how to validate your software much more quickly and reliably.

What's Inside
  • Unit, end-to-end, and integration testing
  • Managing test cost and complexity
  • Practicing test-driven development
  • Dealing with external dependencies
  • Tools like Jest and Cypress


About the Reader
For junior JavaScript developers.

About the Author
Lucas da Costa is a core maintainer of Chai and Sinon.JS, two of the most popular testing tools in the JavaScript ecosystem, and contributed to numerous other open-source projects, including Jest.

Quotes
If you thought you knew how to test JavaScript applications, this book will surprise you!
- Lucian Enache, Glencore

This book is your key to taming the wild frontier of JavaScript testing.
- Bryan Miller, Petrolitix

A detailed, insightful book on JavaScript testing for newcomers and experienced programmers.
- Satej Sahu, Honeywell

An up-to-date, practical book that covers the ground level details of writing real-world tests and testing suites.
- Raul Siles, DinoSec

Table of contents

  1. inside front cover
  2. Testing JavaScript Applications
  3. Copyright
  4. dedication
  5. contents
  6. front matter
    1. preface
    2. acknowledgments
    3. about this book
      1. Who should read this book
      2. How this book is organized: A roadmap
      3. About the code
      4. System requirements
      5. liveBook discussion forum
    4. about the author
    5. about the cover illustration
  7. Part 1. Testing JavaScript applications
  8. 1 An introduction to automated testing
    1. 1.1 What is an automated test?
    2. 1.2 Why automated tests matter
      1. 1.2.1 Predictability
      2. 1.2.2 Reproducibility
      3. 1.2.3 Collaboration
      4. 1.2.4 Speed
    3. Summary
  9. 2 What to test and when?
    1. 2.1 The testing pyramid
    2. 2.2 Unit tests
    3. 2.3 Integration tests
    4. 2.4 End-to-end tests
      1. 2.4.1 Testing HTTP APIs
      2. 2.4.2 Testing GUIs
      3. 2.4.3 Acceptance tests and end-to-end tests are not the same
    5. 2.5 Exploratory testing and the value of QA
    6. 2.6 Tests, cost, and revenue
    7. Summary
  10. Part 2. Writing tests
  11. 3 Testing techniques
    1. 3.1 Organizing test suites
      1. 3.1.1 Breaking down tests
      2. 3.1.2 Parallelism
      3. 3.1.3 Global hooks
      4. 3.1.4 Atomicity
    2. 3.2 Writing good assertions
      1. 3.2.1 Assertions and error handling
      2. 3.2.2 Loose assertions
      3. 3.2.3 Using custom matchers
      4. 3.2.4 Circular assertions
    3. 3.3 Test doubles: Mocks, stubs, and spies
      1. 3.3.1 Mocking imports
    4. 3.4 Choosing what to test
      1. 3.4.1 Don’t test third-party software
      2. 3.4.2 To mock, or not to mock: That’s the question
      3. 3.4.3 When in doubt, choose integration tests
    5. 3.5 Code coverage
      1. 3.5.1 Automated coverage reports
      2. 3.5.2 Coverage types
      3. 3.5.3 What coverage is good for and what it isn’t
    6. Summary
  12. 4 Testing backend applications
    1. 4.1 Structuring a testing environment
      1. 4.1.1 End-to-end testing
      2. 4.1.2 Integration testing
      3. 4.1.3 Unit testing
    2. 4.2 Testing HTTP endpoints
      1. 4.2.1 Testing middleware
    3. 4.3 Dealing with external dependencies
      1. 4.3.1 Integrations with databases
      2. 4.3.2 Integrations with other APIs
    4. Summary
  13. 5 Advanced backend testing techniques
    1. 5.1 Eliminating nondeterminism
      1. 5.1.1 Parallelism and shared resources
      2. 5.1.2 Dealing with time
    2. 5.2 Reducing costs while preserving quality
      1. 5.2.1 Reducing overlap between tests
      2. 5.2.2 Creating transitive guarantees
      3. 5.2.3 Turning assertions into preconditions
    3. Summary
  14. 6 Testing frontend applications
    1. 6.1 Introducing JSDOM
    2. 6.2 Asserting on the DOM
      1. 6.2.1 Making it easier to find elements
      2. 6.2.2 Writing better assertions
    3. 6.3 Handling events
    4. 6.4 Testing and browser APIs
      1. 6.4.1 Testing a localStorage integration
      2. 6.4.2 Testing a History API integration
    5. 6.5 Dealing with WebSockets and HTTP requests
      1. 6.5.1 Tests involving HTTP requests
      2. 6.5.2 Tests involving WebSockets
    6. Summary
  15. 7 The React testing ecosystem
    1. 7.1 Setting up a test environment for React
      1. 7.1.1 Setting up a React application
      2. 7.1.2 Setting up a testing environment
    2. 7.2 An overview of React testing libraries
      1. 7.2.1 Rendering components and the DOM
      2. 7.2.2 React Testing Library
      3. 7.2.3 Enzyme
      4. 7.2.4 The React test renderer
    3. Summary
  16. 8 Testing React applications
    1. 8.1 Testing component integration
      1. 8.1.1 Stubbing components
    2. 8.2 Snapshot testing
      1. 8.2.1 Snapshots beyond components
      2. 8.2.2 Serializers
    3. 8.3 Testing styles
    4. 8.4 Component-level acceptance tests and component stories
      1. 8.4.1 Writing stories
      2. 8.4.2 Writing documentation
    5. Summary
  17. 9 Test-driven development
    1. 9.1 The philosophy behind test-driven development
      1. 9.1.1 What test-driven development is
      2. 9.1.2 Adjusting the size of your iterations
      3. 9.1.3 Why adopt test-driven development?
      4. 9.1.4 When not to apply test-driven development
    2. 9.2 Writing a JavaScript module using TDD
    3. 9.3 Testing top-down versus testing bottom-up
      1. 9.3.1 What bottom-up and top-down testing mean
      2. 9.3.2 How top-down and bottom-up approaches impact a test-driven workflow
      3. 9.3.3 The pros and cons of bottom-up versus top-down approaches
    4. 9.4 Balancing maintenance costs, delivery speed, and brittleness
      1. 9.4.1 Test-driven implementation
      2. 9.4.2 Test-driven maintenance
    5. 9.5 Setting up an environment for TDD to succeed
      1. 9.5.1 Teamwide adoption
      2. 9.5.2 Keeping distinct lanes
      3. 9.5.3 Pairing
      4. 9.5.4 Supplementary testing
    6. 9.6 TDD, BDD, validations, and specificationsBDD (behavior-driven development)
    7. Summary
  18. 10 UI-based end-to-end testing
    1. 10.1 What are UI-based end-to-end tests?
    2. 10.2 When to write each type of test
      1. 10.2.1 UI-based end-to-end tests
      2. 10.2.2 Pure end-to-end tests
      3. 10.2.3 Pure UI tests
      4. 10.2.4 A note on acceptance testing and this chapter’s name
    3. 10.3 An overview of end-to-end testing tools
      1. 10.3.1 Selenium
      2. 10.3.2 Puppeteer
      3. 10.3.3 Cypress
      4. 10.3.4 When to choose Cypress
    4. Summary
  19. 11 Writing UI-based end-to-end tests
    1. 11.1 Your first UI-based end-to-end tests
      1. 11.1.1 Setting up a test environment
      2. 11.1.2 Writing your first tests
      3. 11.1.3 Sending HTTP requests
      4. 11.1.4 Sequencing actions
    2. 11.2 Best practices for end-to-end-tests
      1. 11.2.1 Page objects
      2. 11.2.2 Application actions
    3. 11.3 Dealing with flakiness
      1. 11.3.1 Avoiding waiting for fixed amounts of time
      2. 11.3.2 Stubbing uncontrollable factors
      3. 11.3.3 Retrying tests
    4. 11.4 Running tests on multiple browsers
      1. 11.4.1 Using a testing framework to run tests within a browser
      2. 11.4.2 Running UI-based tests in multiple browsers
    5. 11.5 Visual regression tests
    6. Summary
  20. Part 3. Business impact
  21. 12 Continuous integration and continuous delivery
    1. 12.1 What are continuous integration and continuous delivery?
      1. 12.1.1 Continuous integration
      2. 12.1.2 Continuous delivery
    2. 12.2 The role of automated tests in a CI/CD pipeline
    3. 12.3 Version-control checks
    4. Summary
  22. 13 A culture of quality
    1. 13.1 Using type systems to make invalid states unrepresentable
    2. 13.2 Reviewing code to catch problems machines can’t
    3. 13.3 Using linters and formatters to produce consistent code
    4. 13.4 Monitoring your systems to understand how they actually behave
    5. 13.5 Explaining your software with good documentation
    6. Summary
  23. index
  24. inside back cover

Product information

  • Title: Testing JavaScript Applications
  • Author(s): Lucas Fernandes da Costa
  • Release date: May 2021
  • Publisher(s): Manning Publications
  • ISBN: 9781617297915