jestjs/jest · error · Error

Expected is not a String or a RegExp

Error message

Expected is not a String or a RegExp

What it means

stringMatching / stringNotMatching accept either a string (treated as a regex source) or a RegExp. The constructor throws an Error if the sample is neither — e.g. a number, object, or undefined. Because the constructor wraps the sample in `new RegExp(sample)`, an invalid type would produce a confusing RegExp error, so this guard rejects it early with a clear message.

Solutions

  1. Pass a RegExp or a string regex source: `expect.stringMatching(/^foo/)` or `expect.stringMatching('^foo')`.
  2. If you meant a literal substring, use `expect.stringContaining('foo')` instead.
  3. Coerce unknown inputs to a string before passing, if a string pattern is acceptable.

Example fix

// before
expect(msg).toEqual(expect.stringMatching(123)); // number, not String/RegExp

// after
expect(msg).toEqual(expect.stringMatching(/^\d+$/));
// or substring:
expect(msg).toEqual(expect.stringContaining('123'));
Defensive patterns

Strategy: type-guard

Validate before calling

function stringMatchingSafe(sample) {
  if (typeof sample !== 'string' && !(sample instanceof RegExp)) {
    throw new TypeError('stringMatching requires a string or RegExp');
  }
  return expect.stringMatching(sample);
}

Type guard

function isStringOrRegExp(v: unknown): v is string | RegExp {
  return typeof v === 'string' || v instanceof RegExp;
}

const m = isStringOrRegExp(sample) ? expect.stringMatching(sample) : expect.anything();

Prevention

When it happens

Trigger: Calling `expect.stringMatching(value)` where value is not a string and not a RegExp (e.g. a number, null, object, or a Date). Thrown at construction time.

Common situations: Passing a non-string primitive; forgetting to quote a literal pattern; passing an object whose toString is not the intended pattern; mixing up stringMatching (regex) with stringContaining (substring).

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/fcaef83c3d55b7f8. Report an issue: GitHub.

Appendix: source

Thrown at packages/expect/src/asymmetricMatchers.ts:326

  asymmetricMatch(other: unknown) {
    const result = isA<string>('String', other) && other.includes(this.sample);

    return this.inverse ? !result : result;
  }

  toString() {
    return `String${this.inverse ? 'Not' : ''}Containing`;
  }

  override getExpectedType() {
    return 'string';
  }
}

class StringMatching extends AsymmetricMatcher<RegExp> {
  constructor(sample: string | RegExp, inverse = false) {
    if (!isA('String', sample) && !isA('RegExp', sample)) {
      throw new Error('Expected is not a String or a RegExp');
    }
    super(new RegExp(sample), inverse);
  }

  asymmetricMatch(other: unknown) {
    const result = isA<string>('String', other) && this.sample.test(other);

    return this.inverse ? !result : result;
  }

  toString() {
    return `String${this.inverse ? 'Not' : ''}Matching`;
  }

  override getExpectedType() {
    return 'string';
  }
}

View on GitHub (pinned to 8e6d128e4a)