{"record":{"id":"85fa7b75e9ec4d77","repo":"grafana/k6","slug":"exported-teardown-must-be-a-function","errorCode":null,"errorMessage":"exported 'teardown' must be a function","messagePattern":"exported 'teardown' must be a function","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/bundle.go","lineNumber":227,"sourceCode":"\t\t\t\tdec.DisallowUnknownFields()\n\t\t\t\tif err = dec.Decode(&b.Options); err != nil {\n\t\t\t\t\tif uerr := json.Unmarshal(data, &b.Options); uerr != nil {\n\t\t\t\t\t\t// Beautify the error so we can try to show the user the key and potential value that is failing to parse\n\t\t\t\t\t\tuerr = beautifyOptionsJSONUnmarshalError(data, uerr)\n\t\t\t\t\t\terr = errext.WithAbortReasonIfNone(\n\t\t\t\t\t\t\terrext.WithExitCodeIfNone(uerr, exitcodes.InvalidConfig),\n\t\t\t\t\t\t\terrext.AbortedByScriptError,\n\t\t\t\t\t\t)\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t\tb.preInitState.Logger.WithError(err).Warn(\"There were unknown fields in the options exported in the script\")\n\t\t\t\t\terr = nil\n\t\t\t\t}\n\t\t\tcase consts.SetupFn:\n\t\t\t\terr = errors.New(\"exported 'setup' must be a function\")\n\t\t\t\treturn\n\t\t\tcase consts.TeardownFn:\n\t\t\t\terr = errors.New(\"exported 'teardown' must be a function\")\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t})\n\t<-ch\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif len(b.callableExports) == 0 {\n\t\treturn errors.New(\"no exported functions in script\")\n\t}\n\n\treturn nil\n}\n\nfunc beautifyOptionsJSONUnmarshalError(data []byte, err error) error {\n\tunmarshalTypError := new(json.UnmarshalTypeError)","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/bundle.go#L209-L245","documentation":"populateExports (internal/js/bundle.go:227) enforces that the reserved export name 'teardown' is a function, since k6 invokes teardown(data) exactly once at test end to clean up. Any non-callable value exported as teardown aborts script loading with this error.","triggerScenarios":"`export const teardown = null`, `export let teardown = {}`, or re-exporting a non-function symbol named teardown from another module (`export { cleanupMap as teardown }`).","commonSituations":"Commenting out the teardown body but leaving a placeholder variable; merging modules where teardown collides with a helper object; partial migration from other harnesses.","solutions":["Declare it as a function: `export function teardown(data) { ... }`.","Remove the export entirely if there is no cleanup to do.","Fix accidental re-exports of a non-function teardown symbol."],"exampleFix":"// before\nexport const teardown = () => {};  // exported arrow is fine, but `= {}` or `= null` fails\n// after\nexport function teardown(data) { /* cleanup */ }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// reject non-function teardown exports before shipping\nconst src = require('fs').readFileSync('script.js', 'utf8');\nif (/export\\s+(const|let|var)\\s+teardown\\b/.test(src)) {\n  throw new Error(\"exported 'teardown' must be a function\");\n}","tryCatchPattern":null,"preventionTips":["When disabling teardown temporarily, comment out the whole export rather than stubbing with null.","Avoid `export *` from helper modules that may re-export teardown-like symbols."],"tags":["script-structure","exports","teardown","javascript"],"backgroundTag":"invalid-export-type","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"}