Asserting on Payloads
Learn about Ajax payload and Chai plugins.
We'll cover the following
Ajax payload
Before summarizing all the improvements we made to the original test, we need to discuss AJAX payloads assertions. The original list of problems listed two possible errors related to payloads
-
The AJAX call has the wrong request payload
-
The API returns the wrong payload
Both of these errors could break the app without any meaningful feedback. Cypress provides us APIs to check them.
First of all, the request payload looks like this:
{
"user": {
"username": "Tester92306",
"email": "user+92306@realworld.io",
"password": "mysupersecretpassword"
}
}
The response payload looks like the following:
{
"user": {
"username": "tester92306",
"email": "user+92306@realworld.io",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVkNzA5NmY3MWM2YTQ5MDVjM2VhMGM3NSIsInVzZXJuYW1lIjoidGVzdGVyOTIzMDYiLCJleHAiOjE1NzI4NDczNjcsImlhdCI6MTU2NzY1OTc2N30.uT9qGkT4FPCHwkV2A_7rbRFb_YqJN3cyncdbKOC3xNY"
}
}
We note that:
-
The
username
property is the same but it is converted to lowercase in the request payload. -
The
email
is the same. -
The
token
is a string that changes every time.
Asserting about them means checking that:
-
The request payload contains a
user
object that contains the data used to fill the form. -
The response payload contains both the
email
and theusername
.is a string that’s 200-210 characters long (a JSON Web Token).
Do we need to verify whether the token
is syntactically correct? Well, we should really be asking if the front-end app parses and uses the token, and does the front-end break if the token is not valid? If the answer is yes, assert about it! Otherwise, assertions are not necessary!
Starting from the most recent test:
Get hands-on with 1400+ tech skills courses.