OSD600 Lab8

OSD600 LAB8

In this lab, we were asked to add unit tests, code coverage analysis, continuous integration to my open source project urlStatusChecker.  we were also asked to add Tests to Another project.

Choose and Set Up a Testing Framework

Since my command-line tool is written in javascript, I choose Jest as my testing framework. It is a famous javascript testing framework that works with projects using: Babel, Typescript, Node, React, Angular, Vue, etc. I know that Jest contains a code coverage analysis as well, so it is the perfect option. I simply use the command "npm install --save-dev jest" to install it and start writing my unit test.

Write My First Test

I have never write any unit tests before. After I watched this week's video, I have a basic concept of what is unit testing, integration testing, end to end testing. I chose the simplest function in my project to begin to write my first unit test. 

const goodUrl = (urlString=> {
  const PATTERN = /(http|https)(:\/\/)([\w+\-&@`~#$%^*.=/?:]+)/gi;
  if (typeof urlString != "string"return null;
  let urls = urlString.match(PATTERN);

  if (urls != nullreturn urls[0];
  else return null;
};

This function will return null if the input is not a string and if the string is not matched with the URL pattern. Otherwise, it will return the URL. I wrote 3 tests for each http, https, and a bad URL to test will it return null or the URL first. Then I tested null, undefined, Object, Integer as input will it return null too.

Write a Test for my sendRequest function

Checking response from a given URL is the most important part of my command-line tool. Therefore I have to write a good test that covers all paths of this function to make sure my project works properly. 

Firstly, I need to find an API to simulate network responses. Nock is one of the good options to mock the situation. For example, I declare a string variable const url = "https://.google.com
Then I can simply mock a response with a statusCode of 200, 404, 307, etc. using

    const url = "https://www.google.com";
    nock(url).intercept("/""HEAD").reply(200);

After I mock the response, I call the function and then check whatever it console is the same as my expected.

I got an error when I first time comparing two different strings, and it took me a while to find where I got wrong.



The reason caused this was that my function output uses "chalk" to print a green message to the consoler, and my expected string is a normal string without "chalk". I fixed it by adding chalk.green() 
to my expected string. After that, I added more tests for 200, 404, 301, and other cases.                  

Add Code Coverage Analysis

After I finish my two unit test, I add some command to my script including "coverage": "jest --collectCoverage --", this is a jest command that will create a folder named "coverage" and reports of testing code coverage inside the folder. It also consoles the report to the consoler. This is what I get when I first run the "npm run coverage" command.




The uncovered line in my readUrl is from another function, but the coverage of urlRequest is also bad because I did not include the json, and status cases. My urlRequest took 3 arguments which are string(url), json(boolean) and status(string of "good" or "bad"). I added more testing cases to check all the paths inside my sendRequest function and then I finally got 100% coverage of them. 



After I done this, I add the coverage folder name into .gitignore and squashed and mergeed mytesting branch into master.

Add Continuous Integration

using GitHub Actions to add Continuous Integration is very easy and straight. I simply click on Action button in my repository and set up Node.js workflow from there. After set a few configuration in the "node.js.yml", I commited the file.  From now on, every pull request to my master branch will run the tests in the project, and it prevent all the bad pull requests from someone who forget to run the tests before pushing.

Make a pull request

In order to test my pull request run the CI correctly, I decide to add end to end test to my project then make a pull request it. I used "execa" module to call the commands inside my program, then store the standard output as snapshot. Those snapshot stored in my snapshot folder to compared to future snapshots. If two snapshot is differnt, then something in the program went wrong. 

However, my pull request is failed in Github CI because my snapshot have ANSI colour codes in it. 



I asked for help for this problem, and I knew a good dependency from my professor to remove ANSI colour codes from jest snapshots jest-snapshot-serializer-ansi. 

Add Tests to Another Project

The last quest I need to do is to add test to another project. Since I have too much work to do, I don't want to challenge myself for another language, I chose a javascript project testLink to add end to end test to it.

My pull request passes the CI and get merged by the project owner.


Comments

Popular posts from this blog

My PR for release 0.3 External Project

OSD600 -- Release 0.4.1 -- Choose Project and issues