Chapter 10: Testing our API
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 JEST 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 README page of the GitHub repository. 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: https://github.com/restful-node/restful-nodejs-app/actions.
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 https://testingjavascript.com by Kent C. Dodds.
Last updated