DEV Community

Cynthia Fotso
Cynthia Fotso

Posted on

Mastering Testing: My journey with Jest in my project

As part of continuously studying for my open source course, I had to try testing for the first time. This week focused on setting up a testing framework, writing unit tests and trying to integrate if possible code coverage analysis.

Choosing a Testing Framework

For my Repository Context Packager project (a CLI tool for packaging repository content), I had to chose among a variety of testing frameworks like Cypress, Jest, Vitest one that will best work and enable me write, organize and execute test cases for my project. I chose Jest because it is a popular and zero-configuration testing library that supports several options.
Furthermore, it was my best choice because of the following:

  • After some research, i noticed it is widely used in the JavaScript ecosystem, with excellent documentation and community support.
  • It includes built-in features like mocking and coverage, reducing the need for additional tools.

Here is the link to its GitHub repo https://github.com/jestjs/jest.

Setting Up Jest in my Project

Setting up Jest was straightforward but required tweaks for ECMAScript modules. Here's a step-by-step guide:

To install Jest, I ran npm install --save-dev jest to add it as a dependency. I updated my scripts in package.json to include it and started creating test files in a tests folder and writing tests. When i started testing the matchesIncludePatterns function, i got stuck as I had a failed test and the error message was cannot use import statement outside a module. I searched online and on the stackoverflow page, i saw this is common with Jest if you used ES Modules which are the import and export statements in the code. To fix this, I updated the test script with a flag to support these modules for Jest, and upon running the tests with npm test, they passed.

I implemented other tests and to be able to run a single test, i ran
npm test -- --testNamePattern="test name". Verifying coverage was also very easy as I only had to run npm run test:coverage which produced a HTML report in the coverage folder and also displayed the report in my terminal. I added this to .gitignore to avoid committing generated files.

Writing Test Cases (Lessons and Challenges)

This was the toughest part. I started with the matchesIncludePatterns function from fs-utils.js. I followed by config-utils.js and finally git-utils.js. Jest has this powerful utility jest.fn which allows to create mock functions that enable us test the behavior of functions indirectly by calling other pieces of code rather than just testing their output; I implemented this and covered good values, bad values and some edge cases.

Reflections on the Process

This was my first deep dive into testing. Writing tests forced me to think in order to anticipate failures and improve my code's reliability.

Some takeaways are:

  • Testing isn't just about passing code; it's about confidence in changes.
  • And coverage reports guide where to add tests as we can see the missed lines during testing.

I'll definitely incorporate testing in future projects. It's a skill that pays off in maintainability and collaboration. If you're new to this, start small with one function; it is addictive once you see the benefits!

Top comments (0)