{"record":{"id":"415a72f625cb13ea","repo":"vitest-dev/vitest","slug":"expected-is-not-a-string-or-a-regexp","errorCode":null,"errorMessage":"Expected is not a String or a RegExp","messagePattern":"Expected is not a String or a RegExp","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/jest-asymmetric-matchers.ts","lineNumber":320,"sourceCode":"      return 'object'\n    }\n\n    if (this.sample === Boolean) {\n      return 'boolean'\n    }\n\n    return this.fnNameFor(this.sample)\n  }\n\n  toAsymmetricMatcher() {\n    return `Any<${this.fnNameFor(this.sample)}>`\n  }\n}\n\nexport class StringMatching extends AsymmetricMatcher<RegExp> {\n  constructor(sample: string | RegExp, inverse = false) {\n    if (!isA('String', sample) && !isA('RegExp', sample)) {\n      throw new Error('Expected is not a String or a RegExp')\n    }\n\n    super(new RegExp(sample), inverse)\n  }\n\n  asymmetricMatch(other: string): boolean {\n    const result = isA('String', other) && this.sample.test(other)\n\n    return this.inverse ? !result : result\n  }\n\n  toString() {\n    return `String${this.inverse ? 'Not' : ''}Matching`\n  }\n\n  getExpectedType() {\n    return 'string'\n  }","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/expect/src/jest-asymmetric-matchers.ts#L302-L338","documentation":"StringMatching (expect.stringMatching) requires the sample to be a String or a RegExp; anything else throws at construction. The sample is then compiled into a RegExp via new RegExp(sample) for matching.","triggerScenarios":"expect.stringMatching(sample) with a number, object, null, etc.","commonSituations":"Passing a number expecting numeric pattern semantics; passing a compiled regex from another lib that is not a native RegExp; passing an object with a custom toString.","solutions":["Pass a RegExp literal: expect.stringMatching(/^user-/).","Pass a string pattern: expect.stringMatching('user-').","If you have a custom matcher object, convert it to a RegExp first."],"exampleFix":"// before\nexpect(name).toEqual(expect.stringMatching(123))\n// after\nexpect(name).toEqual(expect.stringMatching(/^user-\\d+$/))","handlingStrategy":"type-guard","validationCode":"function asStringMatching(sample) {\n  if (typeof sample !== 'string' && !(sample instanceof RegExp)) {\n    throw new TypeError('expect.stringMatching needs a string or RegExp')\n  }\n  return expect.stringMatching(sample)\n}","typeGuard":"function isStringOrRegExp(v): v is string | RegExp {\n  return typeof v === 'string' || v instanceof RegExp\n}","tryCatchPattern":null,"preventionTips":["Pass RegExp literals or string patterns to expect.stringMatching.","Convert foreign-library matcher objects to RegExp before use.","Distinguish from expect.stringContaining (substring, not pattern)."],"tags":["expect","asymmetric-matcher","type-error"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}