Integration Testing is a level of software testing where individual units are combined and tested as a group. The purpose of this level of testing is to expose faults in the interaction between integrated units. Today we're going to look at doing Integration Testing using Jest and Supertest.
What is Jest?
The first thing to do is install Jest and Supertest.
Next, create a directory in your root folder called "__tests__".
Within that directory create another folder called "integration".
Now head to package.json and create a new script to run our integration tests.
Next let’s look at our integration test file, which includes our first test.
We begin by creating a localhost url and port string, which we pass into Supertest when we import it into te file. Jest uses a familiar "describe" block, with "it" blocks inside of it. We are using Supertest to execute an HTTP Post request, which includes are body of JSON data to execute against our Lambda API endpoint. The callback inside the .then will be handled by Jest, so we can use any of the Jest expect methods in that callback.
Running Our Integration Tests
Firstly, run the "serverless offline" command, so that we can test our API locally.
Now run our integration test script "npm run integration".
Here's the result of our integration test, which has successfully passed.
Let's add a few more integration tests to validate different scenarios, and review the results.
As you can see, all of our integration tests with Jest are passing successfully, and our Lambda API endpoint is good to go! Enjoy!
Download Github Code Sample
Feel free to DOWNLOAD the sample code of the aforementioned implementation from my Github account.