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/latest/javascript-api/k6/fail.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/latest/javascript-api/k6/fail/. 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.
fail( [err] )
Immediately throw an error, aborting the current iteration.
fail() does not abort the test, nor does it make the test exit with non-0 status.
If you are looking to fail the test by halting the execution, use
test.abort() instead
fail() is a simple convenience wrapper on top of JavaScript’s throw(),
because the latter cannot be used as [expr] || throw, which is a convenient way to write k6 test code.
| Parameter | Type | Description |
|---|---|---|
| err (optional) | string | Error message that gets printed to stderr. |
Example
Aborting the current script iteration if a check fails:
import http from 'k6/http';
import { check, fail } from 'k6';
export default function () {
const res = http.get('https://k6.io');
if (
!check(res, {
'status code MUST be 200': (res) => res.status == 200,
})
) {
fail('status code was *not* 200');
}
}Was this page helpful?
Related resources from Grafana Labs

