Testing APIs with Cypress
In the previous section, we learned how to structure test code using the POM and custom commands design patterns. We also learned that we could use Cypress to interact with our application's API directly. In this section, we will build on the previous section's learnings by testing the API of the blog application previously introduced in the Cypress-driven development section.
The blog application accepts four API requests: a GET
request to get all posts, a POST
request to add a post, a POST
request to get a single post, and a DELETE
request to delete a post. First, we will test the GET
request for all posts:
import fakePost from '../support/generateBlogPost'; const post = fakePost() const getAllPosts = () => cy.request('/api/posts').its('body. posts'); const deletePost = (post) => cy.request('DELETE', `/api/delete/${post.id}`, { id: post.id, ...