Skip to content

[Bug]: objectContaining does not properly match Map values #13968

Closed as not planned
@chasingmaxwell

Description

@chasingmaxwell

Version

29.4.3

Steps to reproduce

  1. Clone my repo at https://github.com/chasingmaxwell/jest-bug-reproduce
  2. Run yarn
  3. Run yarn test
  4. See that some tests fail which should pass while other tests pass which should fail.

Expected behavior

  1. Using objectContaining to assert objects contain a property with a Map value when that Map has different entries than the expectation should fail.
// This should fail, but it passes
expect({ aMap: new Map([[1, 1]]) }).toEqual(
  expect.objectContaining({
    aMap: new Map([[2, 2]]),
  })
);
  1. Using not.objectContaining to assert objects do not contain a property with an identical Map value should pass when the entries are not identical.
// This should pass, but it fails
expect({ aMap: new Map([[1, 1]]) }).toEqual(
  expect.not.objectContaining({
    aMap: new Map([[2, 2]]),
  })
);
  1. Using expect().not.toEqual(expect.objectContaining()) to assert objects do not contain a property with an identical Map value should pass when the entries are not identical.
// This should pass, but it fails
expect({ aMap: new Map([[1, 1]]) }).not.toEqual(
  expect.objectContaining({
    aMap: new Map([[2, 2]]),
  })
);

Actual behavior

  1. Using objectContaining to assert objects contain a property with a Map value when that Map has different entries than the expectation passes.

  2. Using not.objectContaining to assert objects do not contain a property with an identical Map value fails even when the entries are not identical.

  3. Using expect().not.toEqual(expect.objectContaining()) to assert objects do not contain a property with an identical Map value fails even when the entries are not identical.

Additional context

It would seem that when matching against Map values, expect.objectContaining will always pass and expect.not.objectContaining will always fail. This may be related to these issues:

Environment

System:
    OS: macOS 13.0.1
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 16.14.2 - ~/.asdf/installs/nodejs/16.14.2/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/16.14.2/.npm/bin/yarn
    npm: 8.5.0 - ~/.asdf/plugins/nodejs/shims/npm
  npmPackages:
    jest: ^29.4.3 => 29.4.3

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