grafana/k6 · error

parsing locator dispatch event options: %w

Error message

parsing locator dispatch event options: %w

What it means

Thrown when locator.dispatchEvent(type, eventInit, opts) fails to parse the third argument into FrameDispatchEventOptions (embeds FrameBaseOptions: strict, timeout number ms). The mapping wraps the cause as 'parsing locator dispatch event options: %w'. The eventInit payload is exported separately and is not part of this parse; only the trailing opts object is.

Source

Thrown at internal/js/modules/k6/browser/browser/locator_mapping.go:418

				return nil, fmt.Errorf("parsing hover options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, lo.Hover(copts) //nolint:wrapcheck
			}), nil
		},
		"tap": func(opts sobek.Value) (*sobek.Promise, error) {
			copts := common.NewFrameTapOptions(lo.DefaultTimeout())
			if err := copts.Parse(vu.Context(), opts); err != nil {
				return nil, fmt.Errorf("parsing locator tap options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, lo.Tap(copts) //nolint:wrapcheck
			}), nil
		},
		"dispatchEvent": func(typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) {
			popts := common.NewFrameDispatchEventOptions(lo.DefaultTimeout())
			if err := popts.Parse(vu.Context(), opts); err != nil {
				return nil, fmt.Errorf("parsing locator dispatch event options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, lo.DispatchEvent(typ, exportArg(eventInit), popts) //nolint:wrapcheck
			}), nil
		},
		"waitFor": func(opts sobek.Value) (*sobek.Promise, error) {
			copts := common.NewFrameWaitForSelectorOptions(lo.Timeout())
			if err := copts.Parse(vu.Context(), opts); err != nil {
				return nil, fmt.Errorf("parsing wait for options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, lo.WaitFor(copts) //nolint:wrapcheck
			}), nil
		},
	}
}

func parseLocatorOptions(rt *sobek.Runtime, opts sobek.Value) *common.LocatorOptions {

View on GitHub (pinned to 93accf6570)

Solutions

  1. Keep three arguments in order: dispatchEvent('click', { button: 0 }, { timeout: 5000 })
  2. Omit the third argument when defaults suffice
  3. Keep only strict/timeout keys in the options object
  4. Use numeric milliseconds for timeout

Example fix

// before
await page.locator('#el').dispatchEvent('click', { button: 0 }, '5s');

// after
await page.locator('#el').dispatchEvent('click', { button: 0 }, { timeout: 5000 });
Defensive patterns

Strategy: validation

Validate before calling

function assertDispatchEventOpts(opts) {
  if (opts === null || opts === undefined) return;
  if (typeof opts !== 'object' || Array.isArray(opts)) {
    throw new TypeError('dispatchEvent options must be a plain object (third argument)');
  }
  if ('timeout' in opts && typeof opts.timeout !== 'number') {
    throw new TypeError('dispatchEvent timeout must be a number (ms)');
  }
}

Type guard

function isLocatorBaseOptions(v) {
  if (v === null || v === undefined) return true;
  if (typeof v !== 'object' || Array.isArray(v)) return false;
  return (v.timeout === undefined || typeof v.timeout === 'number') &&
         (v.strict === undefined || typeof v.strict === 'boolean');
}

Try / catch

try {
  await locator.dispatchEvent('click', eventInit, opts);
} catch (e) {
  if (/parsing locator dispatch event options/.test(String(e.message))) {
    throw new Error(`Bad dispatchEvent options (third argument): ${JSON.stringify(opts)}`);
  }
  throw e;
}

Prevention

When it happens

Trigger: dispatchEvent('click', { button: 0 }, '5s') — a scalar in the third (opts) slot; passing eventInit fields mixed into the options object; wrong-typed strict/timeout under strict parsers.

Common situations: Three-argument calls where the trailing duration string is easy to misplace; building synthetic events in tests and passing the payload as the last argument; ported Playwright snippets with different arity conventions.

Related errors


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