GitHub

test()

Flexible test function for various comparison and validation operations.

Core Function
Testing
Validation
Syntax
test(type, actual, expected, options)
Parameters

type
string

The type of test to perform. See supported types below.

actual
any

The actual value to test.

expected
any

The expected value or parameter for the test.

options
object
optional

Optional settings to customize behavior.

  • ignoreCase - Case-insensitive string comparisons
  • tolerance - Numeric tolerance for approximate equality
  • deep - Perform deep equality checks for objects/arrays
Returns

boolean
- Result of the test: true if passes, false otherwise.

Supported Test Types
equals

Check if two values are equal

notEquals

Check if two values are not equal

greaterThan

Check if first value is greater than second

lessThan

Check if first value is less than second

contains

Check if string/array contains a value

startsWith

Check if string starts with a value

endsWith

Check if string ends with a value

isType

Check the type of a value

inRange

Check if number is within a range

regexTest

Test string against regex pattern

custom

Use custom predicate function

Examples

Basic Equality

test("equals", 5, 5); // true
test("equals", "hello", "hello"); // true
test("notEquals", 5, 10); // true

String Operations

test("contains", "Hello World", "world", { ignoreCase: true }); // true
test("startsWith", "JavaScript", "Java"); // true
test("endsWith", "filename.txt", ".txt"); // true

Numeric Comparisons

test("greaterThan", 10, 5); // true
test("inRange", 10, [5, 15]); // true
test("equals", 3.14159, 3.14, { tolerance: 0.01 }); // true

Type Checking

test("isType", [], "array"); // true
test("isType", {}, "object"); // true
test("isType", null, "null"); // true
test("isFunction", () => {}, true); // true

Deep Equality

const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
test("equals", obj1, obj2, { deep: true }); // true

const arr1 = [1, [2, 3]];
const arr2 = [1, [2, 3]];
test("equals", arr1, arr2, { deep: true }); // true

Custom Predicates

test("custom", 42, (value) => value % 2 === 0); // true
test("custom", "hello", (value) => value.length > 3); // true