BDD assertion library designed to work seamlessly with the hapi ecosystem
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Precise value checking for specific JavaScript values and special numeric values.
Asserts that the reference value is exactly true.
/**
* Asserts that the reference value is true
* @returns {Assertion} Assertion chain object for further chaining
*/
true()Usage Examples:
const Code = require('@hapi/code');
const expect = Code.expect;
expect(true).to.be.true();
expect(1 === 1).to.be.true();
expect(Boolean(1)).to.be.true();
// These will fail
expect(1).to.not.be.true(); // truthy but not true
expect('true').to.not.be.true(); // string, not booleanAsserts that the reference value is exactly false.
/**
* Asserts that the reference value is false
* @returns {Assertion} Assertion chain object for further chaining
*/
false()Usage Examples:
expect(false).to.be.false();
expect(1 === 2).to.be.false();
expect(Boolean(0)).to.be.false();
// These will fail
expect(0).to.not.be.false(); // falsy but not false
expect('false').to.not.be.false(); // string, not booleanAsserts that the reference value is exactly null.
/**
* Asserts that the reference value is null
* @returns {Assertion} Assertion chain object for further chaining
*/
null()Usage Examples:
expect(null).to.be.null();
let unassignedVariable = null;
expect(unassignedVariable).to.be.null();
// These will fail
expect(undefined).to.not.be.null();
expect(0).to.not.be.null();
expect('').to.not.be.null();Asserts that the reference value is exactly undefined.
/**
* Asserts that the reference value is undefined
* @returns {Assertion} Assertion chain object for further chaining
*/
undefined()Usage Examples:
expect(undefined).to.be.undefined();
let unassignedVariable;
expect(unassignedVariable).to.be.undefined();
const obj = {};
expect(obj.nonExistentProperty).to.be.undefined();
// These will fail
expect(null).to.not.be.undefined();
expect(0).to.not.be.undefined();
expect('').to.not.be.undefined();Asserts that the reference value is NaN (Not a Number).
/**
* Asserts that the reference value is NaN
* @returns {Assertion} Assertion chain object for further chaining
*/
NaN()Usage Examples:
expect(NaN).to.be.NaN();
expect(Number('invalid')).to.be.NaN();
expect(parseInt('abc')).to.be.NaN();
expect(0 / 0).to.be.NaN();
expect(Math.sqrt(-1)).to.be.NaN();
// These will fail
expect(0).to.not.be.NaN();
expect(Infinity).to.not.be.NaN();
expect('NaN').to.not.be.NaN(); // string, not numberValue assertions can be combined with other assertions and used with negation:
const Code = require('@hapi/code');
const expect = Code.expect;
// Multiple value checks
const result = someFunction();
expect(result).to.not.be.null().and.not.be.undefined();
// Type and value combination
expect(true).to.be.a.boolean().and.be.true();
expect(false).to.be.a.boolean().and.be.false();
// Function return value checking
function getBooleanValue() {
return true;
}
expect(getBooleanValue()).to.be.true();
// Conditional value checking
const config = getConfiguration();
if (config.enabled !== undefined) {
expect(config.enabled).to.be.a.boolean();
if (config.enabled) {
expect(config.enabled).to.be.true();
} else {
expect(config.enabled).to.be.false();
}
}Special consideration for null and undefined values:
// Check existence (not null AND not undefined)
expect(someValue).to.exist(); // Passes if not null and not undefined
// Check for null specifically
expect(someValue).to.be.null(); // Only passes for null
// Check for undefined specifically
expect(someValue).to.be.undefined(); // Only passes for undefined
// Check for either null or undefined
expect(someValue).to.not.exist(); // Passes if null OR undefinedSince NaN has special behavior in JavaScript:
// NaN is not equal to itself
expect(NaN).to.not.equal(NaN); // This passes!
// Use .NaN() method instead
expect(NaN).to.be.NaN(); // Proper way to check for NaN
// Check if something is NOT NaN
expect(42).to.not.be.NaN();
expect('hello').to.not.be.NaN();
// Working with calculations that might produce NaN
const result = parseFloat(userInput);
if (isNaN(result)) {
expect(result).to.be.NaN();
} else {
expect(result).to.be.a.number().and.not.be.NaN();
}