grafana/k6 · error
execution.test.fail() called outside of VU context
Error message
execution.test.fail() called outside of VU context
What it means
Thrown (via common.Throw, so it surfaces as a JS exception) by execution.test.fail() when mi.vu.State() is nil — a state guard proving the call was not made from within an active VU context. test.fail() must terminate a running test iteration, which requires VU state; calling it from the init context (or another stateless phase) leaves nothing to mark as failed, so the guard rejects the call instead of silently doing nothing.
Solutions
- Call execution.test.fail() inside a VU-context function (default function, setup, teardown), not in the init context
- Move the failure logic into the scenario/iteration code where execution state exists
- For init-context aborts, use execution.test.abort() which is designed for that purpose
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at internal/js/modules/k6/execution/execution.go:191 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18).
Data as JSON: /api/errors/caf73ecd6cc717ac.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/execution/execution.go:191
// stop the test run
"abort": func() any {
return func(msg sobek.Value) {
reason := errext.AbortTest
if !common.IsNullish(msg) {
reason = fmt.Sprintf("%s: %s", reason, msg.String())
}
rt.Interrupt(&errext.InterruptError{Reason: reason})
}
},
"fail": func() any {
return func(msg sobek.Value) {
reason := errext.MarkedAsFailedTest
if !common.IsNullish(msg) {
reason = fmt.Sprintf("%s: %s", reason, msg.String())
}
if mi.vu.State() == nil {
common.Throw(rt, fmt.Errorf("execution.test.fail() called outside of VU context"))
}
mi.vu.State().Logger.Errorf(reason)
mi.vu.State().TestStatus.MarkFailed()
}
},
"options": func() any {
vuState := mi.vu.State()
if vuState == nil {
common.Throw(rt, errTestInfoInitContext)
}
if optionsObject == nil {
opts, err := optionsAsObject(rt, vuState.Options)
if err != nil {
common.Throw(rt, err)
}
optionsObject = opts
}View on GitHub (pinned to 01ffac6f24)