grafana/k6 · error

error marshaling setup() data to JSON: %w

Error message

error marshaling setup() data to JSON: %w

What it means

The value returned by the script's setup() could not be serialized to JSON. setupData must be JSON-marshaled so it can be distributed to all VUs (and optionally persisted in archives); the marshal fails when the exported value contains circular references, functions, or other non-JSON-serializable types. The setup phase aborts and the test does not start.

Source

Thrown at internal/js/runner.go:305

	}
	r.preInitState.Logger.Debugf("Running %s()...", consts.SetupFn)

	setupCtx, setupCancel := context.WithTimeout(ctx, r.getTimeoutFor(consts.SetupFn))
	defer setupCancel()

	v, err := r.runPart(setupCtx, out, consts.SetupFn, nil)
	if err != nil {
		return err
	}
	// r.setupData = nil is special it means undefined from this moment forward
	if sobek.IsUndefined(v) {
		r.setupData = nil
		return nil
	}

	r.setupData, err = json.Marshal(v.Export())
	if err != nil {
		return fmt.Errorf("error marshaling setup() data to JSON: %w", err)
	}
	var tmp any
	return json.Unmarshal(r.setupData, &tmp)
}

// GetSetupData returns the setup data as json if Setup() was specified and executed, nil otherwise
func (r *Runner) GetSetupData() []byte {
	return r.setupData
}

// SetSetupData saves the externally supplied setup data as json in the runner, so it can be used in VUs
func (r *Runner) SetSetupData(data []byte) {
	r.setupData = data
}

// Teardown runs the teardown function if there is one.
func (r *Runner) Teardown(ctx context.Context, out chan<- metrics.SampleContainer) error {
	if !r.IsExecutable(consts.TeardownFn) {

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Return only JSON-safe data from setup(): plain objects, arrays, strings, numbers, booleans, null
  2. Remove circular references between objects before returning them
  3. Convert or drop functions, class instances with methods, and Sobek-specific objects; deep-copy into plain structures
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at internal/js/runner.go:305 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/25ac8446c59c8725. Report an issue: GitHub.