In this tutorial, you will learn how you can test your APIs using CypressJS. On a recent project, we needed to provide a high level of TEST automation. For this particular project, we were just building the API layer. the API had no website, or, a mobile app that we were responsible to test against. We just had to test the API end-points!

In these situations, manually regression testing a plethora of API end-points whenever a change was made was not an option. In order to allow the team to ship code quickly, we needed automation tests ⚡⚡⚡. Without a high level of test coverage, you will not be able to get a have level of confidence that the code you write works. The only way to check that the code works is by spending x number of hours performing some kind of manual regression testing. If you have to wait for a QA to perform a full regression test before you ship code, you will severely reduce how quickly you can get code into production. It will be the bottleneck!


So we knew we needed lots of integration tests. The next question, who should write these tests and what language and framework should we use? Traditionally in the projects that I have led, integration tests have been written by a QA. The language and framework of choice for most QAs seems to be Selenium tests written using Java. If you work with backend and frontend teams who primary use C# and Javascript, this causes a challenge ✨✨✨

If the QA writes a test in a language that the main development team does not understand, from my experience the QA team usually struggles. The three main reasons for these issues in my experience have been:

  • In the typical life-cycle, work gets done at the end of the sprint. Work comes in batches. This means the QA can struggle to write the tests in the same sprint cycle as they get overloaded with tasks at the end of the sprint. For reference, I tend to work in a team of fewer than 6 people.

  • On most projects, the development team understand the project requirements better than the QA team. In a lot of companies, time is money. To save on resourcing, more often than not, the QA team is not invited to every requirements meeting. This can make it tricky for the QA team to fully understand all the automation tests that need writing

  • Code quality: Automation tests still code. All code needs to be written to good coding standards. If you work within a company with a single QA tester and no one in the dev team is familiar with the language that the QA person uses, code reviews are maintaining test code quality is tricky. All code should be reviewed, even test code. How do you get your team of non-Java developers to help the QA team write better code, especially as the dev team will have a bunch of tasks they need to do themselves?

This is where CypressJs can come in. In most teams that I have worked with, both the backend and frontend teams can write JavaScript. If you can get your QA team to adopt CypressJS you will have great flexibility. Anyone in either backend, frontend or QA will be able to write automation tests, input into code reviews and provide support around the team. This is great!

API Testing With CypressJs

When most people think CypressJS, the first thought might be on testing the front end of a website. CypressJS was written as a tool to run a browser and make assertions that a weebsites HTML works as expected. A little known feature of Cypres is that you can also use it to test APIs. Using a free plug-in written by Gleb Bahmutov called, CY-API. You can install the plug-in using this command:

To include the plug-in within your Cypress project, within your Cypress folder called supportindex.js, add this reference:

With the plug-in enabled, you can write API tests. The structure of how to write an API test is very similar to how you would write a normal Cypress test. The main difference is that with the cy-api plugin, you will have access to a new Cypress function. This function can be accessed using the cy.api(). To make a request to an API you use the 'url' prop passing in the API URL. You can assert the data returned by cy.api in a few ways. You can still use the normal Cypress assertions like its() and should():

cy.api returns a promise, so an alternative way of testing response values is using then(). The test below is checking the JSON response from an API call:

You can also test the status code for an API request. This would allow you to test a happy path scenario. This happy path test could be written like this:

You could also test the sad path with a failing status code:

After installing cy-api, you can write your UI tests and your integration tests using the same framework in the same place. This will make your Ci/Cd pipeline easier to set up. As you are using Javascript more people in your company should have some basic knowledge to write and support the test script creation process. Using CypressJS like this is easy and in my opinion, provides a great use case to adopt it within your team. What do you think? Happy Coding 🤘