jestjs/jest · error · Error

${RECEIVED_COLOR('received')} value must not be null nor und

Error message

${RECEIVED_COLOR('received')} value must not be null nor undefined

What it means

Thrown by `toContain` (matchers.ts:474) when the received value passed to `expect(...)` is null or undefined. `toContain` works on strings and indexable iterables (arrays, sets, NodeLists); it must spread/index the value (`[...received]`), which fails on null/undefined. The guard uses loose equality (`received == null`) to catch both.

Source

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

      // eslint-disable-next-line prefer-template
      matcherHint(matcherName, undefined, '', options) +
      '\n\n' +
      `Received: ${printReceived(received)}`;

    return {message, pass};
  },

  toContain(received: ContainIterable | string, expected: unknown) {
    const matcherName = 'toContain';
    const isNot = this.isNot;
    const options: MatcherHintOptions = {
      comment: 'indexOf',
      isNot,
      promise: this.promise,
    };

    if (received == null) {
      throw new Error(
        matcherErrorMessage(
          matcherHint(matcherName, undefined, undefined, options),
          `${RECEIVED_COLOR('received')} value must not be null nor undefined`,
          printWithType('Received', received, printReceived),
        ),
      );
    }

    if (typeof received === 'string') {
      const wrongTypeErrorMessage = `${EXPECTED_COLOR(
        'expected',
      )} value must be a string if ${RECEIVED_COLOR(
        'received',
      )} value is a string`;

      if (typeof expected !== 'string') {
        throw new TypeError(
          matcherErrorMessage(

View on GitHub (pinned to 8e6d128e4a)

Solutions

  1. Guard the value is non-null before the matcher, or assert it first: expect(value).toBeDefined().
  2. Fix the source so it returns an empty array/string instead of null/undefined for 'no results'.
  3. Use optional chaining when extracting: const value = obj?.items ?? [].
  4. If null is legitimately possible, branch the test to assert the null case separately instead of calling toContain.

Example fix

// before
expect(findUser(id)?.roles).toContain('admin'); // findUser returns null

// after
const user = findUser(id);
expect(user).toBeDefined();
expect(user.roles).toContain('admin');
// or default to empty
expect(user?.roles ?? []).toContain('admin');
Defensive patterns

Strategy: validation

Validate before calling

const value = collection;
if (value == null) {
  throw new Error('cannot call toContain on null/undefined; check the source');
}
expect(value).toContain(item);

Type guard

const isIndexable = (v: unknown): v is string | Array<unknown> | Set<unknown> =>
  v != null && (typeof v === 'string' || Array.isArray(v) || v instanceof Set || Symbol.iterator in Object(v));

Prevention

When it happens

Trigger: Calling `expect(value).toContain(item)` where `value` is null or undefined. Typically the value comes from a property access, an array method (find returning undefined), an optional that was never set, or an API call that returned null for a missing resource.

Common situations: Querying the DOM (.querySelector) returned null; an Array.find returned undefined and you then assert on it; a JSON field absent in the response; a spy return value not captured; refactoring changed a function to possibly return undefined.

Related errors


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