We can use Chai.js
module for testing our JavaScript applications. The assert
is an interface of this module. We can use the equal()
method to check if a particular value is equal to another value.
Note: The
equal()
method is similar to double equality (==
).
assert.equal(val1, val2)
val1
: This is the first value we want to check. val2
: This is the second value to check the equality with the first value.Nothing is returned if the values are equal. But an error is thrown if val1
and val2
are not equal.
// import the chai module and assert interfaceconst {assert} = require('chai')// function to check if values are equalconst checkIfEqual= (val1, val2) =>{try{// use the equal() methodassert.equal(val1, val2)}catch(error){console.log(error.message)}}checkIfEqual(true, true)checkIfEqual(false, true) // test failedcheckIfEqual([], {}) // test failedcheckIfEqual(100, "100")checkIfEqual(null, "null") // test failedcheckIfEqual(null, null)checkIfEqual(undefined, undefined)
chai
module together with its assert
interface.assert.equal()
method. It will take the two values we want to check for their equality. With the try/catch
block, we will catch any error thrown if the test fails.