DEV Community

Paul Kim
Paul Kim

Posted on

Writing Tests

For this week in Topics in Open Source Development, we were tasked with adding tests to an existing project. Here is my experience doing so.

Project Repository: https://github.com/paulkim26/til-to-html
Commit: https://github.com/paulkim26/til-to-html/commit/2a00cf69b4fab11c65baedc9ca518fe3bf36e9d6

Testing Framework

Since my project already uses Bun, I decided to use Bun's own built-in test runner. It is based on Jest which makes it easy to pick for developers familiar with that testing framework. As a result, there was no additional setup required.

Writing Tests

For this project, I added tests that test the markdown parsing modules I had previously written (source). Since each markdown parsing module accepts multiple syntaxes e.g. markdown italics can be represented as either _text_ or *text*, I made sure to test for each matching combination along with an aberrant test case (e.g. an empty string) (example).

One nice thing I realized working with TypeScript is that I don't have to write additional tests that check for strange type combinations e.g. passing a null to a function that accepts only strings, since TypeScript already blocks such cases. If I had written it in a weakly typed language i.e. JavaScript, I would have had to cover these cases as well.

Takeaways

I have only had limited experience writing tests before; this was a good experience to write some more. I was surprised to learn how verbose and comprehensive the act of writing tests can be. It is in its verbosity, however that test writing is great since it is easily readable for a human. I see no reason for any project to not include its own suite of tests, especially open source ones. I will for sure continue to write tests in all of my projects going forward.

Top comments (0)