Menu
Choose a product
Viewing: v1.7.x (latest)
Find another version
Scroll for more
Open source
toContain()
The toContain() method asserts that an array contains a specific item or a string contains a substring.
Syntax
JavaScript
expect(actual).toContain(expected);
expect(actual).not.toContain(expected);Parameters
| Parameter | Type | Description |
|---|---|---|
| expected | any | The value to search for |
Returns
| Type | Description |
|---|---|
| void | No return value |
Description
The toContain() method checks if:
- An array contains a specific element (using
includes()) - A string contains a substring (using
includes())
For arrays, it uses strict equality (===) to match elements. For more complex object matching in arrays, use toContainEqual().
Usage
JavaScript
import { expect } from 'https://jslib.k6.io/k6-testing/0.6.1/index.js';
export default function () {
const numbers = [1, 2, 3, 4, 5];
expect(numbers).toContain(3);
expect(numbers).not.toContain(6);
const fruits = ['apple', 'banana', 'orange'];
expect(fruits).toContain('banana');
expect(fruits).not.toContain('grape');
}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.