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

[Bug]: matcher error is not shown when the expected object has throwing getter #15280

Open
rluvaton opened this issue Aug 26, 2024 · 0 comments

Comments

@rluvaton
Copy link

rluvaton commented Aug 26, 2024

Version

29.7.0

Steps to reproduce

Repro - CodeSandbox

Run this:

const obj = {
  get deprecatedFunction() {
    throw new Error('custom')
  }
}

test('Playground', () => {
  expect(obj).toEqual({
    a: expect.objectContaining({})
  })
});

Expected behavior

The matcher error should be thrown:

expect(received).toEqual(expected) // deep equality

- Expected  - 0
+ Received  + 1

  Object {
    "a": ObjectContaining {},
+   "deprecatedFunction": undefined,
  }

   5 | }
   7 | test('Playground', () => {
>  8 |   expect(obj).toEqual({
     |               ^
   9 |     a: expect.objectContaining({})
  10 |   })
  11 | });

Actual behavior

The error custom was thrown making it impossible to know what was wrong

● › Playground
custom

   5 | }
   7 | test('Playground', () => {
>  8 |   expect(obj).toEqual({
     |               ^
   9 |     a: expect.objectContaining({})
  10 |   })
  11 | });

Additional context

I'm writing custom expect matchers that add assertions for HTTP response objects, and the got library has:

    get searchParameters() {
        throw new Error('The `searchParameters` option does not exist. Use `searchParams` instead.');
    }

in the response and I can't test my matcher error output message

This happens here:

// this accesses the value or getter, depending. We just care about the value anyways, and this allows us to not mess with accessors
// it has the side effect of invoking the getter here though, rather than copying it over
(object as Record<string | symbol, unknown>)[key],

I don't think that stringify should throw

Environment

System:
    OS: macOS 14.6.1
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 22.3.0 - ...
    npm: 8.19.4 - ...
@rluvaton rluvaton changed the title [Bug]: [Bug]: object with throwing getter cant be run expect on Aug 26, 2024
@rluvaton rluvaton changed the title [Bug]: object with throwing getter cant be run expect on [Bug]: matcher error is not shown when the expected object has throwing getter Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
@rluvaton and others