-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update testing.mdx added documentation fot api testing with cypress * Minor edits --------- Co-authored-by: Jan C. Brammer <[email protected]>
- Loading branch information
1 parent
f38f646
commit 17575f8
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,6 +185,41 @@ cy.createDefaultUser('[email protected]', 'cu1').then((user) => { | |
}); | ||
``` | ||
### Example of testing an API endpoint with Cypress | ||
You can also use Cypress to test APIs in a developer-friendly way. | ||
You can conveniently access the API response data from the test runner. | ||
When you click on the request step, Cypress will display the corresponding response data in the browser's console. | ||
Consider the following example, where we are testing `Collection API (GET request)`. | ||
```javascript | ||
cy.request('/api/v1/collections/roots.json').as('roots'); | ||
cy.get('@roots').then(response => { | ||
expect(response.status).to.eq(200); | ||
const collectionData = response.body['collections'][0]; | ||
expect(collectionData.id).to.eq(3); | ||
expect(collectionData.label).to.eq('Col1'); | ||
}); | ||
``` | ||
Here, were are sending a GET request to the `api/v1/collections/roots.json` endpoint and verify that its `response.status` is `200`. | ||
The response body can contain objects, arrays, attributes, etc. This is how we can access a specific component from its body and perform checks on it. | ||
```javascript | ||
// whole object | ||
const collectionData = response.body['collections']; | ||
|
||
// single item from the object list | ||
const collectionData = response.body['collections'][0]; | ||
|
||
// check length of object | ||
expect(response.body['collections'].length).to.eq(0) | ||
|
||
// check if returned fields are matching exactly what's expected | ||
expect(collectionData.id).to.eq(3); | ||
expect(collectionData.label).to.eq('Col1'); | ||
``` | ||
## General rules of thumb | ||
- All public methods should be tested, every statement, conditional branch should be covered. | ||
|