-
Notifications
You must be signed in to change notification settings - Fork 0
Simple assertions
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.
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>;
Assert("something").contains("thing");
Assert("something").contains("some", At.start);
See At on the options page.
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>;
Assert("agent 005").hasMatch(/\d+/)
.that.converted(m => +m[0])
.equals(5);
Assert("agent 005").hasMatch(/\d+/)
.that.converted(Number)
.equals(5);
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>;
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>;
EqType can be one of deepStrictly
, deepLoosely
, loosely
, or strictly
.
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]>;
Assert(myModel).hasProperty(p => p.prop).that.equals(42);
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;
Assert([314]).hasSingle().that.equals(314)
Checks whether the contextual value is an instance of the expected type.
Scope: Preserves/unchanged.
is(expectedType: {
new(...args: any[]): any;
}): IFluentCore<T>;
Assert(val).is(MyModel);
Checks whether the contextual value is defined.
Scope: Preserves/unchanged.
isDefined(): IFluentCore<T>;
Assert(val).isDefined();
Asserts that the given array, object, or string is empty.
Scope: Preserves/unchanged.
isEmpty(): IFluentCore<T>;
Assert([]).isEmpty(); // pass
Assert({}).isEmpty(); // pass
Assert("").isEmpty(); // pass
Assert({ one: 1 }).isEmpty(); // fail
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>;
Assert("").isFalsy(); // pass
Assert(" ").isFalsy(); // fail
Asserts that the value is strictly null. Equivalent to .equals(null)
, but included for readability.
Scope: Preserves/unchanged.
isNull(): IFluentCore<T>;
Assert(obj)
.isDefined() // can pass when null
.not.isNull();
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>;
Assert(123).isTruthy(); // pass
Assert(0).isTruthy(); // fail
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>>;
Assert(val)
.matches(/(\d+)-(\d+)-(\d+)/);
// or
Assert(val)
.hasMatch(/(\d+)-(\d+)/)
.that.has(parts => +parts[1])
.that.equals(3);
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>;
Assert(val).satisfies(v => complicatedBizConstraint(val));
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>;
Assert(lambda)
.throws(MyError)
.that.has({
message: /some regex/,
custom: c => Assert(c).defined() // ...
});