{"record":{"id":"277de817a1ab6def","repo":"grafana/k6","slug":"unmarshaling-q-to-reducedmotion-w","errorCode":null,"errorMessage":"unmarshaling %q to ReducedMotion: %w","messagePattern":"unmarshaling %q to ReducedMotion: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":117,"sourceCode":"var reducedMotionToID = map[string]ReducedMotion{ //nolint:gochecknoglobals\n\t\"reduce\":        ReducedMotionReduce,\n\t\"no-preference\": ReducedMotionNoPreference,\n}\n\n// MarshalJSON marshals the enum as a quoted JSON string.\nfunc (r ReducedMotion) MarshalJSON() ([]byte, error) {\n\tbuffer := bytes.NewBufferString(`\"`)\n\tbuffer.WriteString(reducedMotionToString[r])\n\tbuffer.WriteString(`\"`)\n\treturn buffer.Bytes(), nil\n}\n\n// UnmarshalJSON unmarshals a quoted JSON string to the enum value.\nfunc (r *ReducedMotion) UnmarshalJSON(b []byte) error {\n\tvar j string\n\terr := json.Unmarshal(b, &j)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unmarshaling %q to ReducedMotion: %w\", b, err)\n\t}\n\t// Note that if the string cannot be found then it will be set to the zero value.\n\t*r = reducedMotionToID[j]\n\treturn nil\n}\n\n// Screen represents a device screen.\ntype Screen struct {\n\tWidth  int64 `js:\"width\"`\n\tHeight int64 `js:\"height\"`\n}\n\n// ColorScheme represents a browser color scheme.\ntype ColorScheme string\n\n// Valid color schemes.\nconst (\n\tColorSchemeLight        ColorScheme = \"light\"","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/page.go#L99-L135","documentation":"ReducedMotion.UnmarshalJSON tried to json.Unmarshal the raw bytes into a Go string and that failed — the JSON value provided for the reducedMotion option is not a string (it is a number, boolean, object, array, or malformed JSON). Note the asymmetry: an unrecognized string silently maps to the zero value ('no-preference'); only non-string JSON produces this error.","triggerScenarios":"Passing reducedMotion as a non-string in browser.newContext/newPage options, e.g. reducedMotion: 1, reducedMotion: true, or reducedMotion: {name: 'reduce'}; also feeding malformed JSON when the option is supplied through JSON config (k6 cloud / --config JSON or environment-variable serialized options).","commonSituations":"Options templating that injects booleans/numbers; env-var config (K6_BROWSER... or --config file) where the value lost its quotes; scripts ported from Playwright that pass enum objects instead of strings.","solutions":["Pass one of the string values 'reduce' or 'no-preference' for reducedMotion","If options come from JSON config, ensure the value is a quoted JSON string","Validate options shape before the run: k6 inspect script.js shows the parsed options"],"exampleFix":"// before\nconst ctx = browser.newContext({ reducedMotion: 1 }); // not a string\n\n// after\nconst ctx = browser.newContext({ reducedMotion: 'reduce' });","handlingStrategy":"type-guard","validationCode":"const rm = opts.reducedMotion;\nif (rm !== undefined && typeof rm !== 'string') { throw new TypeError('reducedMotion must be a string: \"reduce\" | \"no-preference\"'); }","typeGuard":"function isValidReducedMotion(v) {\n  return v === undefined || v === 'reduce' || v === 'no-preference';\n}","tryCatchPattern":null,"preventionTips":["Pass reducedMotion as a string literal: 'reduce' or 'no-preference'","When options come from JSON/env config, validate types with k6 inspect before the run","Watch out: unknown strings silently become 'no-preference' — prefer explicit valid values"],"tags":["browser","options","json","enum","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}