Written on 2023-10-11 by Adam Drake - 4 min read
I have been working recently on quite a large project with many API endpoints. I have been working on the Frontend whilst another developer has been working on the backend.
The Frontend stack is:
The Backend stack is:
Part of the flow is when the Backend makes some changes and pushes them the Frontend would pull these changes and then run a script. The script is:
This would use OpenApi to generate services and models in the Frontend codebase from the openapi.json file provided by the backend api. This is a super helpful script and easily allows our Frontend to keep up to date with the backend and also provides all the types we need to interact with these services.
However, as we have been going through this flow, due to the sheer amount of changes quite a few things have been breaking when backend changes are being pushed. This causes api endpoints like the Reseeding functionality to stop working or a specific GET request suddenly doesn’t work.
Due to the amount of endpoints we have in this application we needed a quick way to test the basic functionality of the endpoints whenever there were backend changes introduced. This would very quickly allow us to determine if the latest changes introduced anything that broke something.
This is where Cypress came in super handy. I was looking into their very extremely useful Real World Application repo and getting some ideas about how to do this.
They have a specific folder — api which they use just to test api endpoints. This was exactly what I needed so I decided to implement it. My primary focus was being able to access the apis and do basic GET requests. I didn’t want to spend too long on these tests at this time.
The endpoints we use require an authenticated user to access them. This comes in the form of a token in the request header. How to do this in Cypress? I experimented a few different ways and finally settled on this way:
This is a command I added to the commands.ts file and it allowed me access to the api. The little ‘trick’ here is the line cy.wrap(token).as('jwt'); This allows the token to then be accessed in your tests so they can be used when you access the api endpoints. Something like:
The cy.get('@jwt') allows you access to retrieve this token and then use it in the header of the GET request. Pretty neat!
This was a very quick way to introduce some tests into the codebase that could provide very quick and useful feedback when changes are done on the backend. When working on a large codebase it can sometimes become too much for a single developer to keep every piece of logic and connection in their head. By introducing basic tests like this you are giving yourself and the team a safety net to prevent you from accidentally pushing something broken to the production environment.
Loading...
Adam Drake is a Frontend React Developer who is very passionate about the quality of the web. He lives with his wife and three children in Prague in the Czech Republic.
Adam Drakes Site © 2024