You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The default .toEqual matcher tries to access private fields and fails, making it unusable for testing objects created from classes.
constcategoryResult=newCategory('id',{
...createDetails,})expect(categoryResult.value).toEqual({id: 'id',path: 'path',name: 'name',description: undefined,shortDescription: 'short description',parent: undefined,children: [],
updated,}asCategory)// TypeError: Cannot read private member #id from an object whose class did not declare it
The suggested workaround in this issue is to use .toMatchObject, but this passes when the received value has additional properties not in the expected value: jestjs/jest#10167 . IMO this makes it no real better than checking all properties manually, as the introduction of a new property will not cause tests to fail (as a reminder for them to be updated).
Possible solution A
Check the above issue again, or search other issues, for another solution!
Possible solution B
A custom matcher could solve this. My thoughts were:
It works with nested class objects and class objects in an array property. But fails when a property is a plain object, which sometimes happens in tests where a relation may be created directly using an interface rather than using a factory function that uses a class. E.g.:
constparent: Category={id: 'parent id',children: [],name: 'parent name',path: 'parent path',shortDescription: 'parent short description',updated: newDate(),}constcategoryResult=newCategory('id',{
...createDetails,
parent,// not created using a class
children,description: 'description',})
This is because Object.getOwnPropertyDescriptors(impl.__proto__) does not seem to return normal properties (i.e. simple properties, not getters/setters/etc).
I didn't feel it was worth continuing past this point but maybe it's not much more effort to get it working
The text was updated successfully, but these errors were encountered:
Problem
The default
.toEqual
matcher tries to access private fields and fails, making it unusable for testing objects created from classes.The suggested workaround in this issue is to use
.toMatchObject
, but this passes when the received value has additional properties not in the expected value: jestjs/jest#10167 . IMO this makes it no real better than checking all properties manually, as the introduction of a new property will not cause tests to fail (as a reminder for them to be updated).Possible solution A
Check the above issue again, or search other issues, for another solution!
Possible solution B
A custom matcher could solve this. My thoughts were:
this.equals
function: https://jestjs.io/docs/expect#thisequalsa-bI got part of the way through step 1 with the following:
It works with nested class objects and class objects in an array property. But fails when a property is a plain object, which sometimes happens in tests where a relation may be created directly using an interface rather than using a factory function that uses a class. E.g.:
This is because
Object.getOwnPropertyDescriptors(impl.__proto__)
does not seem to return normal properties (i.e. simple properties, not getters/setters/etc).I didn't feel it was worth continuing past this point but maybe it's not much more effort to get it working
The text was updated successfully, but these errors were encountered: