Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dandv opened this issue Dec 2, 2024 · 0 comments
Labels
enhancement New feature or request expect

Comments

@dandv
Copy link
Contributor

dandv commented Dec 2, 2024

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 lease 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.

@kt3k kt3k added expect enhancement New feature or request labels Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request expect
Projects
None yet
Development

No branches or pull requests

2 participants