Skip to content

Options

Chris Dibbern edited this page May 7, 2018 · 3 revisions

This page details some of the options available in the API.

LocationMode

Specifies how to locate elements within an array.

Used in: hasElements and its shared aliases, has and hasAsserts.

LocationMode.contains (default)

Only requires that the items in the expected array exist within the contextual value with no regard to relative position.

LocationMode.startsWith

Requires that the contextual value begin with the expected elements, in order.

LocationMode.endsWith

Requires that the contextual value end with the expected elements, in order.

LocationMode.sequentialContains

Requires that the contextual value contain the expected values as a subsequence.

MatchMode

Specifies how to interpret property assertions.

Used in: has, hasProperties, hasAll, hasElements.

MatchMode.asserts

For any lambda property assertions, wrap the actual value in an Assert and pass that in as the first parameter. Nevertheless, if the return value of the lambda is a strict (===) true or false, then true is pass, false is fail. Other property assertion types function as normal.

MatchMode.literal

Forces literal interpretation of property values, even if they are lambdas or regular expressions. Similar to deepEquals, but will only check defined properties, rather than failing when one is missing.

MatchMode.normal (default)

Tries to interpret assertion properties as a person might. If the property is a:

  1. Lambda - then either assume it will perform its own assertions, or use the boolean return value to determine pass/fail (true/false).
  2. RegExp - then, if the actual value is a string, check for a match. If its a regular expression, perform a strict equals (===) on their string representations. Otherwise, throw.
  3. Object - recursively match according to 1-4.
  4. All Else - perform a strict equals.

A lambda assertion should be of the form

  (actualValue: any) => boolean | IFluentNode | void;

where actualValue is the current property's value, and IFluentNode is the return value of an assertion (lets the framework know you didn't intend a boolean assertion).

Example of each MatchMode.normal value
Assert(obj).has({
  propFnA: p => p > 3,                          // a boolean assertion, true = pass
  propFnB: p => Assert(p).isDefined(),          // pass if propFnB is defined.
  propFnC: p => Assert(p).isDefined() && false, // caution: this will always fail, even if defined
  propStrRE: /something/,                       // passes if matches RegExp
  propRERE: /something/,                        // passes if string representations match
  propObj: {                                    // passes if nested assertions pass
    nestedProp: // ...
  },
  propOther: "any other value type"                 // passes, if strictly equal
});
Clone this wiki locally