MCPcopy
hub / github.com/jestjs/jest / asymmetricMatch

Method asymmetricMatch

packages/expect/src/asymmetricMatchers.ts:256–289  ·  view source on GitHub ↗
(other: any)

Source from the content-addressed store, hash-verified

254 }
255
256 asymmetricMatch(other: any) {
257 // Ensures that the argument passed to the objectContaining method is an object
258 if (typeof this.sample !== 'object') {
259 throw new TypeError(
260 `You must provide an object to ${this.toString()}, not '${typeof this
261 .sample}'.`,
262 );
263 }
264
265 // Ensures that the argument passed to the expect function is an object
266 // This is necessary to avoid matching of non-object values
267 // Arrays are a special type of object, but having a valid match with a standard object
268 // does not make sense, hence we do a simple array check
269 if (typeof other !== 'object' || Array.isArray(other)) {
270 return false;
271 }
272
273 let result = true;
274
275 const matcherContext = this.getMatcherContext();
276 const objectKeys = getObjectKeys(this.sample);
277
278 for (const key of objectKeys) {
279 if (
280 !hasProperty(other, key) ||
281 !equals(this.sample[key], other[key], matcherContext.customTesters)
282 ) {
283 result = false;
284 break;
285 }
286 }
287
288 return this.inverse ? !result : result;
289 }
290
291 toString() {
292 return `Object${this.inverse ? 'Not' : ''}Containing`;

Callers

nothing calls this directly

Calls 4

toStringMethod · 0.95
getObjectKeysFunction · 0.90
equalsFunction · 0.90
hasPropertyFunction · 0.85

Tested by

no test coverage detected