Menu
Choose a product
Viewing: v1.7.x (latest)
Find another version
Scroll for more
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
| Parameter | Type | Description |
|---|---|---|
| expected | any | The value to search for using deep equality |
Returns
| Type | Description |
|---|---|
| void | No 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.6.1/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' });
}Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.