Mathematical comparison operations for numbers and numeric values.
Asserts that the reference value is greater than the provided value.
/**
* Asserts that the reference value is greater than provided value
* @param {number} value - Value to compare against
* @returns {Assertion} Assertion chain object for further chaining
*/
above(value)Alias:
greaterThan()Usage Examples:
const Code = require('@hapi/code');
const expect = Code.expect;
expect(10).to.be.above(5);
expect(3.14).to.be.above(3);
expect(-1).to.be.above(-5);
// With aliases
expect(100).to.be.greaterThan(50);
// Chaining
expect(userAge).to.be.a.number().and.be.above(18);Asserts that the reference value is at least (greater than or equal to) the provided value.
/**
* Asserts that the reference value is at least (>=) provided value
* @param {number} value - Minimum value
* @returns {Assertion} Assertion chain object for further chaining
*/
least(value)Alias:
min()Usage Examples:
expect(10).to.be.at.least(10); // Equal is allowed
expect(15).to.be.at.least(10); // Greater is allowed
expect(18).to.be.at.min(18); // Using alias
// Age validation
expect(userAge).to.be.at.least(21);Asserts that the reference value is less than the provided value.
/**
* Asserts that the reference value is less than provided value
* @param {number} value - Value to compare against
* @returns {Assertion} Assertion chain object for further chaining
*/
below(value)Alias:
lessThan()Usage Examples:
expect(5).to.be.below(10);
expect(-10).to.be.below(-5);
expect(2.5).to.be.lessThan(3);
// Performance testing
const executionTime = measureTime();
expect(executionTime).to.be.below(1000); // Less than 1 secondAsserts that the reference value is at most (less than or equal to) the provided value.
/**
* Asserts that the reference value is at most (<=) provided value
* @param {number} value - Maximum value
* @returns {Assertion} Assertion chain object for further chaining
*/
most(value)Alias:
max()Usage Examples:
expect(10).to.be.at.most(10); // Equal is allowed
expect(5).to.be.at.most(10); // Less is allowed
expect(100).to.be.at.max(100); // Using alias
// Resource limits
expect(memoryUsage).to.be.at.most(1024);Asserts that the reference value is within the specified range (inclusive of both endpoints).
/**
* Asserts that the reference value is within range (inclusive)
* @param {number} from - Lower bound (inclusive)
* @param {number} to - Upper bound (inclusive)
* @returns {Assertion} Assertion chain object for further chaining
*/
within(from, to)Alias:
range()Usage Examples:
expect(5).to.be.within(1, 10); // 1 <= 5 <= 10
expect(1).to.be.within(1, 10); // Lower bound inclusive
expect(10).to.be.within(1, 10); // Upper bound inclusive
// Using alias
expect(score).to.be.in.range(0, 100);
// Temperature validation
expect(temperature).to.be.within(-10, 40);Asserts that the reference value is between the specified values (exclusive of both endpoints).
/**
* Asserts that the reference value is between values (exclusive)
* @param {number} from - Lower bound (exclusive)
* @param {number} to - Upper bound (exclusive)
* @returns {Assertion} Assertion chain object for further chaining
*/
between(from, to)Usage Examples:
expect(5).to.be.between(1, 10); // 1 < 5 < 10
expect(1).to.not.be.between(1, 10); // Lower bound exclusive
expect(10).to.not.be.between(1, 10); // Upper bound exclusive
// Exclusive range checking
const percentage = calculatePercentage();
expect(percentage).to.be.between(0, 100); // 0 < percentage < 100Asserts that the reference value is approximately equal to the provided value within a specified delta.
/**
* Asserts that the reference value is approximately equal within delta
* @param {number} value - Target value
* @param {number} delta - Maximum allowed difference
* @returns {Assertion} Assertion chain object for further chaining
*/
about(value, delta)Usage Examples:
expect(10).to.be.about(9, 1); // |10 - 9| <= 1
expect(3.14159).to.be.about(3.14, 0.01); // Floating point comparison
expect(100).to.be.about(95, 10); // Within 10 units
// Floating point calculations
const result = 0.1 + 0.2; // JavaScript floating point issues
expect(result).to.be.about(0.3, 0.0001);
// Performance measurements with tolerance
const actualTime = measureExecutionTime();
const expectedTime = 500; // 500ms
expect(actualTime).to.be.about(expectedTime, 50); // ±50ms toleranceconst Code = require('@hapi/code');
const expect = Code.expect;
// Multiple constraints
expect(userAge)
.to.be.a.number()
.and.be.above(0)
.and.be.at.most(150);
// Range validation with multiple approaches
const score = getTestScore();
expect(score)
.to.be.a.number()
.and.be.within(0, 100)
.and.not.be.about(0, 5); // Not too close to zero
// Performance testing
const startTime = Date.now();
performOperation();
const duration = Date.now() - startTime;
expect(duration)
.to.be.a.number()
.and.be.above(0)
.and.be.below(1000) // Less than 1 second
.and.be.about(expectedDuration, 100); // Within expected range// Validate all numbers in range
const temperatures = [-5, 0, 15, 23, 35];
temperatures.forEach(temp => {
expect(temp).to.be.within(-50, 50);
});
// Statistical validation
const scores = [85, 92, 78, 95, 88];
const average = scores.reduce((a, b) => a + b) / scores.length;
expect(average).to.be.above(80).and.be.below(100);
scores.forEach(score => {
expect(score).to.be.above(0).and.be.at.most(100);
});// Avoid exact equality for floating point
const result = Math.sqrt(2) * Math.sqrt(2);
expect(result).to.not.equal(2); // Might fail due to precision
expect(result).to.be.about(2, 0.0001); // Better approach
// Scientific calculations
const pi = Math.PI;
const calculatedPi = calculatePiApproximation();
expect(calculatedPi).to.be.about(pi, 0.001);
// Currency calculations (use integers to avoid floating point issues)
const priceInCents = 1050; // $10.50
expect(priceInCents).to.be.above(1000).and.be.below(2000);// Test edge cases
const minValue = Number.MIN_SAFE_INTEGER;
const maxValue = Number.MAX_SAFE_INTEGER;
expect(minValue).to.be.below(0);
expect(maxValue).to.be.above(0);
// Infinity handling
expect(Infinity).to.be.above(1000000);
expect(-Infinity).to.be.below(-1000000);
// Zero boundaries
expect(0).to.be.at.least(0);
expect(0).to.be.at.most(0);
expect(1).to.be.above(0);
expect(-1).to.be.below(0);