Modules@robinpath/assert
assert

@robinpath/assert

0.1.2Node.jsPublic

Testing assertions: equal, deepEqual, truthy, falsy, type checks, includes, matches, throws, and more

Assert

Testing assertions: equal, deepEqual, truthy, falsy, type checks, includes, matches, throws, and more

Package: @robinpath/assert | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the assert module when you need to:

  • Assert two values are strictly equal (===) -- Use assert.equal to perform this operation
  • Assert two values are not equal -- Use assert.notEqual to perform this operation
  • Assert deep equality of two values -- Use assert.deepEqual to perform this operation
  • Assert value is truthy -- Use assert.truthy to perform this operation
  • Assert value is falsy -- Use assert.falsy to perform this operation

Quick Reference

FunctionDescriptionReturns
equalAssert two values are strictly equal (===)True if equal, throws otherwise
notEqualAssert two values are not equalTrue if not equal
deepEqualAssert deep equality of two valuesTrue if deeply equal
truthyAssert value is truthyTrue if truthy
falsyAssert value is falsyTrue if falsy
isNullAssert value is null or undefinedTrue if null/undefined
isNotNullAssert value is not null/undefinedTrue if not null
isTypeAssert typeof value matches expected typeTrue if type matches
includesAssert array/string includes a valueTrue if includes
matchesAssert string matches a regex patternTrue if matches
throwsAssert that a function throwsTrue if throws
lengthOfAssert array/string has specific lengthTrue if length matches
hasPropertyAssert object has a specific propertyTrue if has property
isAboveAssert number is above thresholdTrue if above
isBelowAssert number is below thresholdTrue if below

Functions

equal

Assert two values are strictly equal (===)

Module: assert | Returns: boolean -- True if equal, throws otherwise

assert.equal $a $b
ParameterTypeRequiredDescription
actualanyYesActual value
expectedanyYesExpected value

notEqual

Assert two values are not equal

Module: assert | Returns: boolean -- True if not equal

assert.notEqual $a $b
ParameterTypeRequiredDescription
actualanyYesActual value
expectedanyYesValue to not match

deepEqual

Assert deep equality of two values

Module: assert | Returns: boolean -- True if deeply equal

assert.deepEqual $obj1 $obj2
ParameterTypeRequiredDescription
actualanyYesActual value
expectedanyYesExpected value

truthy

Assert value is truthy

Module: assert | Returns: boolean -- True if truthy

assert.truthy $val
ParameterTypeRequiredDescription
valueanyYesValue to check

falsy

Assert value is falsy

Module: assert | Returns: boolean -- True if falsy

assert.falsy $val
ParameterTypeRequiredDescription
valueanyYesValue to check

isNull

Assert value is null or undefined

Module: assert | Returns: boolean -- True if null/undefined

assert.isNull $val
ParameterTypeRequiredDescription
valueanyYesValue to check

isNotNull

Assert value is not null/undefined

Module: assert | Returns: boolean -- True if not null

assert.isNotNull $val
ParameterTypeRequiredDescription
valueanyYesValue to check

isType

Assert typeof value matches expected type

Module: assert | Returns: boolean -- True if type matches

assert.isType $val "string"
ParameterTypeRequiredDescription
valueanyYesValue to check
typestringYesExpected type string

includes

Assert array/string includes a value

Module: assert | Returns: boolean -- True if includes

assert.includes "hello" "ell"
ParameterTypeRequiredDescription
haystackanyYesArray or string to search
needleanyYesValue to find

matches

Assert string matches a regex pattern

Module: assert | Returns: boolean -- True if matches

assert.matches "hello" "^h"
ParameterTypeRequiredDescription
strstringYesString to test
patternstringYesRegex pattern

throws

Assert that a function throws

Module: assert | Returns: boolean -- True if throws

assert.throws $fn
ParameterTypeRequiredDescription
fnstringYesFunction to call

lengthOf

Assert array/string has specific length

Module: assert | Returns: boolean -- True if length matches

assert.lengthOf $arr 3
ParameterTypeRequiredDescription
valueanyYesArray or string
lengthnumberYesExpected length

hasProperty

Assert object has a specific property

Module: assert | Returns: boolean -- True if has property

assert.hasProperty $obj "name"
ParameterTypeRequiredDescription
objobjectYesObject to check
propertystringYesProperty name

isAbove

Assert number is above threshold

Module: assert | Returns: boolean -- True if above

assert.isAbove 5 3
ParameterTypeRequiredDescription
valuenumberYesNumber to check
thresholdnumberYesThreshold

isBelow

Assert number is below threshold

Module: assert | Returns: boolean -- True if below

assert.isBelow 3 5
ParameterTypeRequiredDescription
valuenumberYesNumber to check
thresholdnumberYesThreshold

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Expected ${JSON.stringify(args[0])} to equal ${JSON.stringify(args[1])}Check the error message for details
Expected values to not be equal: ${JSON.stringify(args[0])}Check the error message for details
Expected deep equality.\nActual: ${JSON.stringify(args[0])}\nExpected: ${JSON.stringify(args[1])}Check the error message for details
Expected truthy value, got: ${JSON.stringify(args[0])}Check the error message for details
Expected falsy value, got: ${JSON.stringify(args[0])}Check the error message for details
Expected null/undefined, got: ${JSON.stringify(args[0])}Check the error message for details
Expected non-null value, got null/undefinedCheck the error message for details
Expected array to include ${JSON.stringify(needle)}Check the error message for details
@desc "Equal and validate result"
do
  set $result as assert.equal $a $b
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Assert workflow

Chain multiple assert operations together.

@desc "Equal, not equal, and more"
do
  set $r_equal as assert.equal $a $b
  set $r_notEqual as assert.notEqual $a $b
  set $r_deepEqual as assert.deepEqual $obj1 $obj2
  print "All operations complete"
enddo

2. Safe equal with validation

Check results before proceeding.

@desc "Equal and validate result"
do
  set $result as assert.equal $a $b
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/assert

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size4.9 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities