Assertion

Assertion

I represent an assertion we want to make on a specific object (called the actual), against an expectation, in the context of a TestRunner.

I have multiple ways to write expectations, represented by my public instance methods.

When the expectation is evaluated, it reports the results to the TestRunner.

Constructor

new Assertion()

Source:

Classes

Assertion

Methods

doesNotInclude(expectedObject, equalityCriteriaopt) → {void}

Expects the actual collection object to not include an expected object.

Expects the actual collection object to not include an expected object. Works for Array, Strings, Set and Maps. It works in the same way as includes but negating the result.

Source:
Parameters:
Name Type Attributes Description
expectedObject *

the object that you are expecting to not be included.

equalityCriteria function <optional>

a two-argument function to be used to compare elements from the actual collection and expectedObject. Optional.

Returns:
Type:
void
Examples
array
assert.that([1, 2, 3]).doesNotInclude(4)
set
assert.that(new Set([1, 2, 3])).doesNotInclude(4)
string
assert.that('42').doesNotInclude('5')

doesNotRaise(notExpectedError) → {void}

Expects the actual object (in this case, a function) to not raise an exception that matches the given criteria.

Expects the actual object (in this case, a function) to not raise an exception that matches the given criteria.

Source:
Parameters:
Name Type Description
notExpectedError any | RegExp

the error object expected not to be thrown or a Regex that should not match with the actual error message.

Returns:
Type:
void
Examples
exact error object
assert.that(() => throw new Error("oops")).doesNotRaise(new Error("ay!"))
regular expression
assert.that(() => throw new Error("oops")).doesNotRaise(/ay/)

doesNotRaiseAnyErrors() → {void}

Expects the actual object (in this case, a function) to not raise any exception at all.

Expects the actual object (in this case, a function) to not raise any exception at all. This is the most accurate way to ensure that a piece of code does not fail.

Source:
Returns:
Type:
void
Example
assert.that(() => 42).doesNotRaiseAnyErrors()

includes(expectedObject, equalityCriteriaopt) → {void}

Expects the actual collection object to include an expected object.

Expects the actual collection object to include an expected object. Works for Array, Strings, Set and Maps. It works in the same way as isIncludedIn but swapping actual and expected objects.

Source:
Parameters:
Name Type Attributes Description
expectedObject *

the object that you are expecting to be included.

equalityCriteria function <optional>

a two-argument function to be used to compare elements from the actual collection and expectedObject. Optional.

Returns:
Type:
void
Examples
array
assert.that([1, 2, 3]).includes(2)
set
assert.that(new Set([1, 2, 3])).includes(3)
string
assert.that('42').includes('4')

includesExactly(…objects) → {void}

Expects the actual collection object to include exactly the same elements as the given ones, in any order.

Expects the actual collection object to include exactly the same elements as the given ones, in any order. The comparison is done using the default equality criteria.

Source:
Parameters:
Name Type Attributes Description
objects * <repeatable>

the objects that you are expecting to be included in the actual collection.

Returns:
Type:
void
Example
assert.that([1, 2, 3]).includesExactly(2, 1, 3)
assert.that(new Set([1, 2, 3])).includesExactly(3, 1, 2)

isEmpty() → {void}

Expects the actual object to be an empty collection (arrays, strings, sets and maps).

Expects the actual object to be an empty collection (arrays, strings, sets and maps). Another way of writing this assertion is to use the isEmpty method.

Source:
Returns:
Type:
void
Examples
assert.that([]).isEmpty()
assert.that('').isEmpty()
assert.that(new Set()).isEmpty()
assert.that(new Map()).isEmpty()
equivalent version
assert.isEmpty('')

isEqualTo(expected, criteriaopt) → {void}

Expects the actual object to be equal to an expected object, according to a default or custom criteria.

Expects the actual object to be equal to an expected object, according to a default or custom criteria. Another way of writing this assertion is to use the Asserter#areEqual method.

Source:
Parameters:
Name Type Attributes Description
expected *

the object that you are expecting the actual to be.

criteria function <optional>

a two-argument function to be used to compare actual and expected. Optional.

Returns:
Type:
void
Examples
assert.that('3' + '4').isEqualTo('34')
equivalent version
assert.areEqual(3 + 4, 7)
custom criteria
assert.that([2, 3]).isEqualTo(['x', 'y'], (a, b) => a.length === b.length)

isFalse() → {void}

Expects the actual object to be strictly equal to false.

Expects the actual object to be strictly equal to false. Other "falsy" values according to Javascript rules will be considered not false. Another way of writing this assertion is to use the isFalse method.

Source:
Returns:
Type:
void
Examples
assert.that(3 >= 4).isFalse()
equivalent version
assert.isFalse(3 >= 4)

isGreaterThan(number) → {void}

Expects the actual object (in this case, a number) to be strictly greater than the given one.

Expects the actual object (in this case, a number) to be strictly greater than the given one.

Source:
Parameters:
Name Type Description
number Number

number to compare against the actual value.

Returns:
Type:
void
Example
assert.that(4).isGreaterThan(3)

isGreaterThanOrEqualTo(number) → {void}

Expects the actual object (in this case, a number) to be greater or equal than the given one.

Expects the actual object (in this case, a number) to be greater or equal than the given one.

Source:
Parameters:
Name Type Description
number Number

number to compare against the actual value.

Returns:
Type:
void
Example
assert.that(4).isGreaterThanOrEqualTo(4)

isIdenticalTo(expected) → {void}

Expects the actual object to be identical (be the same reference) to an expected one.

Expects the actual object to be identical (be the same reference) to an expected one. Another way of writing this assertion is to use the Asserter#areIdentical method.

Source:
Parameters:
Name Type Description
expected *

the object that you are expecting the actual to be.

Returns:
Type:
void
Examples
literals
assert.that(3).isIdenticalTo(3)
equivalent version
assert.areIdentical(3, 3)
same reference
const object = { my: "object" }
assert.that(object).isIdenticalTo(object)

isIncludedIn(expectedCollection, equalityCriteriaopt) → {void}

Expects the actual object to be included on an expected collection.

Expects the actual object to be included on an expected collection. Works for Array, Strings, Set and Maps. It works in the same way as includes but swapping actual and expected objects.

Source:
Parameters:
Name Type Attributes Description
expectedCollection Array | Set | Map | String

the collection that you are expecting the actual to be included in.

equalityCriteria function <optional>

a two-argument function to be used to compare elements from the expectedCollection and your actual object. Optional.

Returns:
Type:
void
Examples
array
assert.that(2).isIncludedIn([1, 2, 3])
set
assert.that(3).isIncludedIn(new Set([1, 2, 3]))
string
assert.that('lo').isIncludedIn('hello')

isLessThan(number) → {void}

Expects the actual object (in this case, a number) to be strictly less than the given one.

Expects the actual object (in this case, a number) to be strictly less than the given one.

Source:
Parameters:
Name Type Description
number Number

number to compare against the actual value.

Returns:
Type:
void
Example
assert.that(3).isLessThan(4)

isLessThanOrEqualTo(number) → {void}

Expects the actual object (in this case, a number) to be less or equal than the given one.

Expects the actual object (in this case, a number) to be less or equal than the given one.

Source:
Parameters:
Name Type Description
number Number

number to compare against the actual value.

Returns:
Type:
void
Example
assert.that(4).isLessThanOrEqualTo(4)

isNearTo(number, precisionDigitsopt) → {void}

Expects the actual object (in this case, a number) to be near to the expected one, considering a certain precision of decimal digits.

Expects the actual object (in this case, a number) to be near to the expected one, considering a certain precision of decimal digits.

Source:
Parameters:
Name Type Attributes Default Description
number Number

number to compare against the actual value

precisionDigits Number <optional>
4

number of decimal digits to consider in the comparison. Optional, defaults to 4.

Returns:
Type:
void
Example
assert.that(3.14159).isNearTo(3.1416, 4)

isNotEmpty() → {void}

Expects the actual object to be a non-empty collection (arrays, strings, sets and maps).

Expects the actual object to be a non-empty collection (arrays, strings, sets and maps). Another way of writing this assertion is to use the isNotEmpty method.

Source:
Returns:
Type:
void
Examples
assert.that([42]).isNotEmpty()
assert.that('hello').isNotEmpty()
assert.that(new Set([42])).isNotEmpty()
assert.that(new Map([['key', 42]])).isNotEmpty()
equivalent version
assert.isNotEmpty('hello')

isNotEqualTo(expected, criteriaopt) → {void}

Expects the actual object to be not equal to an expected object, according to a default or custom criteria.

Expects the actual object to be not equal to an expected object, according to a default or custom criteria. Another way of writing this assertion is to use the Asserter#areNotEqual method.

Source:
Parameters:
Name Type Attributes Description
expected *

the object that you are expecting the actual to be not equal.

criteria function <optional>

a two-argument function to be used to compare actual and expected. Optional.

Returns:
Type:
void
Examples
assert.that('3' + '4').isNotEqualTo('7')
equivalent version
assert.areNotEqual(3 + 4, 8)
custom criteria
assert.that([2, 3]).isNotEqualTo(['x'], (a, b) => a.length === b.length)

isNotIdenticalTo(expected) → {void}

Expects the actual object to be not identical (not be the same reference) to an expected one.

Expects the actual object to be not identical (not be the same reference) to an expected one. Another way of writing this assertion is to use the Asserter#areNotIdentical method.

Source:
Parameters:
Name Type Description
expected *

the object that you are expecting the actual to not be identical to.

Returns:
Type:
void
Examples
literals
assert.that(3).isNotIdenticalTo('3')
equivalent version
assert.areNotIdentical(3, '3')
different references
const object1 = { my: "object" }
const object2 = { my: "object" }
assert.that(object1).isNotIdenticalTo(object2)

isNotIncludedIn(expectedCollection, equalityCriteriaopt) → {void}

Expects the actual object to not be included in an expected collection.

Expects the actual object to not be included in an expected collection. Works for Array, Strings, Set and Maps. It works in the same way as doesNotInclude but swapping actual and expected objects.

Source:
Parameters:
Name Type Attributes Description
expectedCollection *

the collection that you are expecting the actual to not be included in.

equalityCriteria function <optional>

a two-argument function to be used to compare elements from the expectedCollection and your actual object. Optional.

Returns:
Type:
void
Examples
array
assert.that(4).isNotIncludedIn([1, 2, 3])
set
assert.that(4).isNotIncludedIn(new Set([1, 2, 3]))
string
assert.that('x').isNotIncludedIn('hello')

isNotNull() → {void}

Expects the actual object to be different from null.

Expects the actual object to be different from null. Another way of writing this assertion is to use the isNotNull method.

Source:
Returns:
Type:
void
Examples
assert.that('something').isNotNull()
equivalent version
assert.isNotNull('something')

isNotUndefined() → {void}

Expects the actual object to be not strictly equal to undefined.

Expects the actual object to be not strictly equal to undefined. Another way of writing this assertion is to use the isNotUndefined method.

Source:
Returns:
Type:
void
Examples
assert.that("hello".length).isNotUndefined()
equivalent version
assert.isNotUndefined("hello".length)

isNull() → {void}

Expects the actual object to be strictly equal to null.

Expects the actual object to be strictly equal to null. Another way of writing this assertion is to use the isNull method.

Source:
Returns:
Type:
void
Examples
assert.that(null).isNull()
equivalent version
assert.isNull(null)

isTrue() → {void}

Expects the actual object to be strictly equal to true.

Expects the actual object to be strictly equal to true. Other "truthy" values according to Javascript rules will be considered not true. Another way of writing this assertion is to use the isTrue method.

Source:
Returns:
Type:
void
Examples
assert.that(3 < 4).isTrue()
equivalent version
assert.isTrue(3 < 4)

isUndefined() → {void}

Expects the actual object to be strictly equal to undefined.

Expects the actual object to be strictly equal to undefined. Another way of writing this assertion is to use the isUndefined method.

Source:
Returns:
Type:
void
Examples
assert.that(object.missingProperty).isUndefined()
equivalent version
assert.isUndefined(object.missingProperty)

matches(regex) → {void}

Expects the actual object (in this case, a string) to match the given regular expression.

Expects the actual object (in this case, a string) to match the given regular expression.

Source:
Parameters:
Name Type Description
regex RegExp

the regular expression to match against the actual value.

Returns:
Type:
void
Examples
assert.that("hello world").matches(/hello/)
equivalent version
assert.isMatching('hello', /[a-z]+/)

raises(errorExpectation) → {void}

Expects the actual object (in this case, a function) to raise an exception that matches the given expectation.

Expects the actual object (in this case, a function) to raise an exception that matches the given expectation.

Source:
Parameters:
Name Type Description
errorExpectation any | RegExp

the error object expected to be thrown or a Regex that matches with the actual error message.

Returns:
Type:
void
Examples
exact error object
assert.that(() => throw new Error("oops")).raises(new Error("oops"))
regular expression
assert.that(() => throw new Error("oops I did it again")).raises(/oops/)