grafana/k6 · error

parsing emulateMedia options

Error message

parsing emulateMedia options: %w

What it means

Thrown synchronously by page.emulateMedia(opts) when parsePageEmulateMediaOptions fails. That helper (internal/js/modules/k6/browser/browser/page_mapping.go:1131-1154, marked nolint:unparam) always returns a nil error in the current code: it copies colorScheme, media and reducedMotion as strings without validation. The wrapper is therefore defensive, and invalid enum strings surface later from CDP/browser-side validation, not from this message.

Solutions

  1. Use the documented values: media: 'screen'|'print'|'null', colorScheme: 'light'|'dark'|'no-preference', reducedMotion: 'reduce'|'no-preference'
  2. If the error does appear, check your k6/xk6 build for a patched parsePageEmulateMediaOptions and match its accepted values
  3. Pass null to reset emulation: page.emulateMedia({ media: null })

Example fix

// before
await page.emulateMedia({ media: 'prints' });

// after
await page.emulateMedia({ media: 'print' });
Defensive patterns

Strategy: validation

Validate before calling

const MEDIA = new Set(['screen', 'print', 'null', null]);
const SCHEME = new Set(['light', 'dark', 'no-preference', 'null', null]);
const MOTION = new Set(['reduce', 'no-preference', 'null', null]);
if (opts?.media !== undefined && !MEDIA.has(opts.media)) throw new Error(`bad media: ${opts.media}`);
if (opts?.colorScheme !== undefined && !SCHEME.has(opts.colorScheme)) throw new Error(`bad colorScheme: ${opts.colorScheme}`);
if (opts?.reducedMotion !== undefined && !MOTION.has(opts.reducedMotion)) throw new Error(`bad reducedMotion: ${opts.reducedMotion}`);
await page.emulateMedia(opts);

Type guard

/** @param {unknown} o */
function isEmulateMediaOpts(o) {
  if (o === undefined || o === null) return true;
  if (typeof o !== 'object') return false;
  const m = /** @type {any} */ (o);
  return (m.media === undefined || m.media === null || typeof m.media === 'string')
    && (m.colorScheme === undefined || typeof m.colorScheme === 'string')
    && (m.reducedMotion === undefined || typeof m.reducedMotion === 'string');
}

Try / catch

try {
  await page.emulateMedia(opts);
} catch (e) {
  if (String(e).includes('parsing emulateMedia options')) {
    // only on non-stock builds; strip suspicious keys and retry
    const { media = 'screen' } = opts ?? {};
    await page.emulateMedia({ media });
  } else throw e;
}

Prevention

When it happens

Trigger: No input reaches this error in this version. Invalid values such as media: 'printx' are accepted here and fail later inside p.EmulateMedia when the value is validated/sent to the browser.

Common situations: Typos in media ('print' vs 'prints'), colorScheme ('dark'/'light'/'no-preference'), or reducedMotion ('reduce'/'no-preference') usually produce a different downstream error; seeing this exact message implies a modified k6 build with added validation.

Related errors


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

Appendix: source

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

			}
			return promise(vu, func() (any, error) {
				return nil, p.Dblclick(selector, popts) //nolint:wrapcheck
			}), nil
		},
		"dispatchEvent": func(selector, typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) {
			popts, err := parseFrameDispatchEventOptions(rt, opts, p.MainFrame().Timeout())
			if err != nil {
				return nil, fmt.Errorf("parsing page dispatch event options: %w", err)
			}
			earg := exportArg(eventInit)
			return promise(vu, func() (any, error) {
				return nil, p.DispatchEvent(selector, typ, earg, popts) //nolint:wrapcheck
			}), nil
		},
		"emulateMedia": func(opts sobek.Value) (*sobek.Promise, error) {
			popts, err := parsePageEmulateMediaOptions(rt, opts, common.NewPageEmulateMediaOptions(p))
			if err != nil {
				return nil, fmt.Errorf("parsing emulateMedia options: %w", err)
			}
			return promise(vu, func() (any, error) {
				return nil, p.EmulateMedia(popts) //nolint:wrapcheck
			}), nil
		},
		"emulateVisionDeficiency": func(typ string) *sobek.Promise {
			return promise(vu, func() (any, error) {
				return nil, p.EmulateVisionDeficiency(typ) //nolint:wrapcheck
			})
		},
		"evaluate": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) {
			if sobekEmptyString(pageFunc) {
				return nil, fmt.Errorf("evaluate requires a page function")
			}
			funcString := pageFunc.String()
			gopts := exportArgs(gargs)
			return promise(vu, func() (any, error) {
				return p.Evaluate(funcString, gopts...)

View on GitHub (pinned to 8d06114777)