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

Closed
rluvaton opened this issue Aug 26, 2024 · 4 comments
Closed

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
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Sep 25, 2024
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2024
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants
@rluvaton and others