Documentation Home
This page is for an older version of Jasmine (5.0)
The current stable version of Jasmine is: 5.1 - You can also look at the docs for the next release: Edge

Interface: AsymmetricEqualityTester

AsymmetricEqualityTester

An asymmetric equality tester is an object that can match multiple objects. Examples include jasmine.any() and jasmine.stringMatching(). Jasmine includes a number of built-in asymmetric equality testers, such as jasmine.objectContaining. User-defined asymmetric equality testers are also supported.

Asymmetric equality testers work with any matcher, including user-defined custom matchers, that uses MatchersUtil#equals or MatchersUtil#contains.

Since:
  • 2.0.0
See:

Example

function numberDivisibleBy(divisor) {
  return {
    asymmetricMatch: function(n) {
      return typeof n === 'number' && n % divisor === 0;
    },
    jasmineToString: function() {
      return `<a number divisible by ${divisor}>`;
    }
  };
}

const actual = {
  n: 2,
  otherFields: "don't care"
};

expect(actual).toEqual(jasmine.objectContaining({n: numberDivisibleBy(2)}));

Methods

asymmetricMatch(value, matchersUtil) → {Boolean}

Determines whether a value matches this tester

Parameters:
Name Type Description
value any

The value to test

matchersUtil MatchersUtil

utilities for testing equality, etc

Returns:
Type
Boolean

jasmineToString(pp) → {String}

Returns a string representation of this tester to use in matcher failure messages

Parameters:
Name Type Description
pp function

Function that takes a value and returns a pretty-printed representation

Returns:
Type
String