{"record":{"id":"da845032092b26a8","repo":"microsoft/playwright","slug":"expected-argument-in-tocontainclass-cannot-be-a","errorCode":null,"errorMessage":"\"expected\" argument in toContainClass cannot be a RegExp value","messagePattern":"\"expected\" argument in toContainClass cannot be a RegExp value","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright/src/matchers/matchers.ts","lineNumber":322,"sourceCode":"  }\n}\n\nexport function toContainClass(\n  this: ExpectMatcherStateInternal,\n  locator: LocatorEx,\n  expected: string | string[],\n  options?: { timeout?: number, signal?: AbortSignal },\n) {\n  if (Array.isArray(expected)) {\n    if (expected.some(e => isRegExp(e)))\n      throw new Error(`\"expected\" argument in toContainClass cannot contain RegExp values`);\n    return toEqual.call(this, 'toContainClass', locator, 'Locator', async (isNot, timeout, signal) => {\n      const expectedText = serializeExpectedTextValues(expected);\n      return await locator._expect('to.contain.class.array', { expectedText, isNot, timeout, signal, title: this.title });\n    }, expected, options);\n  } else {\n    if (isRegExp(expected))\n      throw new Error(`\"expected\" argument in toContainClass cannot be a RegExp value`);\n    return toMatchText.call(this, 'toContainClass', locator, 'Locator', async (isNot, timeout, signal) => {\n      const expectedText = serializeExpectedTextValues([expected]);\n      return await locator._expect('to.contain.class', { expectedText, isNot, timeout, signal, title: this.title });\n    }, expected, options);\n  }\n}\n\nexport function toHaveCount(\n  this: ExpectMatcherStateInternal,\n  locator: LocatorEx,\n  expected: number,\n  options?: { timeout?: number, signal?: AbortSignal },\n) {\n  return toEqual.call(this, 'toHaveCount', locator, 'Locator', async (isNot, timeout, signal) => {\n    return await locator._expect('to.have.count', { expectedNumber: expected, isNot, timeout, signal, title: this.title });\n  }, expected, options);\n}\n","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/microsoft/playwright/blob/deda92d15e7d771aacaae47eb2b6e47a562c30ff/packages/playwright/src/matchers/matchers.ts#L304-L340","documentation":"The single-value form of expect(locator).toContainClass() rejects a RegExp `expected` (matchers.ts:320-322). The matcher sends 'to.contain.class' with the serialized expected text and checks that the class attribute's token list contains the exact string; a regex has no exact-string meaning there, so Playwright throws immediately rather than running a doomed assertion.","triggerScenarios":"Calling `await expect(locator).toContainClass(/btn-/)` where `isRegExp(expected)` is true. The TS signature (`expected: string | string[]`) prevents it in typed code, so it typically appears in plain-JS specs or when `expected` arrives from untyped data and bypasses type checking.","commonSituations":"Copy-pasting from toHaveText/toHaveAttribute examples that do allow regexes; trying to match generated class names (CSS modules, Tailwind hashes) with a pattern; passing fixture-driven values whose type is not controlled.","solutions":["Use the exact class name: `await expect(locator).toContainClass('btn-primary')`, or an array for several tokens.","For pattern matching, use a matcher with regex support against the attribute: `await expect(locator).toHaveAttribute('class', /btn-/)`.","Add a runtime type guard on dynamic expected values before asserting (see typeGuard)."],"exampleFix":"// before\nawait expect(page.getByTestId('icon')).toContainClass(/^-icon-/);\n// Error: \"expected\" argument in toContainClass cannot be a RegExp value\n\n// after\nawait expect(page.getByTestId('icon')).toContainClass('my-icon');\n// or:\nawait expect(page.getByTestId('icon')).toHaveAttribute('class', /-icon-/);","handlingStrategy":"type-guard","validationCode":"const expected: unknown = getExpectedFromFixture();\nif (expected instanceof RegExp)\n  throw new Error('toContainClass needs an exact class name; use toHaveAttribute for regex matching');\nawait expect(locator).toContainClass(expected as string);","typeGuard":"function isExactClassNames(value: unknown): value is string | string[] {\n  if (typeof value === 'string') return true;\n  return Array.isArray(value) && value.every(item => typeof item === 'string');\n}\n\n// usage\nif (!isExactClassNames(expected)) throw new Error(`Invalid class expectation: ${String(expected)}`);\nawait expect(locator).toContainClass(expected);","tryCatchPattern":null,"preventionTips":["Treat toContainClass as an exact-token membership check; only toHaveText/toHaveAttribute accept regexes.","Validate fixture- or user-supplied class values are strings before asserting.","For hashed/generated class names, match the whole class attribute with a regex via toHaveAttribute instead."],"tags":["assertions","expect","matchers","locator","regex","typescript"],"backgroundTag":"assertion-argument-type-mismatch","analyzedSha":"deda92d15e7d771aacaae47eb2b6e47a562c30ff","analyzedAt":"2026-08-21T16:56:32.694Z","contentChangedAt":"2026-08-21T16:56:32.694Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}