Menu
Open source

toContainEqual()

The toContainEqual() method asserts that an array or set contains an element that is deeply equal to the expected value, using the same equality logic as toEqual().

Syntax

JavaScript
expect(actual).toContainEqual(expected);
expect(actual).not.toContainEqual(expected);

Parameters

ParameterTypeDescription
expectedanyThe value to search for using deep equality

Returns

TypeDescription
voidNo return value

Description

The toContainEqual() method checks if an array or set contains an element that is deeply equal to the expected value. Unlike toContain(), which uses strict equality (===), toContainEqual() performs deep equality comparison, making it suitable for finding objects with matching content even if they are different instances.

Usage

JavaScript
import http from 'k6/http';
import { expect } from 'https://jslib.k6.io/k6-testing/0.5.0/index.js';

export default function () {
  const response = http.get('https://quickpizza.grafana.com/');

  // Create test data to demonstrate toContainEqual
  const pizzaOptions = [
    { name: 'Margherita', toppings: ['tomato', 'mozzarella'] },
    { name: 'Pepperoni', toppings: ['tomato', 'mozzarella', 'pepperoni'] },
    { name: 'Veggie', toppings: ['tomato', 'mozzarella', 'peppers'] },
  ];

  // Check if array contains specific pizza objects
  expect(pizzaOptions).toContainEqual({
    name: 'Margherita',
    toppings: ['tomato', 'mozzarella'],
  });

  // Works with various data types
  const mixedArray = ['string', 123, { key: 'value' }, [1, 2, 3]];
  expect(mixedArray).toContainEqual({ key: 'value' });
  expect(mixedArray).toContainEqual([1, 2, 3]);
  expect(mixedArray).not.toContainEqual({ key: 'different' });
}