-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
toEqual throws a TypeError when using getter to private field of class #10167
Comments
@InExtremaRes did you ever find a fix or workaround for this? I've too run into this where I am trying to check the equality of a class's getters with a jest expect. |
@defunctzombie No, not really. I started creating a plain object with the properties I want to compare, but that's far from ideal. I made a little utility const x = new X(); // suppose this have props `foo` and `bar`
expect(pick(x, ['foo', 'bar'])).toEqual({ foo: 1, bar: 2 }); I hope to see some progress on this, the way I'm doing it is very tedious. |
I've got the same problem. Is there any update? |
I have the same problem as @defunctzombie, but using |
Having this issue also on |
This issue is still reproducible with jest |
Jest is able to determine properly, that the expected value does not match the received value, so it starts to build the corresponding console output for the difference. For this So the problem here has two parts:
Therefore the getter will be called when the rest of the code expects it to be already replaced with a value, and the getter in turn tries to access the private field, which is also missing by this time (working with the partially copied object). Now obviously copying the private fields is not achievable, so 2.) is not fixable, so we can only expect 1.) to be fixed. The properties copied over are determined by As far as i know there is no built in method that could get the inherited descriptors together with own, so the naive solution is to iterate the prototype chain and collect the descriptors for each level (excluding the root object model). According to that a naive fix can be to replace const descriptors: {
[x: string]: PropertyDescriptor;
} = Object.getOwnPropertyDescriptors(object); in deepCyclicCopyReplaceable.ts with: let descriptors: Record<string, PropertyDescriptor> = {};
let obj = object;
do {
descriptors = Object.assign({}, Object.getOwnPropertyDescriptors(obj), descriptors)
} while ( (obj = Object.getPrototypeOf( obj )) && obj !== Object.getPrototypeOf({}) ); With this in place all the test cases from the top of the issue pass or fail gracefully with proper comparison output in the console. Let me know if PR is welcome! |
Go for it!!! :-D |
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. |
🐛 Bug Report
Jest throws a
TypeError
when usingtoEqual
ortoMatchObject
in a class with private fields (those starting by#
) and a getter that exposes the value. I guess other similar matchers liketoStrictEqual
also fails but didn't test. I tested this using jest-circus and fails the same way.The class under test has a private field
#foo
and a getterget foo()
that returns the value of#foo
. UsingtoMatchObject
with the name of the getter (something like.toMatchObject({ foo: 0 })
) works when the value is equal but throws aTypeError
when are distinct (I bet when trying to show the differences, but I haven't debugged).To Reproduce
Expected behavior
Last two test fail properly since property
foo
are distinct between expected and received objects, showing the right report.envinfo
The text was updated successfully, but these errors were encountered: