![]() When we compile and run the code, the function doesn’t return anything on the output. Then we are comparing the return value of the function with some number in the value parameter of the assert() function.Īssert(output = 10, 'five plus five is ten') In the example below, we are creating a function and performed the addition of two numbers. So when we compile and run the code, it will not throw an AssertionError to the output. In the example below, we are calling the Node.js assert() function by passing a truthy value into the value parameter and a text to the message parameter of the function. Knowing the existence of this module and how you can use it can be helpful also if you want to take a Node.js certification, according to which I read. ![]() Thus the result is false.ĪssertionError : Ten minus seven is fourĪt Object. The Node.js assert API is simple and easy to use, but can be useful if a test suit like Jest is too much to your requirements at a certain moment. When we compile and run the code, it will throw an AssertionError to the output. Then we are comparing the return value of the function with some number in the value parameter of the Node.js assert() function.Īssert(output = 4, 'Ten minus seven is four') In the example below, we are creating a function and performing the subtraction of two numbers. (/home/cg/root/63b28b24b5cef/main.js:2:1)Īt Module._compile (internal/modules/cjs/loader.js:702:30)at Object.Module._extensions.js (internal/modules/cjs/loader.js:713:10) Using this value, we can customize the ERR_ASSERTION statements.Īssert(NaN, "The Given value is not a number ") ĪssertionError : The Given value is not a numberĪt Object. In the following example we are passing a string as the optional parameter (message). (/home/cg/root/639c16a06de3d/main.js:3:1)Īt Module._compile (internal/modules/cjs/loader.js:702:30)Īt Object.Module._extensions.js (internal/modules/cjs/loader.js:713:10)Īt Module.load (internal/modules/cjs/loader.js:612:32)Īt tryModuleLoad (internal/modules/cjs/loader.js:551:12)Īt Function.Module._load (internal/modules/cjs/loader.js:543:3)Īt (internal/modules/cjs/loader.js:744:10)Īt startup (internal/bootstrap/node.js:238:19)Īt bootstrapNodeJSCore (internal/bootstrap/node.js:572:3) Since the given value is falsy, when we compile and run the code, it will throw AssertionError.ĪssertionError : This is a falsy valueĪt Object. In the example below, we are calling the Node.js assert() function by passing the Boolean value “false” as a parameter. This function will return the AssertionError on the terminal if the value is not true. Message − (optional) String or Error type can be passed as a value to this parameter. Value − (required) The value passed in this parameter will be evaluated for being truthy. Syntaxįollowing is the syntax of the Node.js assert() function − This is an inbuilt function of the assert module, which provides a set of assertion functions for verifying invariants. This function works similarly to assert.ok() function. If the value is true it won’t throw any error. This function throws an AssertionError, when the value we pass as a parameter is not false. All the values are considered as truthy except: false, 0, −0, 0n, "", null, undefined, and NaN. Where truthy values are the Boolean or, non-Boolean values which are treated as “true” when used with comparison operators. The Node.js assert() function is used to verify whether the given value is truthy or not.
0 Comments
Leave a Reply. |