DEV Community

loading...

Firelinker + Testing

zg3d profile image Devansh Shah ・2 min read

Intro

This week, I added testing to fireLinker. I always had my github action use npm test but I did not have any tests on fireLinker previously. This blog will talk about my experience with adding jest and mock testing. I also modularized my code to make a better experience when testing and adding new features.

Modularize

I knew I wanted to work with jest because I have been doing a lot of testing with jest on telescope and find it easy to use and it gets the job done. The thing I realized when testing telescope was that the code was modularized. So, the first thing I did was make 2 file classes and utils. Classes would include any global data structure and utils would include each function in it own file. This made adding jest simple as all I added was a tests folder and added jest to my dev dependences.

Mock Testing

A big part of Firelinker is the linkchecker and to test it appropriately I need to do mock testing. The reason for this is that a website can change its status at any point so there is no guarantee that the status of a website will always be the same so. We create a mock environment where before each call of the function in the test file we give fetch a mock return value. The way my function works is it uses node-fetch to retrieve the head and only check the status code. So, my mock return value is just an object with a status property with the the expected status code.

Problem

One thing that happened was I kept getting an error when I made a test function in my test files. but when I would save and run npm test it would work. The problem was with not including jest as a global in eslint.

Reflection

Overall, adding tests to FireLinker has been a positive experience and there are many more tests I could add to test each and every aspect of the code for Example each file in the modularized files get a test file. One thing I really want to add tests for is the Link class.

  • Devansh Shah

Discussion

pic
Editor guide