What is the assert.isNull() method in Chai.js?
Overview
In Chai.js, assert.isNull() is used to check if a value is null or not.
It returns an error if the value passed is not null, and it returns nothing if the value passed is null.
Syntax
assert.isNull(val)
Syntax for assert.isNulll() nethod in Chai.js
Parameters
val: This is the value we want to check.
Return value
If the test succeeds, it returns. If the test fails, it returns an error.
Example
// import the chai module and assert interfaceconst {assert} = require('chai')// function to check if values are nullconst checkIfNull= (val1, val2) =>{try{// use the isNull() methodassert.isNull(val1)}catch(error){console.log(error.message)}}checkIfNull(true)checkIfNull(false)checkIfNull(null) // test succeededcheckIfNull(undefined)checkIfNull(0)
Explanation
- Line 2: We import the
chaimodule as well as theassert.isNull()method. - Line 5–12: We create a function that runs the test using the
assert.isNull()method. It takes the value we want to check if they arenull. We use thetry/catchblock to catch any errors if the test fails. - Lines 14–18: We invoke the function and pass some values to test if they are
null.