Assertive.ts - Core API / Assertion
Class: Assertion<T>
Base class for all assertions.
Type parameters
Name | Description |
---|---|
T |
the type of the actual value |
Table of contents
Constructors
Properties
Methods
- asType
- execute
- normalized
- proxyInverter
- toBe
- toBeEqual
- toBeFalsy
- toBeInstanceOf
- toBeNull
- toBeOfType
- toBePresent
- toBeSame
- toBeSameAs
- toBeSimilar
- toBeTruthy
- toBeUndefined
- toExist
- toMatch
Constructors
constructor
• new Assertion<T
>(actual
): Assertion
<T
>
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
actual |
T |
Returns
Assertion
<T
>
Defined in
packages/core/src/lib/Assertion.ts:56
Properties
actual
• Protected
Readonly
actual: T
Defined in
packages/core/src/lib/Assertion.ts:50
inverted
• Protected
Readonly
inverted: boolean
Defined in
packages/core/src/lib/Assertion.ts:52
not
• Readonly
not: Assertion
<T
>
Defined in
packages/core/src/lib/Assertion.ts:54
Methods
asType
▸ asType<S
, A
>(typeFactory
): A
Check first if the value is of some specific type, in which case returns an assertion instance for that specific type. The new assertion is built from a factory that should extend from the base Assertion class.
We provide some basic factories in TypeFactories
. If you need some
other factory for a custom assertion for instance, you can easily create
one from a Factory reference and a predicate.
Type parameters
Name | Type | Description |
---|---|---|
S |
S |
the type of the factory’s value |
A |
extends Assertion <S > |
the type of the assertion factory |
Parameters
Name | Type | Description |
---|---|---|
typeFactory |
TypeFactory <S , A > |
a factory to assert the type and create an assertion |
Returns
A
a more specific assertion based on the factory type
Example
expect(unknownValue)
.asType(TypeFactories.STRING)
.toStartWith("/api/");
expect(uuid)
.asType({
Factory: UUIDAssertion, // a custom UUID assertion
predicate: (value): value is UUID => {
return typeof value === "string" && UUID.PATTER.test(value);
},
})
.isValid();
Defined in
packages/core/src/lib/Assertion.ts:552
execute
▸ execute(options
): this
A convenience method to execute the assertion. The inversion logic for
.not
is already embedded in this method, so this should always be used
in assertions to keep the negation system working
Parameters
Name | Type | Description |
---|---|---|
options |
ExecuteOptions |
the execution options for the assertion |
Returns
this
the Assertion instance if no error was thrown
Defined in
packages/core/src/lib/Assertion.ts:84
normalized
▸ normalized(): this
A convenience method to normalize the assertion instance. If it was
inverted with .not
, it’ll return it back to the previous non-inverted
state. Otherwise, it returns the same instance.
Returns
this
the normalized assertion instance
Defined in
packages/core/src/lib/Assertion.ts:70
proxyInverter
▸ proxyInverter(isInverted
): undefined
| (target
: Assertion
<T
>, p
: string
| symbol
, receiver
: any
) => any
Parameters
Name | Type |
---|---|
isInverted |
boolean |
Returns
undefined
| (target
: Assertion
<T
>, p
: string
| symbol
, receiver
: any
) => any
Defined in
packages/core/src/lib/Assertion.ts:569
toBe
▸ toBe(expected
): this
Another alias of .toBeSame(..)
assertion.
Parameters
Name | Type | Description |
---|---|---|
expected |
T |
the value to compare for referential equality |
Returns
this
the assertion instance
Example
const x = { a: 1 };
const y = x;
expect(x).toBe(x);
expect(x).toBe(y);
expect(x).not.toBe({ ...x });
Defined in
packages/core/src/lib/Assertion.ts:481
toBeEqual
▸ toBeEqual(expected
): this
Check if the value is deep strict equal to another value.
Parameters
Name | Type | Description |
---|---|---|
expected |
T |
the value to compare for deep equality |
Returns
this
the assertion instance
Example
expect(3 + 2).toBeEqual(5);
expect({ a: { b: 1 } }).toBeEqual({ a: { b: 1 } });
expect(today).toBeEqual(new Date(today.toISOString()));
Defined in
packages/core/src/lib/Assertion.ts:333
toBeFalsy
▸ toBeFalsy(): this
Check if the value is a falsy value. There are six falsy values in
JavaScript: null
, undefined
, 0
, ""
, false
, NaN
. Everything
else is truthy.
Returns
this
the assertion instance
Example
expect(0).toBeFalsy();
expect("").toBeFalsy();
Defined in
packages/core/src/lib/Assertion.ts:273
toBeInstanceOf
▸ toBeInstanceOf(Expected
): this
Check if the value is an instance of the provided constructor.
Parameters
Name | Type | Description |
---|---|---|
Expected |
Constructor <unknown > |
the constructor the value should be an instance |
Returns
this
the assertion instance
Example
expect(pontiac).toBeInstanceOf(Car);
expect(today).toBeInstanceOf(Date);
Defined in
packages/core/src/lib/Assertion.ts:303
toBeNull
▸ toBeNull(): this
Check if the value is null
.
Returns
this
the assertion instance
Example
expect(myNullValue).toBeNull();
Defined in
packages/core/src/lib/Assertion.ts:186
toBeOfType
▸ toBeOfType(expected
): this
Checks if the value is of a specific data type. The supported data types
are the same as the typeof
operator, plus an additional array
which
allows desabiguation between object
(which can also be an array).
Parameters
Name | Type | Description |
---|---|---|
expected |
DataType |
the expected data type |
Returns
this
the assertion instance
Example
const arr = [1, 2, 3];
expect(arr).toBeOfType("array");
expect(arr[0]).toBeOfType("number");
expect(arr[9]).toBeOfType("undefined");
Defined in
packages/core/src/lib/Assertion.ts:502
toBePresent
▸ toBePresent(): this
Check if the value is present. This means that the value should not be
undefined
.
Returns
this
the assertion instance
Example
expect(PI).toBePresent();
Defined in
packages/core/src/lib/Assertion.ts:214
toBeSame
▸ toBeSame(expected
): this
Check if the value is the same as another value.
Parameters
Name | Type | Description |
---|---|---|
expected |
T |
the value to compare for referential equality |
Returns
this
the assertion instance
Example
const x = { a: 1 };
const y = x;
expect(x).toBeSame(x);
expect(x).toBeSame(y);
expect(x).not.toBeSame({ ...x });
Defined in
packages/core/src/lib/Assertion.ts:425
toBeSameAs
▸ toBeSameAs(expected
): this
Alias of .toBeSame(..)
assertion.
Parameters
Name | Type | Description |
---|---|---|
expected |
T |
the value to compare for referential equality |
Returns
this
the assertion instance
Example
const x = { a: 1 };
const y = x;
expect(x).toBeSameAs(x);
expect(x).toBeSameAs(y);
expect(x).not.toBeSameAs({ ...x });
Defined in
packages/core/src/lib/Assertion.ts:460
toBeSimilar
▸ toBeSimilar(expected
): this
Check if the value is shallow equal to another value.
Parameters
Name | Type | Description |
---|---|---|
expected |
T |
the value to compare for shallow equality |
Returns
this
the assertion instance
Example
expect(3 + 2).toBeSimilar(5);
expect({ a: 1 }).toBeSimilar({ a: 1 });
expect({ a: { b: 1 } }).not.toBeSimilar({ a: {b: 1} });
Defined in
packages/core/src/lib/Assertion.ts:365
toBeTruthy
▸ toBeTruthy(): this
Check if the value is a truthy value. There are six falsy values in
JavaScript: null
, undefined
, 0
, ""
, false
, NaN
. Everything
else is truthy.
Returns
this
the assertion instance
Example
expect("hello world").toBeTruthy();
expect(128).toBeTruthy();
Defined in
packages/core/src/lib/Assertion.ts:244
toBeUndefined
▸ toBeUndefined(): this
Check if the value is undefined
Returns
this
the assertion instance
Example
expect(myUndefinedValue).toBeUndefined()
Defined in
packages/core/src/lib/Assertion.ts:159
toExist
▸ toExist(): this
Check if the value exists. This means that the value should be neither
null
nor undefined
.
Returns
this
the assertion instance
Example
expect(planetEarth).toExist();
Defined in
packages/core/src/lib/Assertion.ts:132
toMatch
▸ toMatch(matcher
): this
Check if the value matches the given predicate.
Parameters
Name | Type | Description |
---|---|---|
matcher |
(actual : T ) => boolean |
a matcher predicate |
Returns
this
the assertion instance