Skip to content
the poor coder

jest expect not contain

Poor Man
Published
Reading time
3 min read
On this page

Jest expect not contain#

Explanation#

When working with Jest, the expect function is used to make assertions about the expected behavior of the code being tested. The not keyword can be used to negate an assertion, meaning that it expects the opposite result. The toContain matcher checks whether a given item is included in an array or a string.

Therefore, jest expect not contain refers to the situation where we want to test that a string or array does not contain a particular value.

Example#

Let's say we have a function that returns an array of even numbers. We want to test that the array does not contain any odd numbers. Here's how we can do it:

JavaScript
function getEvenNumbers(arr) {
  return arr.filter(num => num % 2 === 0);
}

// Test case
test('returns only even numbers', () => {
  const nums = [2, 4, 6, 8];
  const result = getEvenNumbers(nums);
  expect(result).not.toContain(1); // Make sure the array does not contain odd numbers
});

In this example, we use the not.toContain matcher to check that the result of our function does not contain the number 1, which is an odd number. If the array contained an odd number, the test would fail.

Alternative ways to test for absence#

In addition to using not.toContain, there are other ways to test for absence:

  • toBeUndefined(): checks whether a value is undefined
  • toBeNull(): checks whether a value is null
  • toBeFalsy(): checks whether a value is falsy (e.g. false, 0, '', null, undefined)
  • not.toBeTruthy(): checks whether a value is not truthy (i.e. it's either false, 0, '', null, or undefined)

These matchers can be useful in different situations depending on the test case.

JavaScript
// Test case using toBeUndefined
test('returns undefined for non-array input', () => {
  const result = getEvenNumbers('not an array');
  expect(result).toBeUndefined();
});

// Test case using toBeNull
test('returns null for null input', () => {
  const result = getEvenNumbers(null);
  expect(result).toBeNull();
});

// Test case using toBeFalsy
test('returns empty array for odd numbers input', () => {
  const nums = [1, 3, 5, 7];
  const result = getEvenNumbers(nums);
  expect(result).toBeFalsy(); // Make sure the result is falsy (i.e. it's an empty array)
});

// Test case using not.toBeTruthy
test('returns non-empty array for even numbers input', () => {
  const nums = [2, 4, 6, 8];
  const result = getEvenNumbers(nums);
  expect(result).not.toBeTruthy(); // Make sure the result is not truthy (i.e. it's not an empty array)
});

In these examples, we use different matchers to test for absence in different ways. The toBeUndefined matcher checks whether the result is undefined, the toBeNull matcher checks whether the result is null, the toBeFalsy matcher checks whether the result is falsy (i.e. an empty array), and the not.toBeTruthy matcher checks whether the result is not truthy (i.e. it's not an empty array).

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC