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]: toMatchObject output does not show class getter value #13397

Closed
Rochet2 opened this issue Oct 6, 2022 · 5 comments
Closed

[Bug]: toMatchObject output does not show class getter value #13397

Rochet2 opened this issue Oct 6, 2022 · 5 comments

Comments

@Rochet2
Copy link

Rochet2 commented Oct 6, 2022

Version

29.1.2

Steps to reproduce

  1. Clone my repo at https://github.com/Rochet2/jest-tomatchobject-class-getter-mcve
  2. run npm install
  3. run npm test
  4. You should see test output that demonstrates the expected output and unexpected output

Expected behavior

When expectation does not match actual, the output should show getter value for a class the same way it does for a regular object with a getter

Actual behavior

When expectation does not match actual, the output does not show class getter value at all which renders output less useful

Additional context

Image of the results of npm test: Image

This issue shows up when tests fail that expect on supertest responses. Supertest responses are classes that hold the body behind a getter. This means that when the test fails it will be difficult to debug as the entire body shows up missing. Here is a small example:

// Body shows up as missing entirely even if it does exist in the response
expect(response).toMatchObject({ body: { nonexistantKey: true } })

It appears that this part may be at fault. Object.keys() does not return the class getter, so the getter is never matched in this part of the code.
https://github.com/facebook/jest/blob/3c31dd619e8c022cde53f40fa12ea2a67f4752ce/packages/expect-utils/src/utils.ts#L127-L133

This issue seems similar to:

It appears that fixing this may partially affect/fix this:

Environment

System:
    OS: Windows 10 10.0.19044
    CPU: (4) x64 Intel(R) Core(TM) i5-3570K CPU @ 3.40GHz
  Binaries:
    Node: 16.17.1 - D:\Cores\jest\node_modules\.bin\node.CMD
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.15.0 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: 29.1.2 => 29.1.2
@Rochet2
Copy link
Author

Rochet2 commented Oct 10, 2022

I was trying to create a quick fix, but it seems quite difficult to cover all bases without editing things here and there. You can see my primitive first attempt here: main...Rochet2:jest:tomatchobject-output

The more I delve into this the more I feel like parts or the core of the diffing logic should be rewritten.
There are nasty edge cases with things like sets, maps, objects, classes, inheritance, getters, private values and so on that are not currently handled properly or as one (I) would expect. There are issues with the diff output and the diffing logic itself. The output can be different from expected as seen above and jest can crash as seen in linked issue. Here are also two cases that do not behave as I expect for example

  it("Objects differ, but test unexpectedly passes", () => {
    const a = new Set([1,2,3]);
    const b = new Set([1,2,3]);
    b.x = 5 // ignored
    expect(a).toMatchObject(b);
  });

  it('Says circular reference is missing, because unexpectedly inherited classes are ignored', () => {
    const a = {}; // ignored
    a.ref = a; // ignored
 
    const b = Object.create(a);
    b.other = 'child';
 
    const matcher = {other: 'child'};
    matcher.ref = matcher;
 
    expect(b).toMatchObject(matcher);
  });

I was not able to set up debugging properly yet while testing around.
I am probably missing something, but jest appears to not have any premade setup for source maps or similar and one needs to set it up themselves. Did not see a debugging guide yet anywhere. I did manage to set something up in the end, but not exactly to my liking as I currently debug the generated js files and not original ts files.
Hmm.

@github-actions
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 Nov 12, 2022
@github-actions
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 Dec 12, 2022
@github-actions
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
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 Jan 12, 2023
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

1 participant