jestjs/jest · error · TypeError

received value must be a string

Error message

received value must be a string

What it means

Thrown by `toMatch` (matchers.ts:816) when the value passed to `expect(...)` is not a string. `toMatch` runs `received.includes(expected)` or `new RegExp(expected).test(received)`, both of which require a string received; a non-string would either crash or silently misbehave. Jest throws a `TypeError` with a `printWithType` hint before any matching runs.

Source

Thrown at packages/expect/src/matchers.ts:824

                  : receivedPath.join('.'),
              )}\n\n${
                hasValue
                  ? `Expected value: ${printExpected(expectedValue)}\n`
                  : ''
              }Received value: ${printReceived(receivedValue)}`);

    return {message, pass};
  },

  toMatch(received: string, expected: string | RegExp) {
    const matcherName = 'toMatch';
    const options: MatcherHintOptions = {
      isNot: this.isNot,
      promise: this.promise,
    };

    if (typeof received !== 'string') {
      throw new TypeError(
        matcherErrorMessage(
          matcherHint(matcherName, undefined, undefined, options),
          `${RECEIVED_COLOR('received')} value must be a string`,
          printWithType('Received', received, printReceived),
        ),
      );
    }

    if (
      !(typeof expected === 'string') &&
      !(expected && typeof expected.test === 'function')
    ) {
      throw new Error(
        matcherErrorMessage(
          matcherHint(matcherName, undefined, undefined, options),
          `${EXPECTED_COLOR(
            'expected',
          )} value must be a string or regular expression`,

View on GitHub (pinned to f49721c78e)

Solutions

  1. If checking equality on a non-string, switch to `toBe` or `toEqual`.
  2. If matching against serialized output, stringify first: `expect(JSON.stringify(obj)).toMatch(/pattern/)`.
  3. For Buffers/typed arrays, convert: `expect(buf.toString('utf8')).toMatch(...)`.
  4. Log `typeof received` to confirm it is a string before the assertion.

Example fix

// before
expect(response).toMatch(/success/i); // response is an object

// after
expect(JSON.stringify(response)).toMatch(/success/i);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof received !== 'string') {
  throw new Error(`received must be a string, got ${typeof received}`);
}
expect(received).toMatch(pattern);

Type guard

const isString = (v: unknown): v is string => typeof v === 'string';

Try / catch

try {
  expect(received).toMatch(pattern);
} catch (e) {
  if (e instanceof TypeError && /received value must be a string/.test(e.message)) {
    console.error('received was', typeof received, received);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling `expect(value).toMatch(/pattern/)` where `value` is a number, object, undefined, null, or an array. Common with `expect(statusCode).toMatch(200)` (use `toBe`), `expect(response.body).toMatch(/ok/)` where body is an object (stringify first), or `expect(Date.now()).toMatch(...)`.

Common situations: Confusing `toMatch` (string/regex) with equality matchers; asserting on a non-serialized object; receiving a Buffer or Uint8Array instead of a string; refactoring that changed a return type from string to object.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/3214870845a46d6d.json. Report an issue: GitHub.