Overview
This week's objective was to set up a unit test environment for izyum and write some unit tests in order to cover core functionality.
Testing engine
Two testing engines are usually used for testing purposes in the ECMAScript world which are jest and mocha. I had previous experience with both of them and to be honest I don't see big differences between them on the basic level. I decided to go with it just because it is more popular and I did use it with typescript before so I did know it had good types of support.
Installation
The installation process was pretty straightforward and I just followed the official tutorial. On the top of basic jest package, I had to install some additional packages that provide typescript support. I faced a minor issue trying to run the first test, but StackOverflow as always came in handy and I just had to add a small config which helped me to resolve this issue.
Writing tests
I decided to write tests for my generated-file module where all my "core" functionality was. The code there wasn't structured where good so I change the business logic a little bit. For example, before the module was provided with a file path and was fetching the file from the file system itself but after some refactoring, I extracted that functionality out of the module and now it just consumes the input string. Some tests that I wrote were to test conversion from txt input and MD input which is the main thing app does.
Watch mode
The good thing jest has out-of-the-box is the "watch" functionality. So if you run tests with a command like jest --watchAll
your tests would be rerun every time your change something, which is extremely useful, especially when you do some sort of TDD. So I ended up adding it as a command to package.json.
Coverage
Another thing that jest has is a coverage report. It shows how many lines of code are covered by tests and it provides a good idea about the weak spots of tests and some scenarios that are not covered. Users can get the coverage report by running npm run coverage
.
CONTRIBUTING.md
I updated the CONTRIBUTING.md with new testing functionalities that I introduced in these updates, where I described how to use all new commands.
Conclusion
When I was done I squashed all commit and rebased to the main. Overall, I enjoyed doing it and extending the environment of my project with some nice unit testing possibilities.
Top comments (0)