Lab 8: Automated Testing and Continuous Integration

When I developed a program, it was time-consuming to test all the functions I created when I add a feature. I needed to run each feature, check whether it gives expected output or not, and check all the output. It is possible I forgot to test a feature and later on, I realize I did not test the feature. I wonder if there any ways I can do it automatically.


Now, I got the way how to do that. There is automated testing for it! To begin with it, I started searching the testing framework to apply my command-line tool named url-fi. There are some testing frameworks for Node.js such as Jest, Mocha, and AVA. The testing framework I chose is Jest which is a testing framework developed by Facebook. I've installed Jest running 'npm install --save-dev jest' and test whether it works properly or not. This document is the way I followed to install the Jest testing framework.


After I've installed the Jest testing framework, I worked on separating functions. As my tool contains all the functions in one file named index.js, it would be better to separate each function inside a different file. After I've done this, I started creating unit tests. It was easy to create a unit test for the function only to display a certain message. however, when I tried to create a unit test for the urlChecker() function, I got some struggles. As my function the urlChecker() does not asynchronously work, it does not give the expected output. I needed to fix my code first! I took quite a lot of time to fix this issue and finally found it can be resolved to return Promise when I run urlChecker() not display the message directly. This way works perfectly! I created all unit tests to cover most of the functions.


On the next step, I make jest test create coverage report. It was easy to do this. I just need to add '--coverage' argument with the command: "test": "jest --coverage". It populates the coverage report of how many percentages unit tests covers the function. When I see the coverage report of my urlChecker() function, I faced the second struggle. I thought I covered most of the lines but it said the tests only cover around 20% of the lines! I would like to increase the coverage percentage so tried to find a reason why this happens. When I asked this on the Slack channel, professor David Humphrey kindly checked my code and test and let me know I used an unnecessary async-await feature on urlChecker(). Also, I used an unnecessary try-catch block on my unit test!. When I fixed these, the coverage percentage becomes around 100% like magic!


After I created unit tests for the functions named checkUrl, cliHelpMsg, and versionInfo, I set up my GitHub Actions CI Workflow. When I contributed open-source community to create a pull request, I saw this a lot but did not know how to do it. I've learned how to do this and apply it to my tool. Setting up my GitHub Actions CI Workflow was easy. After some clicks, it created a YAML file for it. At the first time, it does not work properly because it needed to run 'npm run test' not 'npm test' ('npm test' was a default command). When I changed the built environment to windows and the command 'npm run test', it finally works.

YAML file: https://github.com/hyunjiLeeTech/URL-FI/blob/master/.github/workflows/node.js.yml

GitHub Actions URL: https://github.com/hyunjiLeeTech/URL-FI/actions


After I've done these all tasks and added some more tests to check whether CI workflow works properly or not, I opened the Slack channel to find a partner to add more tests for each project. Luckily, there is one student who tried to find a partner. She even used the same language and the same testing framework as mine! I asked her to work for this lab - partner part together and she accepted. I cloned her project and see what test I can add to her project. As she even created the e2e test and I would like to work on it, I focused on adding more tests to e2e test. Also, I saw some tasks to improve her project so I worked on those features too.

    • Update .gitignore file to ignore coverage folder
    • Add test for URL function
    • Add more e2e tests
    • Update README.md file
    • My pull request is successfully merged!


I knew there is an automated test framework such as Selenium C# for the website. However, I did not know there are other testing frameworks like Jest. It was a really good experience to get the opportunity to use this framework. I would like to keep practicing this framework and other frameworks in the future.

Comments

Popular posts from this blog

Open Source Project: Lab2 (Pull Request)

Release 0.3: External Project (Hasura GraphQL Engine)