jest expect not contain

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:

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.

// 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).

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe