assert

The assert module provides some simple assertion methods for type checks, truthyness tests and guards invalid code paths. When importing the module as default module, it is the assert function itself.

It can be imported as assert from 'laxar';

Contents

Module Members

Types

Module Members

assert( subject, optionalDetails )

Creates and returns a new Assertion instance for the given subject.

Note: this function is no member of the module, but the module itself. Thus when using assert via laxar, assert is will be no simple object, but this function having the other functions as properties.

Example:

import { assert } from 'laxar';
assert( assert ).hasType( Function );
assert.state( typeof assert.codeIsUnreachable === 'function' );
Parameters
Property Type Description
subject * the object assertions are made for
optionalDetails String details that should be printed in case no specific details are given when calling an assertion method
Returns
Type Description
Assertion the assertion instance

Types

Assertion

Assertion.isNotNull( optionalDetails )

Throws an error if the subject is null or undefined.

Parameters
Property Type Description
optionalDetails String details to append to the error message
Returns
Type Description
Assertion this instance

Assertion.hasType( type, optionalDetails )

Throws an error if the subject is not of the given type. No error is thrown for null or undefined.

Parameters
Property Type Description
type Function the expected type of the subject
optionalDetails String details to append to the error message
Returns
Type Description
Assertion this instance

Assertion.hasProperty( property, optionalDetails )

Throws an error if the subject is no object or the given property is not defined on it.

Parameters
Property Type Description
property String the property that is expected for the subject
optionalDetails String details to append to the error message
Returns
Type Description
Assertion this instance

assert

assert.codeIsUnreachable( optionalDetails )

Marks a code path as erroneous by throwing an error when reached.

Parameters
Property Type Description
optionalDetails String details to append to the error message

assert.state( expression, optionalDetails )

Throws an error if the given expression is falsy.

Parameters
Property Type Description
expression * the expression to test for truthyness
optionalDetails String details to append to the error message