DEV Community

Add00
Add00

Posted on

Test Test Test

Can you hear me?

Greetings, another week another lab this week covered the topic of automated testing. When selecting a test framework my first thought was to use Jasmine, which I had used previously, however it turns out that Jasmine does not have good support for ES modules. After doing a bit of research I opted to go with Vitest, since it was ES module compatible, and was inter-compatible with the very popular Vite tool chain.

Getting started with Vitest was very simple first I installed is a development dependency using the following command:

npm install -D vitest

Then I added a test script to the package.json:

"test": "vitest"

While custom configs can be set using a vitest.config.ts file, due to the straight forward nature of my project it was not necessary to have one.

While writing the tests I realized that I had structured the code quite poorly and had put too much code into the main.js file. Since the main file acts as the entry point to the program and the main function cannot be exported. To ensure everything could be thoroughly tested I made a new chat.js and utils.js and moved non-entry related code into them. Then made corresponding test files.

In the testing process I ended up finding and fixing several bugs one of them being that I had created a custom Ollama client, but it was never used instead the default Ollama client was used to make requests.

Going forwards I think I will use Vitest as my primary testing suite for personal projects. While using it I did have problems, but those problems where of my own making! Whereas, with Jasmine, and other testing tools I've run into issues involving the tool itself which is never ideal. Especially for a testing kit.

Top comments (0)