JsHamcrest — Main Namespace

Provides the main namespace, along with core abstractions.

JsHamcrest.areArraysEqual(array, anotherArray)

Returns whether the arrays array and anotherArray are equivalent.

Parameters:
  • array – An array.
  • anotherArray – Another array.
Returns:

True if both arrays are equivalent; false otherwise.

JsHamcrest.isMatcher(obj)

Returns whether obj is a matcher.

Parameters:obj – Object.
Returns:True if the given object is a matcher; false otherwise.
JsHamcrest.version

Library version.

JsHamcrest.CombinableMatcher Class

class JsHamcrest.CombinableMatcher({matches, describeTo, describeValueTo})

Extends JsHamcrest.SimpleMatcher. Defines a composite matcher, that is, a matcher that wraps several matchers into one.

Parameters:
  • matches – Function that provides the matching logic.
  • describeTo – Function that describes this matcher to a JsHamcrest.Description.
  • describeValueTo(Optional) Function that describes the actual value under test. If not provided, the actual value will be described as a JavaScript literal.
CombinableMatcher.and(matcherOrValue)

Wraps this matcher and the given matcher using JsHamcrest.Matchers.allOf().

Parameters:matcherOrValue – Instance of JsHamcrest.SimpleMatcher or a value.
Returns:Instance of JsHamcrest.CombinableMatcher.
CombinableMatcher.or(matcherOrValue)

Wraps this matcher and the given matcher using JsHamcrest.Matchers.anyOf().

Parameters:matcherOrValue – Instance of JsHamcrest.SimpleMatcher or a value.
Returns:Instance of JsHamcrest.CombinableMatcher.

JsHamcrest.Description Class

class JsHamcrest.Description

Extends Object. Defines a textual description builder.

Description.append(text)

Appends text to this description.

Parameters:text – Text to append to this description.
Returns:this.
Description.appendDescriptionOf(selfDescribingObject)

Appends the description of a self describing object to this description.

Parameters:selfDescribingObject – Any object that have a describeTo() function that accepts a JsHamcrest.Description object as argument.
Returns:this.
Description.appendList(start, separator, end, list)

Appends the description of several self describing objects to this description.

Parameters:
  • start – Start string.
  • separator – Separator string.
  • end – End string.
  • list – Array of self describing objects. These objects must have a describeTo() function that accepts a JsHamcrest.Description object as argument.
Returns:

this.

Description.appendLiteral(literal)

Appends a JavaScript language’s literal to this description.

Parameters:literal – Literal to append to this description.
Returns:this.
Description.appendValueList(start, separator, end, list)

Appends an array of values to this description.

Parameters:
  • start – Start string.
  • separator – Separator string.
  • end – End string.
  • list – Array of values to be described to this description.
Returns:

this.

Description.get()

Gets the current content of this description.

Returns:Current content of this description.

JsHamcrest.SimpleMatcher Class

class JsHamcrest.SimpleMatcher({matches, describeTo, describeValueTo})

Extends Object. Defines a matcher that relies on the external functions provided by the caller in order to shape the current matching logic.

Below, an example of matcher that matches middle-aged people:

var middleAged = new JsHamcrest.SimpleMatcher({
    matches: function(person) {
        return person.age >= 40 && person.age <= 60;
    },
    describeTo: function(description) {
        description.append('middle-aged');
    }
});

// Matcher usage
middleAged.matches({name:'Gregory', age:50});  // Expected: true
middleAged.matches({name:'Jeniffer', age:27}); // Expected: false
Parameters:
  • matches – Function that provides the matching logic.
  • describeTo – Function that describes this matcher to a JsHamcrest.Description.
  • describeValueTo(Optional) Function that describes the actual value under test. If not provided, the actual value will be described as a JavaScript literal.
SimpleMatcher.describeTo(description)

Describes this matcher’s tasks to description.

Parameters:description – Instance of JsHamcrest.Description.
Returns:Nothing.
SimpleMatcher.describeValueTo(actual, description)

Describes actual to description.

Parameters:
Returns:

Nothing.

SimpleMatcher.matches(actual)

Checks if this matcher matches actual.

Parameters:actual – Actual value.
Returns:True if the matcher matches the actual value; false otherwise.