grafana/k6 · error · TypeError

The "listener" argument must be a function

Error message

The "listener" argument must be a function

What it means

mapPageOn rejects a page.on(...) call whose second argument is not callable: the sobek.Callable received is nil, so the listener passed from JavaScript is missing or not a function. k6 raises a TypeError to the JS runtime mirroring the browser API's behavior.

Solutions

  1. Pass a function as the listener: page.on('console', (msg) => { ... })
  2. Check the event name is one of the supported page events
  3. Make sure the callback isn't accidentally the result of calling the function instead of the function itself
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/browser/page_mapping.go:776 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/eeb6e93b8485ec67. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/browser/page_mapping.go:777

			}
			var mehs []mapping
			for _, eh := range ehs {
				ehm := mapElementHandle(vu, eh)
				mehs = append(mehs, ehm)
			}
			return mehs, nil
		})
	}

	return maps
}

// mapPageOn enables using various page.on event handlers with the page.on method.
// It provides a generic way to map different event types to their respective handler functions.
func mapPageOn(vu moduleVU, p *common.Page) func(common.PageEventName, sobek.Callable) error {
	return func(eventName common.PageEventName, handle sobek.Callable) error {
		if handle == nil {
			panic(vu.Runtime().NewTypeError(`The "listener" argument must be a function`))
		}

		pageEvents := map[common.PageEventName]struct {
			mapp func(vu moduleVU, event common.PageEvent) mapping
			wait bool // Whether to wait for the handler to complete.
		}{
			common.PageEventConsole:         {mapp: mapConsoleMessage},
			common.PageEventMetric:          {mapp: mapMetricEvent, wait: true},
			common.PageEventRequest:         {mapp: mapRequestEvent},
			common.PageEventResponse:        {mapp: mapResponseEvent},
			common.PageEventRequestFinished: {mapp: mapRequestEvent},
			common.PageEventRequestFailed:   {mapp: mapRequestEvent},
		}
		pageEvent, ok := pageEvents[eventName]
		if !ok {
			return fmt.Errorf("unknown page on event: %q", eventName)
		}

View on GitHub (pinned to 8d06114777)