Skip to content

[Bug]: objectContaining matches non-object values #15420

@flovouin

Description

@flovouin

Version

29.7.0

Steps to reproduce

expect(undefined).toEqual(expect.objectContaining({}));
expect('test').toEqual(expect.objectContaining({}));
expect(1).toEqual(expect.objectContaining({}));
// Expecting the array to contain an object.
expect([]).toEqual([expect.objectContaining({})]);

Expected behavior

All tests above should fail.

Actual behavior

All tests pass.

Additional context

The documentation states that:

expect.objectContaining(object) matches any received object that recursively matches the expected properties. That is, the expected object is a subset of the received object.

However objectContaining can match any type, not just objects. This leads to all sorts of tests unexpectedly passing.

Environment

System:
    OS: macOS 15.2
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 22.11.0 - ~/.asdf/installs/nodejs/22.11.0/bin/node
    npm: 10.9.0 - ~/.asdf/plugins/nodejs/shims/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions