Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/next/javascript-api/jslib/testing/non-retrying-assertions/tocontainequal.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/javascript-api/jslib/testing/non-retrying-assertions/tocontainequal/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
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

