{"id":"2dad6ff80374166e","repo":"vitest-dev/vitest","slug":"input-with-type-checkbox-or-type-radio-cannot-be-u","errorCode":null,"errorMessage":"input with type=checkbox or type=radio cannot be used with .toHaveValue(). Use .toBeChecked() for type=checkbox or .toHaveFormValues() instead","messagePattern":"input with type=checkbox or type=radio cannot be used with \\.toHaveValue\\(\\)\\. Use \\.toBeChecked\\(\\) for type=checkbox or \\.toHaveFormValues\\(\\) instead","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser/src/client/tester/expect/toHaveValue.ts","lineNumber":31,"sourceCode":" * copies or substantial portions of the Software.\n */\n\nimport type { MatcherResult, MatcherState } from 'vitest'\nimport type { Locator } from '../locators'\nimport { arrayAsSetComparison, getElementFromUserInput, getMessage, getSingleElementValue, isInputElement } from './utils'\n\nexport default function toHaveValue(\n  this: MatcherState,\n  actual: Element | Locator,\n  expectedValue?: string,\n): MatcherResult {\n  const htmlElement = getElementFromUserInput(actual, toHaveValue, this)\n\n  if (\n    isInputElement(htmlElement)\n    && ['checkbox', 'radio'].includes(htmlElement.type)\n  ) {\n    throw new Error(\n      'input with type=checkbox or type=radio cannot be used with .toHaveValue(). Use .toBeChecked() for type=checkbox or .toHaveFormValues() instead',\n    )\n  }\n\n  const receivedValue = getSingleElementValue(htmlElement)\n  const expectsValue = expectedValue !== undefined\n\n  let expectedTypedValue = expectedValue\n  let receivedTypedValue = receivedValue\n  // eslint-disable-next-line eqeqeq\n  if (expectedValue == receivedValue && expectedValue !== receivedValue) {\n    expectedTypedValue = `${expectedValue} (${typeof expectedValue})`\n    receivedTypedValue = `${receivedValue} (${typeof receivedValue})`\n  }\n\n  return {\n    pass: expectsValue\n      ? this.equals(receivedValue, expectedValue, [arrayAsSetComparison, ...this.customTesters])","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/browser/src/client/tester/expect/toHaveValue.ts#L13-L49","documentation":"Thrown by `toHaveValue` at toHaveValue.ts:27-33 when the received element is an `<input>` whose `type` is `checkbox` or `radio`. Checkboxes/radios don't have a single meaningful `.value`; the matcher would conflate the unchecked state with the value attribute, so Vitest refuses and points users to the correct matchers.","triggerScenarios":"`expect(page.getByRole('checkbox')).toHaveValue(true)`, `expect(checkboxInput).toHaveValue('on')`, or `expect(radioInput).toHaveValue('yes')`. Any `toHaveValue` call against an `<input type=\"checkbox\">` or `<input type=\"radio\">`.","commonSituations":"Treating a checkbox like a text field. Migrating assertions from a custom helper that used `.value` directly. Confusing the `value` attribute (always present on inputs) with the checked state.","solutions":["For checkboxes use `toBeChecked()`/`toBeUnchecked()`.","For forms containing checkboxes/radios use `toHaveFormValues({ groupName: expectedValueOrArray })`.","If you really need the `value` attribute string, assert on the attribute: `expect(el).toHaveAttribute('value', 'on')`."],"exampleFix":"// before\nexpect(page.getByRole('checkbox')).toHaveValue('on')\n\n// after\nexpect(page.getByRole('checkbox')).toBeChecked()\n// or, for the whole form:\nexpect(form).toHaveFormValues({ accept: true })","handlingStrategy":"validation","validationCode":"function isCheckable(input: Element): boolean {\n  return input instanceof HTMLInputElement && (input.type === 'checkbox' || input.type === 'radio')\n}\nif (isCheckable(el)) throw new Error('use toBeChecked()/toHaveFormValues() for checkable inputs')","typeGuard":"function isCheckableInput(el: Element): el is HTMLInputElement {\n  return el instanceof HTMLInputElement && (el.type === 'checkbox' || el.type === 'radio')\n}","tryCatchPattern":null,"preventionTips":["Use toBeChecked()/toBeUnchecked() for checkboxes and radios.","Use toHaveFormValues() to assert checkable groups by name.","Reserve toHaveValue() for text, number, select, and similar single-value inputs."],"tags":["browser","expect-matcher","tohavevalue","dom","input-type","api-misuse"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}