@@ -108,7 +108,7 @@ export class Anything extends AsymmetricMatcher<void> {
108
108
}
109
109
110
110
export class ObjectContaining extends AsymmetricMatcher <
111
- Record < string , unknown >
111
+ Record < string | symbol | number , unknown >
112
112
> {
113
113
constructor ( sample : Record < string , unknown > , inverse = false ) {
114
114
super ( sample , inverse )
@@ -126,18 +126,27 @@ export class ObjectContaining extends AsymmetricMatcher<
126
126
return obj . constructor . prototype
127
127
}
128
128
129
- hasProperty ( obj : object | null , property : string ) : boolean {
129
+ hasProperty ( obj : object | null , property : string | symbol ) : boolean {
130
130
if ( ! obj ) {
131
131
return false
132
132
}
133
133
134
- if ( Object . prototype . hasOwnProperty . call ( obj , property ) ) {
134
+ if ( Object . hasOwn ( obj , property ) ) {
135
135
return true
136
136
}
137
137
138
138
return this . hasProperty ( this . getPrototype ( obj ) , property )
139
139
}
140
140
141
+ getProperties ( obj : object ) : ( string | symbol ) [ ] {
142
+ return [
143
+ ...Object . keys ( obj ) ,
144
+ ...Object . getOwnPropertySymbols ( obj ) . filter (
145
+ s => Object . getOwnPropertyDescriptor ( obj , s ) ?. enumerable ,
146
+ ) ,
147
+ ]
148
+ }
149
+
141
150
asymmetricMatch ( other : any ) : boolean {
142
151
if ( typeof this . sample !== 'object' ) {
143
152
throw new TypeError (
@@ -149,14 +158,21 @@ export class ObjectContaining extends AsymmetricMatcher<
149
158
let result = true
150
159
151
160
const matcherContext = this . getMatcherContext ( )
152
- for ( const property in this . sample ) {
161
+ const properties = this . getProperties ( this . sample )
162
+ for ( const property of properties ) {
153
163
if (
154
164
! this . hasProperty ( other , property )
155
- || ! equals (
156
- this . sample [ property ] ,
157
- other [ property ] ,
158
- matcherContext . customTesters ,
159
- )
165
+ ) {
166
+ result = false
167
+ break
168
+ }
169
+ const value = Object . getOwnPropertyDescriptor ( this . sample , property ) ?. value ?? this . sample [ property ]
170
+ const otherValue = Object . getOwnPropertyDescriptor ( other , property ) ?. value ?? other [ property ]
171
+ if ( ! equals (
172
+ value ,
173
+ otherValue ,
174
+ matcherContext . customTesters ,
175
+ )
160
176
) {
161
177
result = false
162
178
break
0 commit comments