Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/javascript-api/jslib/testing/non-retrying-assertions/tocontainequal.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/jslib/testing/non-retrying-assertions/tocontainequal/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
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
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
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

