Comprehensive type checking for JavaScript primitives, built-in objects, and custom types.
Asserts that the reference value is an arguments object.
/**
* Asserts that the reference value is an arguments object
* @returns {Assertion} Assertion chain object for further chaining
*/
arguments()Usage Examples:
const Code = require('@hapi/code');
const expect = Code.expect;
function testFunction() {
expect(arguments).to.be.arguments();
}
testFunction(1, 2, 3); // PassAsserts that the reference value is an Array.
/**
* Asserts that the reference value is an Array
* @returns {Assertion} Assertion chain object for further chaining
*/
array()Usage Examples:
expect([1, 2, 3]).to.be.an.array();
expect([]).to.be.an.array();
expect('not array').to.not.be.an.array();Asserts that the reference value is a boolean.
/**
* Asserts that the reference value is a boolean
* @returns {Assertion} Assertion chain object for further chaining
*/
boolean()Usage Examples:
expect(true).to.be.a.boolean();
expect(false).to.be.a.boolean();
expect(1).to.not.be.a.boolean();Asserts that the reference value is a Buffer.
/**
* Asserts that the reference value is a Buffer
* @returns {Assertion} Assertion chain object for further chaining
*/
buffer()Usage Examples:
expect(Buffer.from('hello')).to.be.a.buffer();
expect(Buffer.alloc(10)).to.be.a.buffer();
expect('not buffer').to.not.be.a.buffer();Asserts that the reference value is a Date.
/**
* Asserts that the reference value is a Date
* @returns {Assertion} Assertion chain object for further chaining
*/
date()Usage Examples:
expect(new Date()).to.be.a.date();
expect(new Date('2023-01-01')).to.be.a.date();
expect('2023-01-01').to.not.be.a.date();Asserts that the reference value is an error, with optional type and message constraints.
/**
* Asserts that the reference value is an error
* @param {Function} [type] - Constructor function the error must be instance of
* @param {string|RegExp} [message] - String or regex the error message must match
* @returns {Assertion} Assertion chain object for further chaining
*/
error(type, message)Usage Examples:
const err = new Error('Something went wrong');
// Basic error check
expect(err).to.be.an.error();
// Check error type
expect(err).to.be.an.error(Error);
// Check error message (exact string)
expect(err).to.be.an.error('Something went wrong');
// Check error message (regex)
expect(err).to.be.an.error(/went wrong/);
// Check both type and message
expect(err).to.be.an.error(Error, /went wrong/);
// Custom error types
class CustomError extends Error {}
const customErr = new CustomError('Custom message');
expect(customErr).to.be.an.error(CustomError);Asserts that the reference value is a function.
/**
* Asserts that the reference value is a function
* @returns {Assertion} Assertion chain object for further chaining
*/
function()Usage Examples:
expect(() => {}).to.be.a.function();
expect(function() {}).to.be.a.function();
expect(console.log).to.be.a.function();
expect('not function').to.not.be.a.function();Asserts that the reference value is a number.
/**
* Asserts that the reference value is a number
* @returns {Assertion} Assertion chain object for further chaining
*/
number()Usage Examples:
expect(123).to.be.a.number();
expect(3.14).to.be.a.number();
expect(Infinity).to.be.a.number();
expect(NaN).to.be.a.number(); // NaN is of type number
expect('123').to.not.be.a.number();Asserts that the reference value is a RegExp.
/**
* Asserts that the reference value is a RegExp
* @returns {Assertion} Assertion chain object for further chaining
*/
regexp()Usage Examples:
expect(/abc/).to.be.a.regexp();
expect(new RegExp('abc')).to.be.a.regexp();
expect('abc').to.not.be.a.regexp();Asserts that the reference value is a string.
/**
* Asserts that the reference value is a string
* @returns {Assertion} Assertion chain object for further chaining
*/
string()Usage Examples:
expect('hello').to.be.a.string();
expect('').to.be.a.string();
expect(String(123)).to.be.a.string();
expect(123).to.not.be.a.string();Asserts that the reference value is an object, excluding arrays, buffers, and other native objects.
/**
* Asserts that the reference value is an object (excluding arrays, buffers, etc.)
* @returns {Assertion} Assertion chain object for further chaining
*/
object()Usage Examples:
expect({}).to.be.an.object();
expect({ a: 1, b: 2 }).to.be.an.object();
// These are NOT considered objects by this assertion
expect([]).to.not.be.an.object(); // Array
expect(new Date()).to.not.be.an.object(); // Date
expect(Buffer.from('')).to.not.be.an.object(); // Buffer
expect(null).to.not.be.an.object(); // nullType assertions can be chained with other assertions:
const Code = require('@hapi/code');
const expect = Code.expect;
// Chain type assertion with other checks
expect([1, 2, 3])
.to.be.an.array()
.and.have.length(3)
.and.include(2);
expect('hello world')
.to.be.a.string()
.and.contain('world')
.and.have.length(11);
// Use with negation
expect(42)
.to.be.a.number()
.and.not.be.a.string()
.and.be.above(40);