RESTful Node.js: A Structured Approach
  • Book Cover
  • About the Author
  • Links and Resources
  • Part I: The Why
    • Foreword
    • Preface
    • Chapter 1: Introduction
      • The Rise of REST and Distributed Systems
      • Problem #1: Structureless Design, Structureless REST
      • The Emergence of JavaScript and Node.js
      • Problem #2: Structureless JavaScript, Structureless Node.js
      • Behold, the Solution: A Structured Approach
      • Summary
  • Part 2: The Theory
    • Chapter 2: REST Origins
      • A Brief History of the Web and the Birth of REST
      • REST vs. HTTP
      • REST - The Abstract Web Architecture
      • HTTP - A Peak at REST's Concrete Implementation
      • What does it mean for an API to be RESTful?
      • Measuring "RESTfulness" with Richardson Maturity Model
      • Pragmatic REST vs Dogmatic REST
      • Summary
    • Chapter 3: RESTful API Design Guidelines and "Best Practices"
      • Theories vs. Principles vs. Guidelines
      • URI Design
      • Method Verbs
      • Status Codes
      • Representational Design
      • Metadata Design
      • Versioning Strategies
      • Security Considerations
      • Documentation
      • Case Study: GitHub
      • Summary
    • Chapter 4: Structured JavaScript Architecture
      • The Monstrous Monolith and Its Downfall
      • Layered/N-Tier Architecture: The Unpopular Proven Way
      • Microservices and Distributed Computing: A Popular Misdirection
      • Summary
    • Chapter 5: The 8 Step Recipe
      • Route Name (URI)
      • Input Request
      • Middleware
      • Validation
      • Domain
      • Events
      • Output Response
      • Test, Refactor, Document
      • Summary
  • Part 3: The Code
    • Chapter 6: Introduction to the Bookstore API
      • The Bookstore API Endpoint Specifications
      • API Design and Code Structure
      • Project Setup
      • Summary
    • Chapter 7: Retrieving Books from our API
      • Retrieving All Books - Planning
      • Retrieving All Books - Implementation
      • Retrieving A Book By ID - Planning
      • Retrieving A Book By ID - Implementation
      • Summary
    • Chapter 8: Adding Authentication to our API
      • Registering the User - Planning
      • Registering the User - Implementation
      • Logging the User In - Planning
      • Logging the User In - Implementation
      • Getting Authenticated User - Planning
      • Getting Authenticated User - Implementation
      • Summary
    • Chapter 9: Adding the Create, Update, and Delete Operations to our API
      • Creating A Book Listing - Planning
      • Creating A Book Listing - Implementation
      • Updating A Book Listing By ID - Planning
      • Updating A Book Listing By ID - Implementation
      • Deleting A Book Listing By ID - Planning
      • Deleting A Book Listing By ID - Implementation
      • Summary
    • Chapter 10: Testing our API
      • Testing the Request
      • Testing the Middleware
      • Testing the Validation
      • Testing the Domain
      • Testing the Event
      • Testing the Response
      • Testing the Controller
      • Integration Test
      • Summary
  • Conclusion
    • Final Words
  • Bonus!
    • Refactoring to HATEOAS
  • Appendix
    • Sources & References
Powered by GitBook
On this page
Edit on GitHub
  1. Part 3: The Code

Chapter 10: Testing our API

PreviousSummaryNextTesting the Request

Last updated 3 years ago

Every time you've ever filled a form and you expect to see some kind of notification panel at the top, you are testing. Everyone agrees that testing is good, the only question is, is automated testing good? Why would the answer ever be no? Or think of it this way, why would it ever be a bad thing to have this huge group of tests that backup everything you write.

-Jeffrey Way, Founder of Laracasts.com

At this point, we've written the code for our RESTful API. The only problem is that we don't have any automated tests to back them up. In this chapter, we'll demonstrate how to test each layer of our API and we'll see just how easy and efficient a layered architecture application is able to incorporate automated tests. Since there are way too many files and features to test, we'll keep it simple and consistent, we'll be mainly focused on writing tests for the user registration endpoint.

Before We Begin

We will be using as our testing framework of choice.

All tests will be written in a __tests__ folder depending on which layer we'll be testing except for API integration tests which will be located in the tests directory of the root folder. You can find all the configuration settings in the config/jest.config.js file.

Tests can be ran by following the instructions on the Make sure to install the application and have it running locally before running the tests.

There is a Git Actions setup for continuous integration for this project. You may find it here at: .

Do note that this is not a complete course on testing, if you want to learn more about testing and testing in JavaScript, I recommend taking the course by Kent C. Dodds.

JEST
README page of the GitHub repository.
https://github.com/restful-node/restful-nodejs-app/actions
https://testingjavascript.com