assert.notEqual() Function in Node.js
0 116
Introduction to assert.notEqual() in Node.js
Node.js provides a built-in module called assert
for writing test cases and validating outcomes. One of the key functions available in this module is assert.notEqual()
. This method checks whether two values are not equal using the != operator. If they are equal, the assertion fails and an error is thrown. It’s a straightforward way to ensure two values differ in your tests.
Purpose of assert.notEqual()
The assert.notEqual()
function is used to confirm that the actual output is not equal to an expected value. It performs a loose comparison (non-strict inequality), which means that different data types may still be considered equal if their values match loosely. For stricter comparisons, you should use assert.notStrictEqual()
.
Syntax
Here's the syntax of assert.notEqual()
:
assert.notEqual(actual, expected[, message])
- actual – The actual value to test.
- expected – The value it should not be equal to.
- message – Optional custom message to display if the assertion fails.
Example Usage
Let’s take a quick look at how assert.notEqual()
works in a basic example:
const assert = require('assert');
const result = 5 + 3;
const expected = 10;
assert.notEqual(result, expected, 'Result should not be equal to expected value.');
In this case, the result is 8
, and the expected value is 10
, so the assertion passes successfully. If both were equal, the assertion would fail and throw an error.
Comparison: notEqual() vs notStrictEqual()
The main difference between these two lies in how the comparison is done:
- assert.notEqual(): Uses loose inequality (
!=
) and allows type coercion. - assert.notStrictEqual(): Uses strict inequality (
!==
) and compares both value and type.
Here’s an example to demonstrate the difference:
assert.notEqual(5, '5'); // Fails, because 5 != '5' is false (values are equal when coerced)
assert.notStrictEqual(5, '5'); // Passes, because types differ
Use Cases for assert.notEqual()
This method is helpful in scenarios where you want to make sure a value does not match a specific expected value, such as:
- Ensuring input doesn't match a restricted value
- Checking incorrect or unexpected outputs
- Validating logic flows in negative test cases
Handling Errors
If the values turn out to be equal, the assertion will throw an AssertionError
. You can catch and handle this using a try-catch block:
try {
assert.notEqual(100, 100, 'Values should not be equal');
} catch (err) {
console.error('Assertion failed:', err.message);
}
Conclusion
assert.notEqual()
is a simple but essential function for testing in Node.js. It helps developers confirm that certain values are different, catching issues where values unexpectedly match. Use it when type isn’t a concern but inequality is crucial to your test logic.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments