Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

chai.js

What is assert.isObject() method in Chai.js?

Theodore Kelechukwu Onyejiaku

Overview

We may use the assert interface method isObject(), available in the testing Library chai.js or chai, to know if a value is an object or not.

Syntax

Let's view the syntax of the method given below:

assert.isObject(value)
Syntax for the assert.isObject() method in Chai

Parameters

value: This is the value we want to test to see if it is an Object or not.

Return value

If the test fails, then an error is thrown. The test is successful if nothing is returned.

Example

// import the chai module and assert interface
const {assert} = require('chai')

// function to check if value is an object
const checkIfObject= (val) =>{
  try{
    // use the isObject() method
    assert.isObject(val)
  }catch(error){
    console.log(error.message)
  }
}

// invoke the function
checkIfObject({})                         // test succeeds
checkIfObject([1, 2, 3, 4])
checkIfObject(console)
checkIfObject(assert.isObject)
checkIfObject({language: "JavaScript"})   // test succeeds
checkIfObject(new Date())
checkIfObject(new Map([["year", 2022]]))
checkIfObject(module)                     // test succeeds
checkIfObject(exports)                    // test succeeds                                             
Using the assert.isObject() method in Chai.js

Explanation

  • Line 2: We'll use the require() function to import the chai package and its interface assert.
  • Line 5: We'll create a function checkIfObject() that takes a value and invokes the method assert.isObject() on the value. With the try-catch block, we'll handle any exception thrown if the test fails.
  • Lines 15–23: We'll invoke the function.

RELATED TAGS

chai.js

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring