The toInclude assertion checks if an array or an object includes some things. Now if it's an array, we can check if it includes a certain item in the array. If it's an object, we can check if it includes certain properties. Let's run through an example of that.
We'll expect that an array with the numbers 2, 3, and 4 inside the it callback has the number 5 inside and we can do that using toInclude:
it('should expect some values', () => {
// expect(12).toNotBe(12);
// expect({name: 'andrew'}).toNotEqual({name: 'Andrew'});
expect([2,3,4]).toInclude(5);
});
The toInclude assertion takes the item. In this case, we'll check if the array has 5 inside. Now clearly it doesn't, so this test will fail:

We get the message, Expected [ 2, 3, 4] to include 5. That does not exist. Now we change this to a number that does exist, for example 2:
it('should expect some values', () => {
// expect(12).toNotBe(12);
// expect({name: 'andrew'}).toNotEqual({name: 'Andrew'});
expect([2,3,4]).toInclude(2);
});
We'll rerun the test suite and everything will work as expected:

Now, along with toInclude, we have toExclude like this:
it('should expect some values', () => {
// expect(12).toNotBe(12);
// expect({name: 'andrew'}).toNotEqual({name: 'Andrew'});
expect([2,3,4]).toExclude(1);
});
This will check if something does not exist, for example the number 1, which is not in the array. If we run this assertion, the test passes:

The same two methods, toInclude and toExclude, work with objects as well. We can play with that right on the next line. I'll expect that the following object has something on it:
it('should expect some values', () => {
// expect(12).toNotBe(12);
// expect({name: 'andrew'}).toNotEqual({name: 'Andrew'});
// expect([2,3,4]).toExclude(1);
expect({
})
});
Let's go ahead and create an object that has a few properties. These are:
- name: We'll set it equal to any name, let's say Andrew.
- age: We'll set that equal to age, say 25.
- location: We'll set that equal to any location, for example Philadelphia.
This will look like the following code block:
it('should expect some values', () => {
// expect(12).toNotBe(12);
// expect({name: 'andrew'}).toNotEqual({name: 'Andrew'});
// expect([2,3,4]).toExclude(1);
expect({
name: 'Andrew',
age: 25,
location: 'Philadelphia'
})
});
Now let's say we want to make some assertions about particular properties, not necessarily the entire object. We can use toInclude to assert that the object has some properties and that those property values equals the value we pass in:
it('should expect some values', () => {
// expect(12).toNotBe(12);
// expect({name: 'andrew'}).toNotEqual({name: 'Andrew'});
// expect([2,3,4]).toExclude(1);
expect({
name: 'Andrew',
age: 25,
location: 'Philadelphia'
}).toInclude({
})
});
For example, the age property. Let's say we only care about the age. We can assert that the object has an age property equal to 25 by typing the following code:
it('should expect some values', () => {
// expect(12).toNotBe(12);
// expect({name: 'andrew'}).toNotEqual({name: 'Andrew'});
// expect([2,3,4]).toExclude(1);
expect({
name: 'Andrew',
age: 25,
location: 'Philadelphia'
}).toInclude({
age: 25
})
});
It doesn't matter that there's a name property. The name property could be any value. That is irrelevant in this assertion. Now let's use the value, 23:
.toInclude({
age: 23
})
This test will fail as shown here since the value is not correct:

We expected the age property to be 23, but it was indeed 25, so the test fails. The same thing goes with the toExclude assertion.
Here we can save our test files. This checks if the object does not have a property age equal to 23. It does indeed not have that, so the test passes:

This is just a quick taste as to what expect can do. For a full list of features, I recommend diving through the documentation. There's a ton of other assertions you can use, things like checking if a number is greater than another number, if a number is less than or equal to another number, all sorts of math-related operations are included as well.