1.5.0 • Published 1 year ago

@mangar2/unittest v1.5.0

Weekly downloads
10
License
LGPL-3.0-or-later
Repository
github
Last release
1 year ago

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

Fileunittest.js
AbstractThis module provides a simple class to support unit tests , this is not a framework . Use "testrun" , as a simple uinit test framework
AuthorVolker Böhm
CopyrightCopyright ( c ) 2020 Volker Böhm
LicenseThis 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

NameTypeDescription
verbosebooleantrue to give more output
debugbooltrue , to print a stack trace on fail

UnitTest Members

NameTypedescription
debugboolSet debug to true , to print a stack trace on fail
verboseboolSet verbose to true to print all success infos

UnitTest Methods

assertDeepEqual

assertDeepEqual (a, b, message) => {boolean}

Compares two objects deeply

assertDeepEqual Parameters
NameTypeDescription
aanyfirst object to compare
banysecond object to compare
messagestringmessage to show
assertDeepEqual returns
TypeDescription
booleantrue/false based on success

assertEqual

assertEqual (a, b, message) => {boolean}

Compares two values , fails if they are not equal " = = "

assertEqual Parameters
NameTypeDescription
aanyfirst value
banysecond value
messagestringinfo message on success/fail
assertEqual returns
TypeDescription
booleantrue/false based on success

assertFalse

assertFalse (test, message) => {boolean}

Tests a value for beeing false , fails if the value is true

assertFalse Parameters
NameTypeDescription
testbooleantest value
messagestringinfo message on success/fail
assertFalse returns
TypeDescription
booleantrue/false based on success

assertTrue

assertTrue (test, message) => {boolean}

Tests a value for beeing true , fails if the value is false

assertTrue Parameters
NameTypeDescription
testbooleantest value
messagestringinfo message on success/fail
assertTrue returns
TypeDescription
booleantrue/false based on success

delay

delay (timeInMilliseconds) => {Promise}

Pauses the execution for a while ( needs to "wait" ) for the result .

delay Parameters
NameTypeDescription
timeInMillisecondsnumberdelay in milliseconds
delay returns
TypeDescription
Promiseto wait for

expectException

expectException (callback, instance) => {boolean}

Runs a function and checks for a returned expection

expectException Parameters
NameTypeDescription
callbackfunctionfunction causing the expection
instancestringinstance of the exception
expectException returns
TypeDescription
booleantrue on expection

fail

fail (message)

Adds a failure

fail Parameters
NameTypeDescription
messagestringmessage to print on failure

log

log (info)

Logs a string , if verbose

log Parameters
NameTypeDescription
infostringinfo to log

logError

logError (err)

Logs an error usually from a catch section

logError Parameters
NameTypeDescription
errError, stringerror information

showResult

showResult (expectedAmount)

Show the overall test result . Exits the process with "1" on failure

showResult Parameters
NameTypeDescription
expectedAmountnumberexpected amount of positive test in the current run

success

success (message)

Adds a success

success Parameters
NameTypeDescription
messagestringmessage 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
NameTypeAttributeDefaultDescription
toValidateanyelement to be validated
expectedanydescription of expected element
pathstringpath to the element to compare
exactstringoptionalfalse
validateRec throws
TypeDescription
stringfirst 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
NameTypeAttributeDefaultDescription
resultObjectobject to be validated
expectedObjectexpected object
pathstringpath to be displayed on success/error
exactbooleanoptionalfalsetrue , if result may not have additional properties
validateResult returns
TypeDescription
booleantrue , if the test is ok
1.5.0

1 year ago

1.4.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.0

5 years ago