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 comparisonstolerance- Numeric tolerance for approximate equalitydeep- Perform deep equality checks for objects/arrays
Returns
boolean
- Result of the test: true if passes, false otherwise.Supported Test Types
equalsCheck if two values are equal
notEqualsCheck if two values are not equal
greaterThanCheck if first value is greater than second
lessThanCheck if first value is less than second
containsCheck if string/array contains a value
startsWithCheck if string starts with a value
endsWithCheck if string ends with a value
isTypeCheck the type of a value
inRangeCheck if number is within a range
regexTestTest string against regex pattern
customUse custom predicate function
Examples
Basic Equality
test("equals", 5, 5); // true
test("equals", "hello", "hello"); // true
test("notEquals", 5, 10); // trueString Operations
test("contains", "Hello World", "world", { ignoreCase: true }); // true
test("startsWith", "JavaScript", "Java"); // true
test("endsWith", "filename.txt", ".txt"); // trueNumeric Comparisons
test("greaterThan", 10, 5); // true
test("inRange", 10, [5, 15]); // true
test("equals", 3.14159, 3.14, { tolerance: 0.01 }); // trueType Checking
test("isType", [], "array"); // true
test("isType", {}, "object"); // true
test("isType", null, "null"); // true
test("isFunction", () => {}, true); // trueDeep 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 }); // trueCustom Predicates
test("custom", 42, (value) => value % 2 === 0); // true
test("custom", "hello", (value) => value.length > 3); // true