Skip to content

expect toMatchObject should show which object properties caused the assertion to fail #6225

Closed

Description

@dandv

When using expect(...).toMatchObject with complex objects, it's hard to see which property/properties caused the assertion to fail.

Steps to Reproduce

import { it } from '@std/testing/bdd';
import { expect } from '@std/expect';

const expected = {
  field1: 'abc',
  field2: 'def',
  foo: 'bar',
}

it('toMatchObject', () => {
  expect({ 
    field1: 'abc',
    field2: 'def',
    foo: 'baz',
  }).toMatchObject(expected);
});
$ deno test denobug.test.ts 
running 1 test from ./denobug.test.ts
toMatchObject ... FAILED (2ms)

 ERRORS 

toMatchObject => https://jsr.io/@std/testing/1.0.5/_test_suite.ts:218:10
error: AssertionError: Expected {
  field1: "abc",
  field2: "def",
  foo: "baz",
} to NOT match {
  field1: "abc",
  field2: "def",
  foo: "bar",
}

Describe the solution you'd like

The library could indicate which property did not match, at least for the top-level properties (in this case, foo).

Describe alternatives you've considered

assertObjectMatch and Jest do this, for all properties, including nested ones:

image

But obviously, assertObjectMatch doesn't support matchers, so there's no easy workaround for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions