Static
containsAsserts that an array or string contains a specified value or substring.
For arrays, uses indexOf
for shallow equality.
For strings, uses indexOf
for substring check.
Throws AssertionError
if the value is not found.
The array or string to search.
The value (or substring) to search for.
(Optional) Message to prefix in case of failure.
Static
doesAsserts that the provided function does NOT throw an error.
If an error is thrown, an AssertionError
is thrown with the provided message or details of the error.
A function that is expected to NOT throw.
Must be passed as a function reference, e.g. () => codeThatShouldNotThrow()
.
(Optional) Prefix for the error message if the assertion fails.
Assert.throws for the opposite assertion.
Static
equalsAsserts that two values are equal by type and value.
Supports comparison of primitive types, one-dimensional arrays of primitives,
and one-dimensional arrays of objects (deep equality via JSON.stringify
).
If the values differ, a detailed error is thrown.
For arrays, mismatches include index, value, and type.
For arrays of objects, a shallow comparison using JSON.stringify
is performed.
If a value cannot be stringified (e.g., due to circular references), it is treated as [unprintable value]
in error messages and object equality checks.
T
if the assertion passes.Assert.equals(2 + 2, 4, "Simple math")
Assert.equals(["a", "b"], ["a", "b"], "Array match")
Assert.equals([1, "2"], [1, 2], "Array doesn't match") // Fails
Assert.equals([{x:1}], [{x:1}], "Object array match") // Passes
Assert.equals([{x:1}], [{x:2}], "Object array mismatch") // Fails
Assert.notEquals for the opposite assertion.
Static
failFails the test by throwing an error with the provided message. This method is used to explicitly indicate that a test case has failed, regardless of any conditions or assertions.
Optional
message: string(Optional) The failure message to display.
If not provided, a default Assertion failed
message is used.
Static
isAsserts that the given value is defined (not undefined
).
Alias for isNotUndefined
method.
The value to check.
(Optional) Message to prefix in case of failure.
Assert.isDefined(42, "Value should be defined")
Assert.isDefined(null, "Null is allowed, but not undefined")
Assert.isDefined(42)
Assert.isDefined(null)
Static
isAsserts that the provided value is falsy.
Throws AssertionError
if the value is not falsy.
The value to test for falsiness.
(Optional) Message to prefix in case of failure.
Assert.isFalse(1 > 2, "Math sanity")
Assert.isFalse(null, "Null should be falsy")
Assert.isFalse(undefined, "Undefined should be falsy")
Assert.isFalse(false, "Boolean false should be falsy")
Assert.isFalse(0, "Zero should be falsy")
Assert.isFalse("", "Empty string should be falsy")
Assert.isTrue for the opposite assertion.
Static
isAsserts that the value is an instance of the specified constructor.
Throws AssertionError
if not.
The value to check.
The class/constructor function.
Optional error message prefix.
class MyClass {}
const instance = new MyClass()
Assert.isInstanceOf(instance, MyClass, "Should be an instance of MyClass")
Assert.isInstanceOf(instance, Object) // Passes, since all classes inherit from Object
Assert.isInstanceOf(42, MyClass) // Fails
Assert.isNotInstanceOf for the opposite assertion.
Static
isAsserts that the value is NOT an instance of the specified constructor.
Throws AssertionError
if it is.
The value to check.
The class/constructor function.
Optional error message prefix.
class MyClass {}
const instance = new MyClass()
Assert.isNotInstanceOf(instance, String, "Should not be an instance of String")
Assert.isNotInstanceOf(instance, MyClass) // Fails
Assert.isNotInstanceOf(42, MyClass) // Passes, since 42 is not an instance of MyClass
Assert.isInstanceOf for the opposite assertion.
Static
isAsserts that the given value is not null
.
Provides a robust stringification of the value for error messages,
guarding against unsafe or error-throwing toString()
implementations.
Narrows the type of value to NonNullable<T>
if assertion passes.
The value to test.
Optional message to prefix in case of failure.
NonNullable<T>
if the assertion passes.Assert.isNotNull(42, "Value should not be null")
Assert.isNotNull(null, "Value should be null") // Fails
null
or undefined
).Static
isAsserts that a value is NOT of the specified primitive type.
The value to check.
The unwanted type as a string (string
, number
, etc.)
Optional error message.
isNotType("hello", "number"); // passes
isNotType(42, "string"); // passes
isNotType({}, "object"); // throws
isNotType(null, "object", "Should not be object"); // throws (null is object in JS)
This method checks the type using typeof
and throws an AssertionError
if the type matches.
The type
parameter must be one of the following strings: string
, number
, boolean
, object
, function
, undefined
, symbol
, or bigint
.
Assert.isType for the positive assertion.
Static
isAsserts that the given value is not undefined
.
Narrows the type to exclude undefined
.
Throws AssertionError
if the value is undefined
.
The value to check.
(Optional) Message to prefix in case of failure.
Assert.isNotUndefined(42, "Value should not be undefined")
Assert.isNotUndefined(null, "Null is allowed, but not undefined")
Assert.isNotUndefined(42)
Assert.isNotUndefined(null)
undefined
).Static
isAsserts that the given value is strictly null
.
Provides a robust stringification of the value for error messages,
guarding against unsafe or error-throwing toString()
implementations.
The value to test.
Optional message to prefix in case of failure.
null
if the assertion passes.Assert.isNull(null, "Value should be null")
Assert.isNull(undefined, "Value should not be undefined") // Fails
Assert.isNull(0, "Zero is not null") // Fails
Assert.isNull(null)
Assert.isNull(undefined) // Fails
Assert.isNull(0) // Fails
Assert.isDefined for an alias that checks for defined values (not null
or undefined
).
Static
isAsserts that the provided value is truthy.
Throws AssertionError
if the value is not truthy.
The value to test for truthiness.
(Optional) Message to prefix in case of failure.
Assert.isTrue(1 < 2, "Math sanity")
Assert.isTrue("non-empty string", "String should be truthy")
Assert.isFalse for the opposite assertion.
Static
isAsserts that a value is of the specified primitive type.
The value to check.
The expected type as a string (string
, number
, etc.)
Optional error message.
isType("hello", "string"); // passes
isType(42, "number"); // passes
isType({}, "string"); // throws
isType([], "object", "Expected an object"); // passes
isType(null, "object", "Expected an object"); // throws
This method checks the type using typeof
and throws an AssertionError
if the type does not match.
It is useful for validating input types in functions or methods.
The type
parameter must be one of the following strings: string
, number
, boolean
, object
, function
, undefined
, symbol
, or bigint
.
If the value is null
, it will be considered an object, which is consistent with JavaScript's behavior.
Assert.isNotType for the opposite assertion.
Static
isAsserts that the given value is strictly undefined
.
Throws AssertionError
if the value is not exactly undefined
.
The value to check.
(Optional) Message to prefix in case of failure.
undefined
if the assertion passes.Assert.isUndefined(void 0)
Assert.isUndefined(undefined)
Assert.isUndefined(null, "Null is not undefined") // Fails
undefined
).Static
notAsserts that two values are not equal (deep comparison).
For arrays and objects, uses deep comparison (via JSON.stringify
).
Throws AssertionError
if the values are equal.
Assert.notEquals(1, 2, "Numbers should not be equal")
Assert.notEquals([1, 2], [2, 1], "Arrays should not be equal")
Assert.notEquals({ a: 1 }, { a: 2 }, "Objects should not be equal")
Assert.notEquals(1, 2)
Assert.notEquals([1,2], [2,1])
Assert.equals for the opposite assertion.
Static
throwsAsserts that the provided function throws an error. Optionally checks the error type and message.
A function that is expected to throw an error.
Must be passed as a function reference, e.g. () => codeThatThrows()
.
Optional
expectedErrorType: Function(Optional) Expected constructor of the thrown error (e.g., TypeError
).
Optional
expectedMessage: string(Optional) Exact expected error message.
(Optional) Additional prefix for the error message if the assertion fails.
AssertionError - If no error is thrown, or if the thrown error does not match the expected type or message.
Assert.throws(() => {
throw new TypeError("Invalid")
}, TypeError, "Invalid", "Should throw TypeError")
Assert.doesNotThrow for the opposite assertion.
Utility class for writing unit-test-style assertions. This class provides a set of static methods to perform common assertions such as checking equality, type, and exceptions, etc. In case of assertion failure, an
AssertionError
is thrown and the user can provide a descriptive message.