Adding Jest tests to a project

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Good introduction Jest and testing is not that scary when you get used to the concept.
Exactly, once you are in the mindset of writing them they become very easy to write and maintain.
This blog came at a perfect time. I am starting to look into on how to do test with jest. This summarizes nicely. I think I can learn with their documentation smoothly. 😀
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Now that we are talking about testing, let's try out Jest, a pretty well-used testing framework for JavaScript.
I've been using Jest for the past couple of months, finding it really powerful so far.
There are some other really great frameworks out there, and choosing one comes down to flavor and needs.
For this article, I'll use a simple JavaScript project as our basis. It will have a determination script to determine if certain statements are true or false.
You can download the starting point from GitHub.
You can see the determine.js function in the main folder.
This is basically our app. It's used to determine if a variable is a number.
In the most basic case, it should perform the following tests:
isNumber(123);
// Should be true
isNumber('abc');
// Should be false
We first have to add Jest to our project to make this testable.
npm install -D jest
The code above will install the jest package in our dev dependencies.
Now we should modify our package.json to include a testing command.
"scripts": {
"test": "jest"
}
The cool thing about Jest is that it can fire automatically on any files it deems testable.
By default, it can run on the following files:
.js files inside __tests__ folders.test.js files inside your project.spec.js files inside your projectFor now, let's add a determine.test.js file. This makes sense as we have our determine.js file as our basis.
To start the file, we have to import our function first.
const { isNumber } = require('./determine');
Then we can define tests. Each test is an isolated scope that can pass or fail.
Let's start by adding a test made to expect the right answer.
test('Validate a number', () => {
expect(isNumber(1)).toBeTruthy();
});
We start by defining the test, which holds a string (the name of the test) and executes a function. The actual test states:
Expect -> function (variables) -> to be true
I really love how Jest made these super human-readable and understandable.
We ask for the function isNumber to be true when we pass the number one to it.
We can now run the test by invoking the following command:
npm run test

And as you can see, our test is succeeding.
Let's step up our test and add a failing test, but leave the same evaluation.
test('Invalidate a string', () => {
expect(isNumber('ABC')).toBeTruthy();
});
We use the same test but pass a wrong number, so the test should fail.

And it does fail, which is expected! So let's modify the test case to evaluate a false value.
test('Invalidate a string', () => {
expect(isNumber('ABC')).toBeFalsy();
});
Our test will succeed, but we still check for a failed value!
The completed test can be found here on GitHub.
Although this is a very straightforward test, it can be super important. Let's say we modify something in this isNumber function. Our test will now quickly show something went wrong.
I'll be writing a bit more about writing some particular test cases and showing you how you can use and leverage Jest.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter