Skip to content
Raphael Luba edited this page Nov 21, 2016 · 5 revisions

Description object

A Description object is provided to a matcher's describeTo and describeMismatch methods (see custom matchers for details). It provides the following methods to help creating consistent and detailed error descriptions:

  • append(text): Appends the given text to the description

  • appendValue(value): Appends the given value to the description while trying to maintain as much information in the final output as possible. For example strings ("15") can be differentiated from numbers (<15>), functions are described by their name, arrays describe their content and general objects are converted to JSON, if possible.

  • appendList(start, separator, end, list): Appends a list of values or self-describing objects (see below).

    For example to describe a tuple:

    description.appendList('(', ', ', ')', tuple);
  • appendDescriptionOf(selfDescribing): Appends self-describing objects (i.e. anything that has a describeTo method, such as a matcher) to the description.

  • indented(fn) (available since 3.0): Calls fn and increases the indentation level for everything that's appended by fn. Returns the result of fn.

    This can be used to indent the description of a sub-matcher to make it easier to decipher complex messages:

    
    describeMismatch(actual, description) {
        description.append('My submatcher thinks something went wrong: ');
    return description.indented(() => mySubMatcher.describeMismatch(actual, description));
    }

    If fn returns a promise, everything that's appended to description until the promise is resolved/rejected will be indented.