{"record":{"id":"79cc1f6b60587c90","repo":"vitest-dev/vitest","slug":"exact-option-does-not-support-regexp-expected-clas","errorCode":null,"errorMessage":"Exact option does not support RegExp expected class names","messagePattern":"Exact option does not support RegExp expected class names","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser/src/client/tester/expect/toHaveClass.ts","lineNumber":42,"sourceCode":"): MatcherResult {\n  const htmlElement = getElementFromUserInput(actual, toHaveClass, this)\n  const { expectedClassNames, options } = getExpectedClassNamesAndOptions(params)\n\n  const received = splitClassNames(htmlElement.getAttribute('class'))\n  const expected = expectedClassNames.reduce(\n    (acc, className) => {\n      return acc.concat(\n        typeof className === 'string' || !className\n          ? splitClassNames(className)\n          : className,\n      )\n    },\n    [] as (string | RegExp)[],\n  )\n\n  const hasRegExp = expected.some(className => className instanceof RegExp)\n  if (options.exact && hasRegExp) {\n    throw new Error('Exact option does not support RegExp expected class names')\n  }\n\n  if (options.exact) {\n    return {\n      pass: isSubset(expected, received) && expected.length === received.length,\n      message: () => {\n        const to = this.isNot ? 'not to' : 'to'\n        return getMessage(\n          this,\n          this.utils.matcherHint(\n            `${this.isNot ? '.not' : ''}.toHaveClass`,\n            'element',\n            this.utils.printExpected(expected.join(' ')),\n          ),\n          `Expected the element ${to} have EXACTLY defined classes`,\n          expected.join(' '),\n          'Received',\n          received.join(' '),","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/browser/src/client/tester/expect/toHaveClass.ts#L24-L60","documentation":"The .toHaveClass() matcher supports an `exact: true` option for ordered/equal comparison, but exact matching is fundamentally incompatible with RegExp class-name expectations (a regex cannot express a precise equality set). Vitest throws when both options.exact and at least one RegExp expected value are present, instead of producing a misleading pass/fail.","triggerScenarios":"Calling expect(el).toHaveClass([/btn-/], { exact: true }); combining exact: true with a RegExp in either the array or single-argument form.","commonSituations":"Copying a regex-based assertion and adding exact: true for stricter checks; migrating from CSS-class strings to regex without removing the exact flag.","solutions":["Remove exact: true when using RegExp class-name expectations.","If you need exact matching, supply literal class-name strings instead of regexes.","Split into two assertions: a regex presence check (no exact) and a string-based exact check."],"exampleFix":"// before\nexpect(el).toHaveClass([/btn-/, 'active'], { exact: true })\n// after\nexpect(el).toHaveClass([/btn-/, 'active'])  // exact removed\n// or with literal strings:\nexpect(el).toHaveClass(['btn-primary', 'active'], { exact: true })","handlingStrategy":"validation","validationCode":"const hasRegex = Array.isArray(expected)\n  ? expected.some(c => c instanceof RegExp)\n  : expected instanceof RegExp\nif (options?.exact && hasRegex) {\n  // drop exact, or convert regexes to literal strings\n}\nexpect(el).toHaveClass(expected, options)","typeGuard":"function isRegExp(v: unknown): v is RegExp {\n  return v instanceof RegExp\n}","tryCatchPattern":null,"preventionTips":["Never combine { exact: true } with RegExp class-name expectations.","Use literal class strings when you need exact matching.","Document the exact/regex incompatibility in shared matcher helpers."],"tags":["browser","expect","matcher","class","options"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}