Common Jest matchers

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.
No comments yet. Be the first to comment.
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...

Jest using something called matchers to test different results. These matchers can be used to determine if a criterion is what we expect it to be.
In the previous article we've used toBeTruthy and toBeFalsy.
However, there are many different ones we can leverage. In this article, we'll look at some commonly used ones.
In its simplest form, we can use the toBe matcher to assume something needs "to be" a specific value.
For example:
test('two plus two is four', () => {
expect(2 + 2).toBe(4);
});
This is a calculation where we always expect the result to be four. The same can be used to determine string manipulation.
test('uppercase is correct', () => {
expect('abc'.toUpperCase()).toBe('ABC');
});
Often we also want to determine if an object is a particular format.
For this, we can use the toEqual check.
test('should modify a object', () => {
const data = { foo: 'bar' };
data['baz'] = 'fuu';
expect(data).toEqual({ foo: 'bar', baz: 'fuu' });
});
Another well-used method is to check if specific criteria are no longer met. Sometimes we don't know the exact output, but we don't want the outcome to be something specific.
Let's say we have a dice, which is zero, and on the roll, it should be anything but zero.
test('roll the dice', () => {
let dice = 0;
expect(rollDice(dice)).not.toBe(0);
});
When it comes to Arrays, we might want to check if a specific element is included in the array.
For that, we can use the toContain test.
test('Dont forget the milk', () => {
const shoppingList = ['coffee', 'eggs', 'milk'];
expect(shoppingList).toContain('milk');
});
As we saw, we can check for something being true or false, but we have a couple extra truthiness checks.
toBeNull: If the output is precisely nulltoBeUndefined: If the output is precisely undefinedtoBeDefined: Anything but undefinedtoBeTruthy: Anything considered truetoBeFalsy: Anything considered falseThese can be super good to check for specific assertions, and especially to make sure something is not one of these.
So far, we have seen we can use toBe to match specific numbers.
But we can go one step further and even determine to which degree numbers should match.
toBeGreaterThan: Should be more significant than a certain numbertoBeGreaterThanOrEqual: Greater than or equal to certain numbertoBeLessThan: Less than a certain numbertoBeLessThanOrEqual: Less than or equal to a certain numbertoBeCloseTo: For floating points to be close to a specific decimalOften we only want to match strings based on a specific part. As we might not 100% know the rest of the value.
For this, you can use toMatch. It takes regular regex as its parameter.
test('Test for part of string', () => {
expect('Christian').toMatch(/Chris/);
});
The other way around, we can make sure that a part is not in this string.
test('Make sure nothing went wrong', () => {
expect('Something went wrong, sorry').not.toMatch(/wrong/);
});
Note: The above will fail as we do have the word "wrong" in our test case
These are some elementary operations, but it's not the complete list of operations we can use.
A lot of those are super cool to determine if elements are visible on the screen, so we'll get into that later.
Which test do you use all the time?
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter