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/tohaveproperty.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/javascript-api/jslib/testing/non-retrying-assertions/tohaveproperty/. 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.
toHaveProperty()
The toHaveProperty() method asserts that an object has a specific property, optionally with a specific value. It supports both simple property names and nested property paths.
Syntax
expect(actual).toHaveProperty(keyPath);
expect(actual).toHaveProperty(keyPath, value);
expect(actual).not.toHaveProperty(keyPath);
expect(actual).not.toHaveProperty(keyPath, value);Parameters
| Parameter | Type | Description |
|---|---|---|
| keyPath | string | Array | The property path to check |
| value | any | Optional expected value for the property |
Returns
| Type | Description |
|---|---|
| void | No return value |
Description
The toHaveProperty() method checks if an object has a specific property. The property path can be:
- A simple property name (e.g.,
'name') - A nested property path using dot notation (e.g.,
'user.address.city') - An array of property keys (e.g.,
['user', 'address', 'city'])
If a value is provided, it also checks that the property has that exact value.
Usage
import { expect } from 'https://jslib.k6.io/k6-testing/0.6.1/index.js';
export default function () {
const user = {
id: 123,
name: 'John Doe',
email: 'john@example.com',
active: true,
};
// Check property existence
expect(user).toHaveProperty('id');
expect(user).toHaveProperty('name');
expect(user).toHaveProperty('email');
expect(user).toHaveProperty('active');
// Check missing property
expect(user).not.toHaveProperty('phone');
expect(user).not.toHaveProperty('address');
}Was this page helpful?
Related resources from Grafana Labs

