grafana/k6 · error
the argument to filter() must be a function, a selector or a
Error message
the argument to filter() must be a function, a selector or a selection
What it means
Selection.Filter() in k6's HTML module accepts exactly three argument kinds: a CSS selector string, another Selection, or a predicate function. The Go code first tries the string and Selection cases via a type switch, then falls back to sobek.AssertFunction; anything that is none of the three throws this error.
Source
Thrown at js/modules/k6/html/html.go:393
common.Throw(s.rt, fmt.Errorf("the function passed to each() failed: %w", err))
}
}
return Selection{s.rt, s.sel.Each(fn), s.URL}
}
func (s Selection) Filter(v sobek.Value) Selection {
switch val := v.Export().(type) {
case string:
return Selection{s.rt, s.sel.Filter(val), s.URL}
case Selection:
return Selection{s.rt, s.sel.FilterSelection(val.sel), s.URL}
}
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to filter() must be a function, a selector or a selection"))
}
return Selection{s.rt, s.sel.FilterFunction(s.buildMatcher(v, sobekFn)), s.URL}
}
func (s Selection) Is(v sobek.Value) bool {
switch val := v.Export().(type) {
case string:
return s.sel.Is(val)
case Selection:
return s.sel.IsSelection(val.sel)
default:
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to is() must be a function, a selector or a selection"))
}View on GitHub (pinned to 93accf6570)
Solutions
- Use a selector string: sel.filter('.active')
- Or a function: sel.filter((idx, el) => el.attr('data-ok') === '1')
- Or wrap an existing selection: sel.filter(otherSelection)
Example fix
// before selection.filter(someElement); // after selection.filter((idx, el) => el.id() === someElement.id());
Defensive patterns
Strategy: type-guard
Type guard
const isFilterArg = (v) =>
typeof v === 'string' ||
(v !== null && typeof v === 'object' && 'sel' in v && typeof v === 'object' && typeof v.sel !== 'undefined') ||
typeof v === 'function';
if (!isFilterArg(arg)) throw new TypeError('filter() expects selector, Selection, or function');
sel.filter(arg); Prevention
- Pass selectors as strings, comparisons as functions, never raw elements
- Confirm variables are defined before using them as filter arguments
- Review ported jQuery code for element-argument patterns
When it happens
Trigger: Calling filter() with a DOM Element (e.g. an item obtained from each()), a number, null/undefined, or an array of nodes.
Common situations: Passing an element handle where a selector or function was intended; calling filter() with no arguments; mixing up the k6/html API with browser-module Element APIs.
Related errors
- the argument to each() must be a function
- the argument to is() must be a function, a selector or a sel
- the argument to map() must be a function
- Error while parsing selector `${selector}`: ${e.message}
- Error while parsing selector `${selector}` - cannot use ${op
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/2c9b977bef89ee87.
Report an issue: GitHub.