grafana/k6 · error
the argument to is() must be a function, a selector or a sel
Error message
the argument to is() must be a function, a selector or a selection
What it means
Selection.Is() in k6's HTML module checks whether the current selection matches a CSS selector, another Selection, or a predicate function, and throws when the argument is none of these. The type switch handles string and Selection; the default branch asserts a function via sobek.AssertFunction before calling goquery's IsFunction.
Source
Thrown at js/modules/k6/html/html.go:410
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"))
}
return s.sel.IsFunction(s.buildMatcher(v, sobekFn))
}
}
// Map implements ES5 Array.prototype.map
func (s Selection) Map(v sobek.Value) []sobek.Value {
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to map() must be a function"))
}
var values []sobek.Value
s.sel.Each(func(idx int, sel *goquery.Selection) {
selection := &Selection{sel: sel, URL: s.URL, rt: s.rt}
if fnRes, fnErr := sobekFn(v, s.rt.ToValue(idx), s.rt.ToValue(selection)); fnErr == nil {View on GitHub (pinned to 93accf6570)
Solutions
- Pass a selector: sel.is('.visible')
- Or a Selection to test overlap: sel.is(otherSel)
- Or a function: sel.is((idx, el) => el.attr('aria-hidden') !== 'true')
Example fix
// before
if (selection.is(element)) { ... }
// after
if (selection.is((idx, el) => el.id() === element.id())) { ... } Defensive patterns
Strategy: type-guard
Type guard
const isIsArg = (v) => typeof v === 'string' || typeof v === 'function' ||
(v !== null && typeof v === 'object' && typeof v.sel !== 'undefined');
if (!isIsArg(arg)) throw new TypeError('is() expects selector, Selection, or function');
sel.is(arg); Prevention
- Use selector strings for class/tag checks with is()
- Compare elements via id/attribute inside a predicate function
- Guard dynamic arguments with a typeof check
When it happens
Trigger: Calling is() with a DOM Element, null, undefined, a boolean, or any other non-string/non-Selection/non-function value.
Common situations: Comparing a selection against an element variable; forgetting the argument in a quick REPL test; chaining is() after a call that already returned a boolean.
Related errors
- the argument to each() must be a function
- the argument to filter() must be a function, a selector or a
- 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/caa8282ada554dfe.
Report an issue: GitHub.