{"record":{"id":"25ac8446c59c8725","repo":"grafana/k6","slug":"error-marshaling-setup-data-to-json-w","errorCode":null,"errorMessage":"error marshaling setup() data to JSON: %w","messagePattern":"error marshaling setup\\(\\) data to JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/runner.go","lineNumber":312,"sourceCode":"\t}\n\tr.preInitState.Logger.Debugf(\"Running %s()...\", consts.SetupFn)\n\n\tsetupCtx, setupCancel := context.WithTimeout(ctx, r.getTimeoutFor(consts.SetupFn))\n\tdefer setupCancel()\n\n\tv, err := r.runPart(setupCtx, out, consts.SetupFn, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\t// r.setupData = nil is special it means undefined from this moment forward\n\tif sobek.IsUndefined(v) {\n\t\tr.setupData = nil\n\t\treturn nil\n\t}\n\n\tr.setupData, err = json.Marshal(v.Export())\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error marshaling setup() data to JSON: %w\", err)\n\t}\n\tvar tmp any\n\treturn json.Unmarshal(r.setupData, &tmp)\n}\n\n// GetSetupData returns the setup data as json if Setup() was specified and executed, nil otherwise\nfunc (r *Runner) GetSetupData() []byte {\n\treturn r.setupData\n}\n\n// SetSetupData saves the externally supplied setup data as json in the runner, so it can be used in VUs\nfunc (r *Runner) SetSetupData(data []byte) {\n\tr.setupData = data\n}\n\n// Teardown runs the teardown function if there is one.\nfunc (r *Runner) Teardown(ctx context.Context, out chan<- metrics.SampleContainer) error {\n\tif !r.IsExecutable(consts.TeardownFn) {","sourceCodeStart":294,"sourceCodeEnd":330,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/runner.go#L294-L330","documentation":"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.","triggerScenarios":"Thrown at internal/js/runner.go:305 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Return only JSON-safe data from setup(): plain objects, arrays, strings, numbers, booleans, null","Remove circular references between objects before returning them","Convert or drop functions, class instances with methods, and Sobek-specific objects; deep-copy into plain structures"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}