grafana/k6 · error

unknown browser event: %q, must be %q

Error message

unknown browser event: %q, must be %q

What it means

Browser.On (browser.go:773) accepts exactly one event name — "disconnected" (EventBrowserDisconnected). Any other string passed to browser.on(event) fails immediately with 'unknown browser event: %q, must be "disconnected"' before any waiting starts. This is a fail-fast API-misuse error, not an environment problem.

Source

Thrown at internal/js/modules/k6/browser/common/browser.go:773

	browserCtx, err := b.NewContext(opts)
	if err != nil {
		return nil, spanRecordErrorf(span, "new page: %w", err)
	}

	page, err := browserCtx.NewPage()
	if err != nil {
		return nil, spanRecordError(span, err)
	}

	return page, nil
}

// On returns a Promise that is resolved when the browser process is disconnected.
// The only accepted event value is "disconnected".
func (b *Browser) On(event string) (bool, error) {
	if event != EventBrowserDisconnected {
		return false, fmt.Errorf("unknown browser event: %q, must be %q", event, EventBrowserDisconnected)
	}

	select {
	case <-b.browserProc.lostConnection:
		return true, nil
	case <-b.vuCtx.Done():
		return false, fmt.Errorf("browser.on promise rejected: %w", ContextErr(b.vuCtx))
	}
}

// UserAgent returns the controlled browser's user agent string.
func (b *Browser) UserAgent() string {
	return b.version.userAgent
}

// Version returns the controlled browser's version.
func (b *Browser) Version() string {
	product := b.version.product

View on GitHub (pinned to 93accf6570)

Solutions

  1. Use exactly browser.on('disconnected') — the only supported event
  2. Check the event name is a literal 'disconnected' before calling if it comes from a variable/config
  3. Use a linter/constant in shared test helpers so the name cannot drift

Example fix

// before
await browser.on('close') // throws: unknown browser event

// after
await browser.on('disconnected')
Defensive patterns

Strategy: validation

Validate before calling

const eventName = 'disconnected'
if (eventName !== 'disconnected') {
  throw new Error(`unsupported event: ${eventName}`)
}
await browser.on(eventName)

Type guard

function isBrowserEvent(e) {
  return e === 'disconnected'
}

Prevention

When it happens

Trigger: Calling browser.on('close'), browser.on('disconnected ') with whitespace/case differences, or any invented event name; passing a variable that is undefined or an empty string.

Common situations: Porting Playwright code that uses browser.on('disconnected') together with other event names; copy-paste from EventEmitter-style APIs; typos and case errors.

Related errors


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