I am working with a customer now that is looking to go through a JSON:API upgrade, from version 1.x on Drupal 8.6.x to 2.x and then ultimately to Drupal 8.7.x (where it is bundled into core).
As this upgrade will involve many moving parts, and it is critical to not break any existing integrations (e.g. mobile applications etc), having basic end-to-end tests over the API endpoints is essential.
I won't go too much in depth about Cypress in this blog post (I will likely post more in the coming months), instead I want to focus specifically on JSON:API testing using Cypress.
In this basic test, I just wanted to hit some known valid endpoints, and ensure the response was roughly OK.
Rather than have to rinse and repeat a lot of boiler plate code for every API end point, I wrote a custom Cypress command, to which abstracts all of this away in a convenient function.
Below is what the
spec file looks like (the test definition), it is very clean, and is mostly just the JSON:API paths.
And as for the custom function implementation, it is fairly straight forward. Basic tests are done like:
- Ensure the response is an HTTP 200
- Ensure the content-type is valid for JSON:API
- Ensure there is a response body and it is valid JSON
- Enforce a minimum number of entities you expect to be returned
- Check for certain properties in those returned entities.
Some of the neat things in this function is that it does log the parsed JSON response with
cy.log(body); this allows you to inspect the response in Chrome. This allows you to extend the test function rather easily to meet you own needs (as you can see the full entity properties and fields.
Using Cypress is like having an extra pair of eyes on the Drupal upgrade. Over time Cypress will end up saving us a lot of developer time (and therefore money). The tests will be in place forever, and so regressions can be spotted much sooner (ideally in local development) and therefore fixed much faster.
If you do JSON:API testing with Cypress I would be keen to know if you have any tips and tricks.