Menu
Choose a product
Viewing: v1.7.x (latest)
Find another version
Scroll for more
Open source
check( val, sets, [tags] )
Run checks on a value. A check is a test condition that can give a truthy or
falsy result. The sets parameter contains one or more checks, and the check()
function will return false if any of them fail.
Note that checks are not asserts in their traditional sense - a failed assertion will throw an error, while a check will always return with a pass or a failure. Failure conditions can then instead be controlled by thresholds, for more power and flexibility.
| Parameter | Type | Description |
|---|---|---|
| val | any | Value to test. |
| sets | object | Tests (checks) to run on the value. |
| tags (optional) | object | Extra tags to attach to metrics emitted. |
Returns
| Type | Description |
|---|---|
| boolean | true if all checks have succeeded, false otherwise. |
Examples
Using check() to verify that an HTTP response code was 200:
JavaScript
JavaScript
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const res = http.get('https://quickpizza.grafana.com/');
check(res, {
'response code was 200': (res) => res.status == 200,
});
}Using check() with a custom tag to verify that an HTTP response code was 200 and that body was 1234 bytes. The checkOutput can be used for any condition in your script logic:
JavaScript
JavaScript
import http from 'k6/http';
import { check, fail } from 'k6';
export default function () {
const res = http.get('https://quickpizza.grafana.com/');
const checkOutput = check(
res,
{
'response code was 200': (res) => res.status == 200,
'body size was larger than 123 bytes': (res) => res.body.length > 123,
},
{ myTag: "I'm a tag" }
);
if (!checkOutput) {
fail('unexpected response');
}
}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.