This week I was working on creating test cases for Telescope. I was assigned to an issue-615. The main point of this issue was to create tests for cases where a client requests different content types of text for a given post ID. The content types that I covered were JSON, Text, and HTML.
Automated tests for Telescope’s REST API endpoints are written using Jest and Supertest. I started by researching both of them and skimming documentation provided online.
Supertest is a library made specifically for testing NodeJS HTTP servers. It is also an open-source project with handy documentation on its GitHub page.
My task for this issue was to:
- Update the two objects (addedPost and receivedPost) so that the HTML property contains real HTML (content
) and the text property contains the same content as text stripped of HTML (content),
- Convert all the it() to test(),
- Write cases for requesting Text, HTML, and JSON.
When I was writing tests for ‘request test’ and ‘request HTML’ I faced an issue – I was using the response body (res.body) to compare received data with testing data and res.body was always empty. It did not make any sense, so I’ve spent some time researching why it is not working the way I want. I found out that if content type header is text/plain or text/HTML, then the response body won’t be parsed. For cases like this, there is res.text that will contain data as a string. Here is the code snippet of it:
The rest of my changes for this PR you could find here.
Thank you for your attention!