DEV Community

Tuan Thanh Tan
Tuan Thanh Tan

Posted on

Adding tests for my static site generator using Jest

Introduction

Hello everyone, my name is Dustin. Today, I wanted to talk about testing in nodejs project. This is literally one of my first time writing tests using Jest library.

Reason

Testing is very important when it comes to a good project. Either your project is small or big, testing should always be important and be paid attention as no developers can cover every potential error or bug. When the project grows, more and more code will be added not only by you but other developers, it is very easy to break things. A new added function seems to work perfectly itself but bringing it to the project is another different story as it involves other factors and sometimes it will break other people's code. However, testing will take care of that as it will help developers fully run the project with every edge cases, and even good cases.

Experience

At the beginning, I didn't think that it would take much of the time as it looks very simple and easy to write. But when I started write the very first one, I was like what should I test with this code?

Testing is not way too difficult but it has its own challenges. For example, writing a feature in nodejs just takes 15 mins and 30 lines of code, however, writing testings for that feature will take 2 to 3 times that amount, or more. Testing doesn't require much of researching but a lot of thinking to make sure that I've covered enough cases. More testings can be added later when I discover a case that can break your code. I haven't got a chance to finish my testing code but I'll try my best to add more testings for my project.

As I have around 6 classes, so I'll have 5 to 6 testing files. There's a class that has 2 functions that call an external library, so I don't need to add testings for that class.

However, there' still a few errors with my testing code as it generates some warnings while testing and I have not done with my e2e testing part.

How to test

Testing the project is easy and can be done by using npm test to run all the tests, or npm test e2e to just run end-to-end testing.

The project can be found here

Top comments (0)