Abstract
This module provides a simple class to support unit tests , this is not a framework . Use "testrun" , as a simple uinit test framework
Contents
Meta
File | unittest.js |
Abstract | This module provides a simple class to support unit tests , this is not a framework . Use "testrun" , as a simple uinit test framework |
Author | Volker Böhm |
Copyright | Copyright ( c ) 2020 Volker Böhm |
License | This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose . |
Class UnitTest
new UnitTest(verbose, debug)
Creates an object supporting unit tests
Example
const unitTest = new UnitTest(true, true)
unitTest.assertEqual('1', '1', 'equal')
unitTest.assertTrue(true, 'true')
unitTest.assertFalse(false, 'false')
unitTest.success('success')
unitTest.log('just a log')
unitTest.assertDeepEqual({ a: 'b' }, { a: 'b' })
unitTest.expectException(() => { throw new Error('Hello World') }, 'Error', 'message')
UnitTest Parameters
Name | Type | Description |
---|---|---|
verbose |
boolean |
true to give more output |
debug |
bool |
true , to print a stack trace on fail |
UnitTest Members
Name | Type | description |
---|---|---|
debug |
bool |
Set debug to true , to print a stack trace on fail |
verbose |
bool |
Set verbose to true to print all success infos |
UnitTest Methods
assertDeepEqual
assertDeepEqual (a, b, message) => {boolean}
Compares two objects deeply
assertDeepEqual Parameters
Name | Type | Description |
---|---|---|
a |
any |
first object to compare |
b |
any |
second object to compare |
message |
string |
message to show |
assertDeepEqual returns
Type | Description |
---|---|
boolean |
true/false based on success |
assertEqual
assertEqual (a, b, message) => {boolean}
Compares two values , fails if they are not equal " = = "
assertEqual Parameters
Name | Type | Description |
---|---|---|
a |
any |
first value |
b |
any |
second value |
message |
string |
info message on success/fail |
assertEqual returns
Type | Description |
---|---|
boolean |
true/false based on success |
assertFalse
assertFalse (test, message) => {boolean}
Tests a value for beeing false , fails if the value is true
assertFalse Parameters
Name | Type | Description |
---|---|---|
test |
boolean |
test value |
message |
string |
info message on success/fail |
assertFalse returns
Type | Description |
---|---|
boolean |
true/false based on success |
assertTrue
assertTrue (test, message) => {boolean}
Tests a value for beeing true , fails if the value is false
assertTrue Parameters
Name | Type | Description |
---|---|---|
test |
boolean |
test value |
message |
string |
info message on success/fail |
assertTrue returns
Type | Description |
---|---|
boolean |
true/false based on success |
delay
delay (timeInMilliseconds) => {Promise}
Pauses the execution for a while ( needs to "wait" ) for the result .
delay Parameters
Name | Type | Description |
---|---|---|
timeInMilliseconds |
number |
delay in milliseconds |
delay returns
Type | Description |
---|---|
Promise |
to wait for |
expectException
expectException (callback, instance) => {boolean}
Runs a function and checks for a returned expection
expectException Parameters
Name | Type | Description |
---|---|---|
callback |
function |
function causing the expection |
instance |
string |
instance of the exception |
expectException returns
Type | Description |
---|---|
boolean |
true on expection |
fail
fail (message)
Adds a failure
fail Parameters
Name | Type | Description |
---|---|---|
message |
string |
message to print on failure |
log
log (info)
Logs a string , if verbose
log Parameters
Name | Type | Description |
---|---|---|
info |
string |
info to log |
logError
logError (err)
Logs an error usually from a catch section
logError Parameters
Name | Type | Description |
---|---|---|
err |
Error, string |
error information |
showResult
showResult (expectedAmount)
Show the overall test result . Exits the process with "1" on failure
showResult Parameters
Name | Type | Description |
---|---|---|
expectedAmount |
number |
expected amount of positive test in the current run |
success
success (message)
Adds a success
success Parameters
Name | Type | Description |
---|---|---|
message |
string |
message to print on success ( if verbose is true ) |
validateRec
validateRec (toValidate, expected, path, exact)
Checks an element recursively for differences . It only checks values that are specified in "expected" and ignores additional data in result . This enables us to specify only the values that we like to check in the test cases .
validateRec Parameters
Name | Type | Attribute | Default | Description |
---|---|---|---|---|
toValidate |
any |
element to be validated | ||
expected |
any |
description of expected element | ||
path |
string |
path to the element to compare | ||
exact |
string |
optional | false |
validateRec throws
Type | Description |
---|---|
string |
first difference |
validateResult
validateResult (result, expected, path, exact) => {boolean}
Validates a result object against an expected object . It validates with exact equal , that "result" has all object properties of expected .
validateResult Parameters
Name | Type | Attribute | Default | Description |
---|---|---|---|---|
result |
Object |
object to be validated | ||
expected |
Object |
expected object | ||
path |
string |
path to be displayed on success/error | ||
exact |
boolean |
optional | false | true , if result may not have additional properties |
validateResult returns
Type | Description |
---|---|
boolean |
true , if the test is ok |