{"record":{"id":"caf73ecd6cc717ac","repo":"grafana/k6","slug":"execution-test-fail-called-outside-of-vu-context","errorCode":null,"errorMessage":"execution.test.fail() called outside of VU context","messagePattern":"execution\\.test\\.fail\\(\\) called outside of VU context","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/execution/execution.go","lineNumber":191,"sourceCode":"\t\t// stop the test run\n\t\t\"abort\": func() any {\n\t\t\treturn func(msg sobek.Value) {\n\t\t\t\treason := errext.AbortTest\n\t\t\t\tif !common.IsNullish(msg) {\n\t\t\t\t\treason = fmt.Sprintf(\"%s: %s\", reason, msg.String())\n\t\t\t\t}\n\t\t\t\trt.Interrupt(&errext.InterruptError{Reason: reason})\n\t\t\t}\n\t\t},\n\t\t\"fail\": func() any {\n\t\t\treturn func(msg sobek.Value) {\n\t\t\t\treason := errext.MarkedAsFailedTest\n\t\t\t\tif !common.IsNullish(msg) {\n\t\t\t\t\treason = fmt.Sprintf(\"%s: %s\", reason, msg.String())\n\t\t\t\t}\n\n\t\t\t\tif mi.vu.State() == nil {\n\t\t\t\t\tcommon.Throw(rt, fmt.Errorf(\"execution.test.fail() called outside of VU context\"))\n\t\t\t\t}\n\n\t\t\t\tmi.vu.State().Logger.Errorf(reason)\n\t\t\t\tmi.vu.State().TestStatus.MarkFailed()\n\t\t\t}\n\t\t},\n\t\t\"options\": func() any {\n\t\t\tvuState := mi.vu.State()\n\t\t\tif vuState == nil {\n\t\t\t\tcommon.Throw(rt, errTestInfoInitContext)\n\t\t\t}\n\t\t\tif optionsObject == nil {\n\t\t\t\topts, err := optionsAsObject(rt, vuState.Options)\n\t\t\t\tif err != nil {\n\t\t\t\t\tcommon.Throw(rt, err)\n\t\t\t\t}\n\t\t\t\toptionsObject = opts\n\t\t\t}","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/execution/execution.go#L173-L209","documentation":"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.","triggerScenarios":"Thrown at internal/js/modules/k6/execution/execution.go:191 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}