Assertion

Assertion

It contains all the assertion methods.

Constructor

new Assertion(actual, nameopt, parentopt)

Source:
Do no use it directly. Use the module function
Parameters:
Name Type Attributes Description
actual * actual value, promise or function
name string <optional>
name of the field that could be used in the error messages
parent Assertion <optional>
parent assertion

Members

andIt :this

Source:
It could be use for meaningful chains
Type:
  • this
Example
assert('a').isAString().andIt.hasLengthOf(1)

Methods

contains(str, messageopt) → {this}

Source:
Asserts that the actual value contains the given string.
Example
assert('abcd').contains('bc') // Passes
assert('abcd').contains('ac') // Fails
Parameters:
Name Type Attributes Default Description
str string given string
message string <optional>
{name} does not contain the given string error message
Throws:
when the actual value does not contain the given string
Type
AssertionError
Returns:
chainable method
Type
this

doesNotIncludeOwnProperty(name, messageopt) → {this}

Source:
Asserts that the actual value does not have the own given property
Example
assert({ a: 3 }).doesNotIncludeOwnProperty('b') // Passes
Parameters:
Name Type Attributes Default Description
name string name of the property
message string <optional>
{name} contains the own property {property} error message
Throws:
when the actual value has the own given property
Type
AssertionError
Returns:
chainable method
Type
this

doesNotIncludeProperty(name, messageopt) → {this}

Source:
Asserts that the actual value does not have the given property
Example
assert({ a: 3 }).doesNotIncludeProperty('b') // Passes
Parameters:
Name Type Attributes Default Description
name string name of the property
message string <optional>
{name} contains the property {property} error message
Throws:
when the actual value has the given property
Type
AssertionError
Returns:
chainable method
Type
this

endsWith(str, messageopt) → {this}

Source:
Asserts that the actual value ends with the given string.
Example
assert('abcd').endsWith('cd') // Passes
assert('abcd').endsWith('bc') // Fails
Parameters:
Name Type Attributes Default Description
str string given string
message string <optional>
{name} does not end with the given string error message
Throws:
when the actual value does not end with the given string
Type
AssertionError
Returns:
chainable method
Type
this

every(test) → {this}

Source:
Asserts that every value of the array pass the test
Example
assert([3, 6]).every(it => it.isAbove(2)) // Passes
Parameters:
Name Type Description
test assertionCallback test for each element
Throws:
when any value fails the test
Type
AssertionError
Returns:
chainable method
Type
this

getActual() → {*}

Source:
Example
console.log(assert('orange').isAString().getActual()) // prints 'orange'
Returns:
current value
Type
*

getFullName() → {string}

Source:
Returns:
full name including parent names
Type
string

getName() → {string}

Source:
Example
console.log(assert('orange').isAString().getName()) // prints 'actual value'
console.log(assert('orange', 'fruit').isAString().getName()) // prints 'fruit'
Returns:
current name
Type
string

getRef() → {*}

Source:
Deprecated:
Returns:
actual value
Type
*

hasLength(testopt, messageopt) → {this}

Source:
Asserts that the property length exists and optionally pass some test against it
Example
assert('').hasLength() // Passes
assert(1).hasLength() // Fails
assert([2]).hasLength() // Passes
assert([2, 5]).hasLength(it => it.isAbove(1)) // Passes
Parameters:
Name Type Attributes Default Description
test assertionCallback <optional>
test for the property
message string <optional>
{name} does not have length property error message
Throws:
when the property does not exists or the test fails
Type
AssertionError
Returns:
chainable method
Type
this

hasLengthOf(expected, messageopt) → {this}

Source:
Asserts that the property length exists and it is equal to the giving number
Example
assert('').hasLengthOf(0) // Passes
assert(1).hasLengthOf(1) // Fails
assert([2, 3]).hasLengthOf(2) // Passes
Parameters:
Name Type Attributes Description
expected number expected length
message string <optional>
error message
Throws:
when the property does not exists or the test fails
Type
AssertionError
Returns:
chainable method
Type
this

includes(expected, messageopt) → {this}

Source:
Asserts that the actual value includes the expected value using deep equality
Example
assert([{ a: 4 }, { a: 6 }]).includes({ a: 6 }) // Passes
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
{name} does not include the given item error message
Throws:
when the actual value does not include the expected value
Type
AssertionError
Returns:
chainable method
Type
this

includesOnly(expected, messageopt) → {this}

Source:
Asserts that the actual value only includes the expected values using deep equality. It could include duplicates.
Example
assert([1, 2]).includes([2, 1, 3]) // Passes
assert([1, 1]).includes([2, 1, 3]) // Passes
assert([1, 7]).includes([2, 1, 3]) // Fails
Parameters:
Name Type Attributes Default Description
expected Array expected value
message string <optional>
{name} does not include only the given items error message
Throws:
when the actual value does not only include the expected values
Type
AssertionError
Returns:
chainable method
Type
this

includesOwnProperty(name, test, messageopt) → {this}

Source:
Asserts that the actual value has the own given property and run some test on it
Example
assert({ a: 3 }).includesOwnProperty('a', it => it.isAbove(2)) // Passes
Parameters:
Name Type Attributes Default Description
name string name of the property
test assertionCallback test for the property
message string <optional>
{name} does not include the own property {property} error message
Throws:
when the actual value does not have the own given property or the tests fails
Type
AssertionError
Returns:
chainable method
Type
this

includesProperty(name, test, messageopt) → {this}

Source:
Asserts that the actual value has the given property and run some test on it
Example
assert({ a: 3 }).includesProperty('a', it => it.isAbove(2)) // Passes
Parameters:
Name Type Attributes Default Description
name string name of the property
test assertionCallback test for the property
message string <optional>
{name} does not contain the property {property} error message
Throws:
when the actual value does not have the given property or the tests fails
Type
AssertionError
Returns:
chainable method
Type
this

is(expected, messageopt) → {this}

Source:
Alias of module:xassert.Assertion#isDeeplyEqualTo
Example
assert({ c: 3 }).is({ c: 3 }) // Passes
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
{name} is not expected value error message
Throws:
when the actual value is not deeply equal to expected value
Type
AssertionError
Returns:
chainable method
Type
this

isAbove(number, messageopt) → {this}

Source:
Asserts that the actual value is above the given number
Example
assert(4).isAbove(3) // Passes
assert(4).isAbove(4) // Fails
Parameters:
Name Type Attributes Default Description
number number given number
message string <optional>
{name} is not above expected value error message
Throws:
when the actual value is not above the given number
Type
AssertionError
Returns:
chainable method
Type
this

isAnArray(messageopt) → {this}

Source:
Asserts that the actual value is an array
Example
assert([2, 3]).isAnArray() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not an array error message
Throws:
when the actual value is not an array
Type
AssertionError
Returns:
chainable method
Type
this

isANumber(messageopt) → {this}

Source:
Asserts that the actual value is a number
Example
assert(4.3).isANumber() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not a number error message
Throws:
when the actual value is not a number
Type
AssertionError
Returns:
chainable method
Type
this

isAPromise(messageopt) → {this}

Source:
Asserts that the actual value is a promise
Example
assert(Promise.resolve(3)).isAPromise() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not a promise error message
Throws:
when the actual value is not a promise
Type
AssertionError
Returns:
chainable method
Type
this

isAString(messageopt) → {this}

Source:
Asserts that the actual value is a string
Example
assert('banana').isAString() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not a string error message
Throws:
when the actual value is not a string
Type
AssertionError
Returns:
chainable method
Type
this

isAtLeast(number, messageopt) → {this}

Source:
Asserts that the actual value is at least the given number
Example
assert(4).isAtLeast(4) // Passes
assert(4).isAtLeast(5) // Fails
Parameters:
Name Type Attributes Default Description
number number given number
message string <optional>
{name} is not at least as expected value error message
Throws:
when the actual value is not at least the given number
Type
AssertionError
Returns:
chainable method
Type
this

isAtMost(number, messageopt) → {this}

Source:
Asserts that the actual value is at most the given number
Example
assert(4).isAtMost(4) // Passes
assert(4).isAtMost(3) // Fails
Parameters:
Name Type Attributes Default Description
number number given number
message string <optional>
{name} is not at most as expected value error message
Throws:
when the actual value is not at most the given number
Type
AssertionError
Returns:
chainable method
Type
this

isBelow(number, messageopt) → {this}

Source:
Asserts that the actual value is below the given number
Example
assert(4).isBelow(5) // Passes
assert(4).isBelow(4) // Fails
Parameters:
Name Type Attributes Default Description
number number given number
message string <optional>
{name} is not below expected value error message
Throws:
when the actual value is not bellow the given number
Type
AssertionError
Returns:
chainable method
Type
this

isDeeplyEqualTo(expected, messageopt) → {this}

Source:
Asserts that the actual value is deeply equal to expected value
Example
assert({ c: 3 }).isDeeplyEqualTo({ c: 3 }) // Passes
assert({ c: 3 }).isDeeplyEqualTo('3') // Fail
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
{name} is not deeply equal to expected value error message
Throws:
when the actual value is not deeply equal to expected value
Type
AssertionError
Returns:
chainable method
Type
this

isDeeplyEqualToAnyOf(expected, messageopt) → {this}

Source:
Asserts that the actual value is deeply equal to any of expected values
Example
assert({ c: 3 }).isDeeplyEqualToAnyOf([{ a: 3 } ,{ c: 3 }]) // Passes
assert({ c: 3 }).isDeeplyEqualToAnyOf([{ a: 3 } ,{ c: 4 }])') // Fail
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
{name} is different than any of the expected values error message
Throws:
when the actual value is not deeply equal to any of expected values
Type
AssertionError
Returns:
chainable method
Type
this

isEqualTo(expected, messageopt) → {this}

Source:
Asserts that the actual value is strictly equal to expected value
Example
assert(value).isEqualTo('Banana')
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
{name} is different than expected value error message
Throws:
when the actual value is not strictly equal to expected value
Type
AssertionError
Returns:
chainable method
Type
this

isEqualToAnyOf(expected, messageopt) → {this}

Source:
Asserts that the actual value is strictly equal to any of expected values
Example
assert(value).isEqualToAnyOf(['Banana', 'Apple'])
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
{name} is different than any expected value error message
Throws:
when the actual value is not strictly equal to any of expected values
Type
AssertionError
Returns:
chainable method
Type
this

isFalse(messageopt) → {this}

Source:
Asserts that the actual value is strictly false
Example
assert(false).isFalse() // Passes
assert('apple').isFalse() // Fail
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not strictly false error message
Throws:
when the actual value is not strictly false
Type
AssertionError
Returns:
chainable method
Type
this

isFalsy(messageopt) → {this}

Source:
Asserts that the actual value is falsy
Example
assert(false).isFalsy() // Passes
assert(undefined).isFalsy() // Passes
assert('').isFalsy() // Passes
assert('apple').isFalsy() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not falsy error message
Throws:
when the actual value is not falsy
Type
AssertionError
Returns:
chainable method
Type
this

isFrozen(messageopt) → {this}

Source:
Asserts that the actual value is frozen
Example
assert(Object.freeze({ a: 1 })).isFrozen() // Passes
assert({ a: 1 }).isFrozen() // Fail
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not frozen error message
Throws:
when the actual value is not frozen
Type
AssertionError
Returns:
chainable method
Type
this

isFulfilled(testopt, messageopt) → {Promise.<*>}

Source:
Asserts that the promise is fulfilled and the test passes
Example
assert(Promise.resolve(3)).isFulfilled() // Passes
assert(Promise.resolve(3)).isFulfilled(it => it.isEqualTo(3)) // Passes
assert(Promise.resolve(3)).isFulfilled(it => it.isEqualTo(5)) // Fail
assert(Promise.reject(new Error())).isFulfilled() // Fail
Parameters:
Name Type Attributes Default Description
test assertionCallback <optional>
test for the resolved value
message string <optional>
{name} has been rejected error message
Throws:
when the promise is rejected and the test fails
Type
AssertionError
Returns:
resolved promise with the value
Type
Promise.<*>

isInstanceOf(expected, messageopt) → {this}

Source:
Asserts that the actual value is a instance of a given class
Example
assert(new Cat()).isInstanceOf(Animal) // Passes
assert(null).isInstanceOf(Object) // Passes
assert(aCar).isInstanceOf(Plane) // Fail
Parameters:
Name Type Attributes Description
expected number expected class
message string <optional>
error message
Throws:
when the actual value is not a instance of a given class
Type
AssertionError
Returns:
chainable method
Type
this

isNaN(messageopt) → {this}

Source:
Asserts that the actual value is NaN
Example
assert('j').isNaN() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not NaN error message
Throws:
when the actual value is not NaN
Type
AssertionError
Returns:
chainable method
Type
this

isNotAnArray(messageopt) → {this}

Source:
Asserts that the actual value is not an array
Example
assert(33).isNotAnArray() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is an array error message
Throws:
when the actual value is an array
Type
AssertionError
Returns:
chainable method
Type
this

isNotANumber(messageopt) → {this}

Source:
Asserts that the actual value is not a number
Example
assert(4.3).isNotANumber() // Fail
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is a number error message
Throws:
when the actual value is a number
Type
AssertionError
Returns:
chainable method
Type
this

isNotAPromise(messageopt) → {this}

Source:
Asserts that the actual value is not promise
Example
assert(Promise.resolve(3)).isNotAPromise() // Fail
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is a promise error message
Throws:
when the actual value is a promise
Type
AssertionError
Returns:
chainable method
Type
this

isNotAString(messageopt) → {this}

Source:
Asserts that the actual value is not a string
Example
assert('banana').isNotAString() // Fail
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is a string error message
Throws:
when the actual value is a string
Type
AssertionError
Returns:
chainable method
Type
this

isNotDeeplyEqualTo(expected, messageopt) → {this}

Source:
Asserts that the actual value is not deeply equal to expected value
Example
assert({ c: 3 }).isNotDeeplyEqualTo({ c: 3 }) // Fail
assert({ c: 3 }).isNotDeeplyEqualTo('3') // Passes
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
actual is deeply equal to expected error message
Throws:
when the actual value is deeply equal to expected values
Type
AssertionError
Returns:
chainable method
Type
this

isNotDeeplyEqualToAnyOf(expected, messageopt) → {this}

Source:
Asserts that the actual value is not deeply equal to any of expected values
Example
assert({ c: 3 }).isNotDeeplyEqualToAnyOf([{ a: 3 } ,{ c: 3 }]) // Fail
assert({ c: 3 }).isNotDeeplyEqualToAnyOf([{ a: 3 } ,{ c: 4 }])') // Passes
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
{name} is equal one of non expected values error message
Throws:
when the actual value is deeply equal to any of expected values
Type
AssertionError
Returns:
chainable method
Type
this

isNotEqualTo(expected, messageopt) → {this}

Source:
Asserts that the actual value is not strictly equal to expected value
Example
assert(value).isNotEqualTo('Banana')
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
{name} is equal to expected value error message
Throws:
when the actual value is strictly equal to expected value
Type
AssertionError
Returns:
chainable method
Type
this

isNotEqualToAnyOf(expected, messageopt) → {this}

Source:
Asserts that the actual value is not strictly equal to any of expected values
Example
assert(value).isNotEqualToAnyOf(['Banana', 'Apple'])
Parameters:
Name Type Attributes Default Description
expected * expected value
message string <optional>
{name} is equal to some expected value error message
Throws:
when the actual value is strictly equal to any of expected values
Type
AssertionError
Returns:
chainable method
Type
this

isNotFrozen(messageopt) → {this}

Source:
Asserts that the actual value is not frozen
Example
assert(Object.freeze({ a: 1 })).isNotFrozen() // Fail
assert({ a: 1 }).isNotFrozen() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is frozen error message
Throws:
when the actual value is frozen
Type
AssertionError
Returns:
chainable method
Type
this

isNotNaN(messageopt) → {this}

Source:
Asserts that the actual value is not NaN
Example
assert('j').isNotNaN() // Fail
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is NaN error message
Throws:
when the actual value is NaN
Type
AssertionError
Returns:
chainable method
Type
this

isNotNull(messageopt) → {this}

Source:
Asserts that the actual value is not null
Example
assert('a').isNotNull() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is null error message
Throws:
when the actual value is not null
Type
AssertionError
Returns:
chainable method
Type
this

isNotUndefined(messageopt) → {this}

Source:
Asserts that the actual value is not undefined
Example
assert(undefined).isNotUndefined() // Fail
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is undefined error message
Throws:
when the actual value is undefined
Type
AssertionError
Returns:
chainable method
Type
this

isNull(messageopt) → {this}

Source:
Asserts that the actual value is null
Example
assert(null).isNull() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not null error message
Throws:
when the actual value is not null
Type
AssertionError
Returns:
chainable method
Type
this

isRejected(testopt, messageopt) → {Promise.<*>}

Source:
Asserts that the promise is rejected and the test passes
Example
assert(Promise.resolve(3)).isRejected() // Fail
assert(Promise.reject(new Error)).isRejected(it => it.isInstanceOf(Error)) // Passes
Parameters:
Name Type Attributes Default Description
test assertionCallback <optional>
test for the resolved value
message string <optional>
{name} has been fulfilled error message
Throws:
when the promise is fulfilled and the test fails
Type
AssertionError
Returns:
resolved promise with the error
Type
Promise.<*>

isTrue(messageopt) → {this}

Source:
Asserts that the actual value is strictly true
Example
assert(true).isTrue() // Passes
assert('apple').isTrue() // Fail
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not strictly true error message
Throws:
when the actual value is not strictly true
Type
AssertionError
Returns:
chainable method
Type
this

isTruthy(messageopt) → {this}

Source:
Asserts that the actual value is truthy
Example
assert(true).isTruthy() // Passes
assert('apple').isTruthy() // Passes
assert(null).isTruthy() // Fail
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not truthy error message
Throws:
when the actual value is not truthy
Type
AssertionError
Returns:
chainable method
Type
this

isUndefined(messageopt) → {this}

Source:
Asserts that the actual value is undefined
Example
assert(undefined).isUndefined() // Passes
Parameters:
Name Type Attributes Default Description
message string <optional>
{name} is not undefined error message
Throws:
when the actual value is not null
Type
AssertionError
Returns:
chainable method
Type
this

item(index, test) → {this}

Source:
Asserts that the actual value at a given index pass a tests
Example
assert([3, 6]).every(it => it.isAbove(2)) // Passes
Parameters:
Name Type Description
index number index to be tested
test assertionCallback test
Throws:
when any value at a given index fails the test
Type
AssertionError
Returns:
chainable method
Type
this

matches(reopt, messageopt) → {this}

Source:
Asserts that the actual value matches the given regular expression.
Example
assert(() => throw new Error()).throwsAn(Error) // Passes
assert(() => throw new Error()).throwsAn(InvalidFormat) // Fails
Parameters:
Name Type Attributes Default Description
re RegExp <optional>
regular expression
message string <optional>
{name} does not match the given regular expression: {regexp} error message
Throws:
when the actual value does not match the given regular expression
Type
AssertionError
Returns:
chainable method
Type
this

named(name) → {Assertion}

Source:
Parameters:
Name Type Description
name string name of the field
Returns:
new ValueAssertion with the same value and a new name
Type
Assertion

some(test, messageopt) → {this}

Source:
Asserts that some value of the array pass the test
Example
assert([3, 6]).some(it => it.isAbove(5)) // Passes
Parameters:
Name Type Attributes Default Description
test assertionCallback test for each element
message string <optional>
{name} does not contain any item that passes any test error message
Throws:
when no value passes the test
Type
AssertionError
Returns:
chainable method
Type
this

startsWith(str, messageopt) → {this}

Source:
Asserts that the actual value starts with the given string.
Example
assert('abcd').startsWith('ab') // Passes
assert('abcd').startsWith('bc') // Fails
Parameters:
Name Type Attributes Default Description
str string given string
message string <optional>
{name} does not start with the given string error message
Throws:
when the actual value does not start with the given string
Type
AssertionError
Returns:
chainable method
Type
this

throws(testopt, messageopt) → {this}

Source:
Asserts that the provided function throws an exception and optionally tests the error
Example
assert(() => throw new Error()).throws() // Passes
assert(() => throw new Error()).throws(it => it.isInstanceOf(Error)) // Passes
assert(() => 3).throws() // Fails
Parameters:
Name Type Attributes Default Description
test assertionCallback <optional>
test error
message string <optional>
{name} did not throw error message
Throws:
when the provided function does not throw an exception or the test fails
Type
AssertionError
Returns:
chainable method
Type
this

throwsA(classRefopt, messageopt) → {this}

Source:
Asserts that the provided function throws the given exception
Example
assert(() => throw new NotFoundError()).throwsA(NotFoundError) // Passes
assert(() => throw new ServerError()).throwsA(NotFoundError) // Fails
Parameters:
Name Type Attributes Default Description
classRef function <optional>
class reference
message string <optional>
{name} is not a {class} error message
Throws:
when the provided function does not throw the given exception
Type
AssertionError
Returns:
chainable method
Type
this

throwsAn(classRefopt, messageopt) → {this}

Source:
Asserts that the provided function throws the given exception. Alias of module:xassert.Assertion#throwsA
Example
assert(() => throw new Error()).throwsAn(Error) // Passes
assert(() => throw new Error()).throwsAn(InvalidFormat) // Fails
Parameters:
Name Type Attributes Default Description
classRef function <optional>
class reference
message string <optional>
{name} in not an {class} error message
Throws:
when the provided function does not throw the given exception
Type
AssertionError
Returns:
chainable method
Type
this