grafana/k6 · error

execution.test.fail() called outside of VU context

Error message

execution.test.fail() called outside of VU context

What it means

Error "execution.test.fail() called outside of VU context" thrown in grafana/k6.

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 93accf6570)

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@93accf6570 (2026-08-15). Data as JSON: /api/errors/caf73ecd6cc717ac. Report an issue: GitHub.