Skip to content

Simple assertions

Chris Dibbern edited this page May 16, 2018 · 12 revisions

Where property assertions deal with an entity's specific properties, simple entity assertions talk about the whole entity, itself. This page describes the available methods.

contains

Checks whether the given string exists in the contextual value (also a string), at the given location. For array-contains assertions, see hasElements.

Scope: Preserves/unchanged.

contains(expected: T, location?: At): IFluentCore<T>;

Example

Assert("something").contains("thing");
Assert("something").contains("some", At.start);

Options

See At on the options page.

converted

Performs the specified transformation and returns the result in the fluent context. It's a shorthand for hasProperty(...).that which we include to aid clarity.

Scope: Automatically switches/narrows to the converted value.

converted<R>(lambda: (v: T) => R): IFluentCore<R>;

Example

Assert("agent 005").hasMatch(/\d+/)
  .that.converted(m => +m[0])
  .equals(5);

Assert("agent 005").hasMatch(/\d+/)
  .that.converted(Number)
  .equals(5);

equals

Compares the contextual value with an expected one. By default, the comparison is shallow and strictly equal (===).

Scope: Preserves/unchanged.

equals(expected: T, eqType: EqType = EqType.strictly): IFluentCore<T>;

Example

Assert(returnVal).equals(3.14);
Assert(viewModel).has(p => p.prop).that.equals(3.14);

A number of helper methods exist mainly for readability:

strictlyEquals(expected: T): IFluentCore<T>;
looselyEquals(expected: T): IFluentCore<T>;
deeplyEquals(
        expected: T,
        eqType: EqType.strictly | EqType.loosely = EqType.strictly
    ): IFluentCore<T>;
deepStrictlyEquals(expected: T): IFluentCore<T>;
deepLooselyEquals(expected: T): IFluentCore<T>;

Comparison types

EqType can be one of deepStrictly, deepLoosely, loosely, or strictly.

hasProperty

Existence-checks the selected property and optionally narrows the fluent context to its value.

Scope: Can be narrowed to the selected property's value (see that).

hasProperty<K extends keyof T>(key: K): INarrowableFluentCore<T, T[K]>;
hasProperty<K extends keyof T>(selector: (o: T) => T[K]): INarrowableFluentCore<T, T[K]>;

Example

Assert(myModel).hasProperty(p => p.prop).that.equals(42);

hasSingle

Asserts that the given list contains only one element. Errors if not an array.

Scope: Can be narrowed to the first element's value (see that).

hasSingle(): T extends any[] ? INarrowableFluentCore<T, T[0]> : void;

Example

Assert([314]).hasSingle().that.equals(314)

is

Checks whether the contextual value is an instance of the expected type.

Scope: Preserves/unchanged.

is(expectedType: {
        new(...args: any[]): any;
    }): IFluentCore<T>;

Example

Assert(val).is(MyModel);

isDefined

Checks whether the contextual value is defined.

Scope: Preserves/unchanged.

isDefined(): IFluentCore<T>;

Example

Assert(val).isDefined();

isEmpty

Asserts that the given array, object, or string is empty.

Scope: Preserves/unchanged.

isEmpty(): IFluentCore<T>;

Example

Assert([]).isEmpty(); // pass
Assert({}).isEmpty(); // pass
Assert("").isEmpty(); // pass
Assert({ one: 1 }).isEmpty(); // fail

isFalsy

Asserts that the value can be coerced to false (e.g., !value === true). Ex: empty strings, zero, null, undefined, false, etc.

Note: For a strict false check, use equals.

Scope: Preserves/unchanged.

isFalsy(): IFluentCore<T>;

Example

Assert("").isFalsy(); // pass
Assert(" ").isFalsy(); // fail

isNull

Asserts that the value is strictly null. Equivalent to .equals(null), but included for readability.

Scope: Preserves/unchanged.

isNull(): IFluentCore<T>;

Example

Assert(obj)
  .isDefined() // can pass when null
  .not.isNull();

isTruthy

Asserts that the value can be coerced to true. Ex: non-empty strings, numbers not equal to zero, objects, true, etc.

Note: For a strict true check, use equals.

Scope: Preserves/unchanged.

isTruthy(): IFluentCore<T>;

Example

Assert(123).isTruthy(); // pass
Assert(0).isTruthy(); // fail

matches, hasMatch

Check whether the contextual value matches the given regular expression.

Scope: hasMatch optionally narrows the scope (see that) to the matching parts. Exists to avoid grammar-related confusion.

matches(matcher: RegExp): IFluentCore<string>;
hasMatch(matcher: RegExp): INarrowableFluentCore<T, Array<string>>;

Example

Assert(val)
  .matches(/(\d+)-(\d+)-(\d+)/);

// or

Assert(val)
  .hasMatch(/(\d+)-(\d+)/)
  .that.has(parts => +parts[1])
  .that.equals(3);

satisfies

Checks whether the contextual value satisfies the given predicate (causes it to evaluate to true).

Scope: Preserves/unchanged.

satisfies(
        predicate: (t: T) => boolean
    ): IFluentCore<T>;

Example

Assert(val).satisfies(v => complicatedBizConstraint(val));

throws

Checks whether the contextual value throws an error. The parameter errorType is optional, and, if provided, the assertion will fail if the thrown error isn't an instance of it.

Scope: Can be narrowed to the actual error object for further matching (see that).

throws(): INarrowableFluentCore<T, Error>;
throws<TError extends Error>(errorType?: {
        new(...args: Array<any>): TError;
    }): INarrowableFluentCore<T, TError>;

Example

Assert(lambda)
  .throws(MyError)
  .that.has({
    message: /some regex/,
    custom: c => Assert(c).defined() // ...
  });
Clone this wiki locally