Code Testing

In order to enhance the DLCheck tool, code testing is mandatory, it ensures the codes' behavior to a certain extent and allows the contributors to discover bugs as they try to update the tool. The tools I chose to perform testing with my code is Jest, combined with the nock npm module.  I chose to use Jest because it is one of the most popular testing frameworks for JS code and it is simple to set up and use, the nock module was extremely powerful as it allows me to manipulate requests to servers and customize the response in order to keep the test results consistent. If I have used a real url to perform the testing, for example, google.ca, Google's server may go down and be temporarily unavailable, which will fail the tests, however, with the nock module, I will always have the customized response that I want for each test.Setting up the tools mentioned above is easy, simply follow the instructions here to set up Jest, and run "npm install --save-dev nock" to install nock.

This is my first time dealing with code tests, after I finished adding the existing tests, I found that writing tests could be as difficult as implementing the actual project, since there are different cases to cover, and that in order to write good tests, mock data must be used, and this could be difficult, how the project is written will have a direct impact on how easy you can implement the tests as well. The biggest issue I had while trying to add tests to the project is when I tried to test the 200 and 404 responses from a server. I realized that I did not make a dedicated function to make the requests, therefore, it is difficult to write the tests for the 200 and 404 response code cases. In order to achieve those tests, I had to refactor my original project and make modifications. I ended up moving the request block into a new file and made a function which returns a Promise, which resolves the statusCode if the request was successful and rejects with "error.message" if it ran into an error during the request.

After the first couple of tests were added, I thought it was good enough for the moment, until I checked the test coverage and realized that one of the lines in the requestUrl function was never hit by the tests, that is the line “if(error) reject(error.message);” Therefore, I decided to write a test case to check if the error message received will behave as I want it to. In order to do this, the nock module was used again.

In the image below, it shows how to use the nock module, the first test tests the received status code, the reply() function allows us to customize the statusCode, to test the error messaged mentioned above, I used the replyWithError function instead, this does not send back any resposne code, but an error instead, which allows me to test the “if(error) reject(error.message);” line.


Since all of those tests were used, it only make sense if they are constantly used, I used GitHub Actions and set up builds which would run these tests automatically every time a contributor try to push and send a PR to the master branch.


Aside from the DLCheck project, I have also added some tests for one of my classmate's project, which is pretty much the same tool, I added some test cases for his getStatus() function, to ensure that the function handles null, undefined and empty URL strings correctly.

This was a great learning experience, and I will definitely put code testing into consideration when building future projects, because not only does it test the functionality, it also teaches me how to write better code because throughout this experience I noticed how much the project could be improved.

DLCheck repohttps://github.com/Wei-J-Huang/dlcheck

Added testshttps://github.com/Wei-J-Huang/dlcheck/pull/16/commits/b0571019c9c1210833a2a5429e650a4f0604510b

Tests added to classmate's repohttps://github.com/Jasper-Mui/me-check-links/pull/13

Comments

Popular posts from this blog

Working on the Telescope Project

Hacktoberfest -1

Hacktoberfest -2