grafana/k6 · error

group() requires a callback as a second argument

Error message

group() requires a callback as a second argument

What it means

`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.

Source

Thrown at internal/js/modules/k6/k6.go:96

		timer.Stop()
	}
}

// RandomSeed sets the seed to the random generator used for this VU.
func (mi *K6) RandomSeed(seed int64) {
	randSource := rand.New(rand.NewSource(seed)).Float64 //nolint:gosec
	mi.vu.Runtime().SetRandSource(randSource)
}

// Group wraps a function call and executes it within the provided group name.
func (mi *K6) Group(name string, val sobek.Value) (sobek.Value, error) {
	state := mi.vu.State()
	if state == nil {
		return nil, ErrGroupInInitContext
	}

	if common.IsNullish(val) {
		return nil, errors.New("group() requires a callback as a second argument")
	}
	fn, ok := sobek.AssertFunction(val)
	if !ok {
		return nil, errors.New("group() requires a callback as a second argument")
	}
	if common.IsAsyncFunction(mi.vu.Runtime(), val) {
		return sobek.Undefined(), errors.New("group() does not support async functions as arguments, " +
			"please see https://grafana.com/docs/k6/latest/javascript-api/k6/group/ for more info")
	}
	oldGroupName, _ := state.Tags.GetCurrentValues().Tags.Get(metrics.TagGroup.String())
	// TODO: what are we doing if group is not tagged
	newGroupName, err := lib.NewGroupPath(oldGroupName, name)
	if err != nil {
		return sobek.Undefined(), err
	}

	shouldUpdateTag := state.Options.SystemTags.Has(metrics.TagGroup)
	if shouldUpdateTag {

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass the function as the second argument: `k6.group('mygroup', () => { ... })`
  2. Check the variable holding the callback is defined and is a function before calling group

Example fix

// before
k6.group('fetch-flow');

// after
k6.group('fetch-flow', () => {
  http.get('https://test.k6.io/');
});
Defensive patterns

Strategy: validation

Validate before calling

if (fn === null || fn === undefined) { throw new TypeError('group() needs a callback as the second argument'); }

Type guard

const isCallable = (v) => typeof v === 'function' || v === null || v === undefined ? typeof v === 'function' : false;

Try / catch

try {
  k6.group('phase', fn);
} catch (e) {
  if (String(e.message).includes('requires a callback')) throw new TypeError(`group callback missing or not callable`);
  throw e;
}

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/a0fb2226a1c27a55. Report an issue: GitHub.