{"record":{"id":"a0fb2226a1c27a55","repo":"grafana/k6","slug":"group-requires-a-callback-as-a-second-argument","errorCode":null,"errorMessage":"group() requires a callback as a second argument","messagePattern":"group\\(\\) requires a callback as a second argument","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/k6.go","lineNumber":96,"sourceCode":"\t\ttimer.Stop()\n\t}\n}\n\n// RandomSeed sets the seed to the random generator used for this VU.\nfunc (mi *K6) RandomSeed(seed int64) {\n\trandSource := rand.New(rand.NewSource(seed)).Float64 //nolint:gosec\n\tmi.vu.Runtime().SetRandSource(randSource)\n}\n\n// Group wraps a function call and executes it within the provided group name.\nfunc (mi *K6) Group(name string, val sobek.Value) (sobek.Value, error) {\n\tstate := mi.vu.State()\n\tif state == nil {\n\t\treturn nil, ErrGroupInInitContext\n\t}\n\n\tif common.IsNullish(val) {\n\t\treturn nil, errors.New(\"group() requires a callback as a second argument\")\n\t}\n\tfn, ok := sobek.AssertFunction(val)\n\tif !ok {\n\t\treturn nil, errors.New(\"group() requires a callback as a second argument\")\n\t}\n\tif common.IsAsyncFunction(mi.vu.Runtime(), val) {\n\t\treturn sobek.Undefined(), errors.New(\"group() does not support async functions as arguments, \" +\n\t\t\t\"please see https://grafana.com/docs/k6/latest/javascript-api/k6/group/ for more info\")\n\t}\n\toldGroupName, _ := state.Tags.GetCurrentValues().Tags.Get(metrics.TagGroup.String())\n\t// TODO: what are we doing if group is not tagged\n\tnewGroupName, err := lib.NewGroupPath(oldGroupName, name)\n\tif err != nil {\n\t\treturn sobek.Undefined(), err\n\t}\n\n\tshouldUpdateTag := state.Options.SystemTags.Has(metrics.TagGroup)\n\tif shouldUpdateTag {","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/k6.go#L78-L114","documentation":"`k6.group(name, fn)` requires a function as its second argument. At internal/js/modules/k6/k6.go:94 the value is first checked with common.IsNullish; passing null/undefined yields \"group() requires a callback as a second argument\". Group executes the callback inside a tagged scope, so there is nothing meaningful to run without it.","triggerScenarios":"`k6.group('mygroup')` with the callback omitted, or `k6.group('mygroup', null)` / `k6.group('mygroup', undefined)` — e.g. the function variable was never defined or a conditional produced null.","commonSituations":"Typos or scoping mistakes leave the callback undefined; optional wrappers pass a variable that is conditionally assigned; scripts migrated from other tools that call group(name) only for tagging.","solutions":["Pass the function as the second argument: `k6.group('mygroup', () => { ... })`","Check the variable holding the callback is defined and is a function before calling group"],"exampleFix":"// before\nk6.group('fetch-flow');\n\n// after\nk6.group('fetch-flow', () => {\n  http.get('https://test.k6.io/');\n});","handlingStrategy":"validation","validationCode":"if (fn === null || fn === undefined) { throw new TypeError('group() needs a callback as the second argument'); }","typeGuard":"const isCallable = (v) => typeof v === 'function' || v === null || v === undefined ? typeof v === 'function' : false;","tryCatchPattern":"try {\n  k6.group('phase', fn);\n} catch (e) {\n  if (String(e.message).includes('requires a callback')) throw new TypeError(`group callback missing or not callable`);\n  throw e;\n}","preventionTips":["Always write group with an inline arrow: k6.group('name', () => { ... })","Check typeof on dynamic callbacks before passing them","Lint for k6.group( calls that do not contain '=> ' or 'function'"],"tags":["k6-api","javascript","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}